Revert "TODO epan/dissectors/asn1/kerberos/packet-kerberos-template.c new GSS flags"
[wireshark-sm.git] / epan / dissectors / packet-ipmi.c
blob4d5da5bc49efc81a15c5da969b310d1920dff293
1 /* packet-ipmi.c
2 * Routines for IPMI dissection
3 * Copyright 2002-2008, Alexey Neyman, Pigeon Point Systems <avn@pigeonpoint.com>
5 * Wireshark - Network traffic analyzer
6 * By Gerald Combs <gerald@wireshark.org>
7 * Copyright 1998 Gerald Combs
9 * SPDX-License-Identifier: GPL-2.0-or-later
12 #define WS_LOG_DOMAIN "packet-ipmi"
14 #include "config.h"
15 #include <wireshark.h>
17 #include <epan/packet.h>
18 #include <epan/conversation.h>
19 #include <epan/prefs.h>
20 #include <epan/addr_resolv.h>
22 #include "packet-ipmi.h"
24 static dissector_handle_t ipmi_i2c_handle;
26 void proto_register_ipmi(void);
27 void proto_reg_handoff_ipmi(void);
30 * See the IPMI specifications at
32 * http://www.intel.com/design/servers/ipmi/
35 /* Top-level search structure: list of registered handlers for a given netFn */
36 struct ipmi_netfn_root {
37 ipmi_netfn_t *list;
38 const char *desc;
39 uint32_t siglen;
42 enum {
43 MSGFMT_NONE = 0,
44 MSGFMT_IPMB,
45 MSGFMT_LAN,
46 MSGFMT_GUESS
49 struct ipmi_parse_typelen {
50 void (*get_len)(unsigned *, unsigned *, tvbuff_t *, unsigned, unsigned, bool);
51 void (*parse)(char *, tvbuff_t *, unsigned, unsigned);
52 const char *desc;
55 /* IPMI parsing context */
56 typedef struct {
57 ipmi_header_t hdr;
58 unsigned hdr_len;
59 unsigned flags;
60 uint8_t cks1;
61 uint8_t cks2;
62 } ipmi_context_t;
64 /* Temporary request-response matching data. */
65 typedef struct {
66 /* Request header */
67 ipmi_header_t hdr;
68 /* Frame number where the request resides */
69 uint32_t frame_num;
70 /* Nest level of the request in the frame */
71 uint8_t nest_level;
72 } ipmi_request_t;
74 /* List of request-response matching data */
75 typedef wmem_list_t ipmi_request_list_t;
77 #define NSAVED_DATA 2
79 /* Per-command data */
80 typedef struct {
81 uint32_t matched_frame_num;
82 uint32_t saved_data[NSAVED_DATA];
83 } ipmi_cmd_data_t;
85 /* Per-frame data */
86 typedef struct {
87 ipmi_cmd_data_t * cmd_data[3];
88 nstime_t ts;
89 } ipmi_frame_data_t;
91 /* RB tree of frame data */
92 typedef wmem_tree_t ipmi_frame_tree_t;
94 /* cached dissector data */
95 typedef struct {
96 /* tree of cached frame data */
97 ipmi_frame_tree_t * frame_tree;
98 /* list of cached requests */
99 ipmi_request_list_t * request_list;
100 /* currently dissected frame number */
101 uint32_t curr_frame_num;
102 /* currently dissected frame */
103 ipmi_frame_data_t * curr_frame;
104 /* current nesting level */
105 uint8_t curr_level;
106 /* subsequent nesting level */
107 uint8_t next_level;
108 /* top level message channel */
109 uint8_t curr_channel;
110 /* top level message direction */
111 uint8_t curr_dir;
112 /* pointer to current command */
113 const ipmi_header_t * curr_hdr;
114 /* current completion code */
115 uint8_t curr_ccode;
116 } ipmi_packet_data_t;
118 /* Maximum nest level where it worth caching data */
119 #define MAX_NEST_LEVEL 3
121 int proto_ipmi;
122 static int proto_ipmb;
123 static int proto_kcs;
124 static int proto_tmode;
126 /* WARNING: Setting this to true might result in the entire dissector being
127 disabled by default or removed completely. */
128 static bool dissect_bus_commands;
129 static bool fru_langcode_is_english = true;
130 static unsigned response_after_req = 5000;
131 static unsigned response_before_req;
132 static unsigned message_format = MSGFMT_GUESS;
133 static unsigned selected_oem = IPMI_OEM_NONE;
135 static int hf_ipmi_command_data;
136 static int hf_ipmi_session_handle;
137 static int hf_ipmi_header_trg;
138 static int hf_ipmi_header_trg_lun;
139 static int hf_ipmi_header_netfn;
140 static int hf_ipmi_header_crc;
141 static int hf_ipmi_header_src;
142 static int hf_ipmi_header_src_lun;
143 static int hf_ipmi_header_bridged;
144 static int hf_ipmi_header_sequence;
145 static int hf_ipmi_header_command;
146 static int hf_ipmi_header_completion;
147 static int hf_ipmi_header_sig;
148 static int hf_ipmi_data_crc;
149 static int hf_ipmi_response_to;
150 static int hf_ipmi_response_in;
151 static int hf_ipmi_response_time;
153 static int ett_ipmi;
154 static int ett_header;
155 static int ett_header_byte_1;
156 static int ett_header_byte_4;
157 static int ett_data;
158 static int ett_typelen;
160 static expert_field ei_impi_parser_not_implemented;
162 static struct ipmi_netfn_root ipmi_cmd_tab[IPMI_NETFN_MAX];
164 static ipmi_packet_data_t *
165 get_packet_data(packet_info * pinfo)
167 ipmi_packet_data_t * data;
169 /* get conversation data */
170 conversation_t * conv = find_or_create_conversation(pinfo);
172 /* get protocol-specific data */
173 data = (ipmi_packet_data_t *)
174 conversation_get_proto_data(conv, proto_ipmi);
176 if (!data) {
177 /* allocate per-packet data */
178 data = wmem_new0(wmem_file_scope(), ipmi_packet_data_t);
180 /* allocate request list and frame tree */
181 data->frame_tree = wmem_tree_new(wmem_file_scope());
182 data->request_list = wmem_list_new(wmem_file_scope());
184 /* add protocol data */
185 conversation_add_proto_data(conv, proto_ipmi, data);
188 /* check if packet has changed */
189 if (pinfo->num != data->curr_frame_num) {
190 data->curr_level = 0;
191 data->next_level = 0;
194 return data;
197 static ipmi_frame_data_t *
198 get_frame_data(ipmi_packet_data_t * data, uint32_t frame_num)
200 ipmi_frame_data_t * frame = (ipmi_frame_data_t *)
201 wmem_tree_lookup32(data->frame_tree, frame_num);
203 if (frame == NULL) {
204 frame = wmem_new0(wmem_file_scope(), ipmi_frame_data_t);
206 wmem_tree_insert32(data->frame_tree, frame_num, frame);
208 return frame;
211 static ipmi_request_t *
212 get_matched_request(ipmi_packet_data_t * data, const ipmi_header_t * rs_hdr,
213 unsigned flags)
215 wmem_list_frame_t * iter = wmem_list_head(data->request_list);
216 ipmi_header_t rq_hdr;
218 /* reset message context */
219 rq_hdr.context = 0;
221 /* copy channel */
222 rq_hdr.channel = data->curr_channel;
224 /* toggle packet direction */
225 rq_hdr.dir = rs_hdr->dir ^ 1;
227 rq_hdr.session = rs_hdr->session;
229 /* swap responder address/lun */
230 rq_hdr.rs_sa = rs_hdr->rq_sa;
231 rq_hdr.rs_lun = rs_hdr->rq_lun;
233 /* remove reply flag */
234 rq_hdr.netfn = rs_hdr->netfn & ~1;
236 /* swap requester address/lun */
237 rq_hdr.rq_sa = rs_hdr->rs_sa;
238 rq_hdr.rq_lun = rs_hdr->rs_lun;
240 /* copy sequence */
241 rq_hdr.rq_seq = rs_hdr->rq_seq;
243 /* copy command */
244 rq_hdr.cmd = rs_hdr->cmd;
246 /* TODO: copy prefix bytes */
248 ws_debug("%d, %d: rq_hdr : {"
249 "\tchannel=%d"
250 "\tdir=%d"
251 "\trs_sa=%x"
252 "\trs_lun=%d"
253 "\tnetfn=%x"
254 "\trq_sa=%x"
255 "\trq_lun=%d"
256 "\trq_seq=%x"
257 "\tcmd=%x }",
258 data->curr_frame_num, data->curr_level,
259 rq_hdr.channel, rq_hdr.dir, rq_hdr.rs_sa, rq_hdr.rs_lun,
260 rq_hdr.netfn, rq_hdr.rq_sa, rq_hdr.rq_lun, rq_hdr.rq_seq,
261 rq_hdr.cmd);
263 while (iter) {
264 ipmi_request_t * rq = (ipmi_request_t *) wmem_list_frame_data(iter);
266 /* check if in Get Message context */
267 if (rs_hdr->context == IPMI_E_GETMSG && !(flags & IPMI_D_TRG_SA)) {
268 /* diregard rsSA */
269 rq_hdr.rq_sa = rq->hdr.rq_sa;
272 /* compare command headers */
273 if (!memcmp(&rq_hdr, &rq->hdr, sizeof(rq_hdr))) {
274 return rq;
277 /* proceed to next request */
278 iter = wmem_list_frame_next(iter);
281 return NULL;
284 static void
285 remove_old_requests(ipmi_packet_data_t * data, const nstime_t * curr_time)
287 wmem_list_frame_t * iter = wmem_list_head(data->request_list);
289 while (iter) {
290 ipmi_request_t * rq = (ipmi_request_t *) wmem_list_frame_data(iter);
291 ipmi_frame_data_t * frame = get_frame_data(data, rq->frame_num);
292 nstime_t delta;
294 /* calculate time delta */
295 nstime_delta(&delta, curr_time, &frame->ts);
297 if (nstime_to_msec(&delta) > response_after_req) {
298 wmem_list_frame_t * del = iter;
300 /* proceed to next request */
301 iter = wmem_list_frame_next(iter);
303 /* free request data */
304 wmem_free(wmem_file_scope(), rq);
306 /* remove list item */
307 wmem_list_remove_frame(data->request_list, del);
308 } else {
309 break;
314 static void
315 match_request_response(ipmi_packet_data_t * data, const ipmi_header_t * hdr,
316 unsigned flags)
318 /* get current frame */
319 ipmi_frame_data_t * rs_frame = data->curr_frame;
321 /* get current command data */
322 ipmi_cmd_data_t * rs_data = rs_frame->cmd_data[data->curr_level];
324 /* check if parse response for the first time */
325 if (!rs_data) {
326 ipmi_request_t * rq;
328 /* allocate command data */
329 rs_data = wmem_new0(wmem_file_scope(), ipmi_cmd_data_t);
331 /* search for matching request */
332 rq = get_matched_request(data, hdr, flags);
334 /* check if matching request is found */
335 if (rq) {
336 /* get request frame data */
337 ipmi_frame_data_t * rq_frame =
338 get_frame_data(data, rq->frame_num);
340 /* get command data */
341 ipmi_cmd_data_t * rq_data = rq_frame->cmd_data[rq->nest_level];
343 /* save matched frame numbers */
344 rq_data->matched_frame_num = data->curr_frame_num;
345 rs_data->matched_frame_num = rq->frame_num;
347 /* copy saved command data information */
348 rs_data->saved_data[0] = rq_data->saved_data[0];
349 rs_data->saved_data[1] = rq_data->saved_data[1];
351 /* remove request from the list */
352 wmem_list_remove(data->request_list, rq);
354 /* delete request data */
355 wmem_free(wmem_file_scope(), rq);
358 /* save command data pointer in frame */
359 rs_frame->cmd_data[data->curr_level] = rs_data;
363 static void
364 add_request(ipmi_packet_data_t * data, const ipmi_header_t * hdr)
366 /* get current frame */
367 ipmi_frame_data_t * rq_frame = data->curr_frame;
369 /* get current command data */
370 ipmi_cmd_data_t * rq_data = rq_frame->cmd_data[data->curr_level];
372 /* check if parse response for the first time */
373 if (!rq_data) {
374 ipmi_request_t * rq;
376 /* allocate command data */
377 rq_data = wmem_new0(wmem_file_scope(), ipmi_cmd_data_t);
379 /* set command data pointer */
380 rq_frame->cmd_data[data->curr_level] = rq_data;
382 /* allocate request data */
383 rq = wmem_new0(wmem_file_scope(), ipmi_request_t);
385 /* copy request header */
386 memcpy(&rq->hdr, hdr, sizeof(rq->hdr));
388 /* override context, channel and direction */
389 rq->hdr.context = 0;
390 rq->hdr.channel = data->curr_channel;
391 rq->hdr.dir = data->curr_dir;
393 /* set request frame number */
394 rq->frame_num = data->curr_frame_num;
396 /* set command nest level */
397 rq->nest_level = data->curr_level;
399 /* append request to list */
400 wmem_list_append(data->request_list, rq);
402 ws_debug("%d, %d: rq_hdr : {"
403 "\tchannel=%d"
404 "\tdir=%d"
405 "\trs_sa=%x"
406 "\trs_lun=%d"
407 "\tnetfn=%x"
408 "\trq_sa=%x"
409 "\trq_lun=%d"
410 "\trq_seq=%x"
411 "\tcmd=%x }",
412 data->curr_frame_num, data->curr_level,
413 rq->hdr.channel, rq->hdr.dir, rq->hdr.rs_sa, rq->hdr.rs_lun,
414 rq->hdr.netfn, rq->hdr.rq_sa, rq->hdr.rq_lun, rq->hdr.rq_seq,
415 rq->hdr.cmd);
419 static void
420 add_command_info(packet_info *pinfo, const ipmi_cmd_t * cmd,
421 bool resp, uint8_t cc_val, const char * cc_str, bool broadcast)
423 if (resp) {
424 col_add_fstr(pinfo->cinfo, COL_INFO, "Rsp, %s, %s (%02xh)",
425 cmd->desc, cc_str, cc_val);
426 } else {
427 col_add_fstr(pinfo->cinfo, COL_INFO, "Req, %s%s",
428 broadcast ? "Broadcast " : "", cmd->desc);
432 static int
433 dissect_ipmi_cmd(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
434 int hf_parent_item, int ett_tree, const ipmi_context_t * ctx)
436 ipmi_packet_data_t * data;
437 ipmi_netfn_t * cmd_list;
438 const ipmi_cmd_t * cmd;
439 proto_item * ti;
440 proto_tree * cmd_tree = NULL, * tmp_tree;
441 uint8_t prev_level, cc_val;
442 unsigned offset, siglen, is_resp;
443 const char * cc_str, * netfn_str;
445 if (!dissect_bus_commands) {
446 ti = proto_tree_add_item(tree, hf_parent_item, tvb, 0, -1, ENC_NA);
447 cmd_tree = proto_item_add_subtree(ti, ett_tree);
448 proto_tree_add_item(cmd_tree, hf_ipmi_command_data, tvb, 0, -1, ENC_NA);
449 return 0;
452 /* get packet data */
453 data = get_packet_data(pinfo);
454 if (!data) {
455 return 0;
458 /* get prefix length */
459 siglen = ipmi_getsiglen(ctx->hdr.netfn);
461 /* get response flag */
462 is_resp = ctx->hdr.netfn & 1;
464 /* check message length */
465 if (tvb_captured_length(tvb) < ctx->hdr_len + siglen + is_resp
466 + !(ctx->flags & IPMI_D_NO_CKS)) {
467 /* don bother with anything */
468 return call_data_dissector(tvb, pinfo, tree);
471 /* save nest level */
472 prev_level = data->curr_level;
474 /* assign next nest level */
475 data->curr_level = data->next_level;
477 /* increment next nest level */
478 data->next_level++;
480 /* check for the first invocation */
481 if (!data->curr_level) {
482 /* get current frame data */
483 data->curr_frame = get_frame_data(data, pinfo->num);
484 data->curr_frame_num = pinfo->num;
486 /* copy frame timestamp */
487 memcpy(&data->curr_frame->ts, &pinfo->abs_ts, sizeof(nstime_t));
489 /* cache channel and direction */
490 data->curr_channel = ctx->hdr.channel;
491 data->curr_dir = ctx->hdr.dir;
493 /* remove requests which are too old */
494 remove_old_requests(data, &pinfo->abs_ts);
497 if (data->curr_level < MAX_NEST_LEVEL) {
498 if (ctx->hdr.netfn & 1) {
499 /* perform request/response matching */
500 match_request_response(data, &ctx->hdr, ctx->flags);
501 } else {
502 /* add request to the list for later matching */
503 add_request(data, &ctx->hdr);
507 /* get command list by network function code */
508 cmd_list = ipmi_getnetfn(ctx->hdr.netfn,
509 tvb_get_ptr(tvb, ctx->hdr_len + is_resp, siglen));
511 /* get command descriptor */
512 cmd = ipmi_getcmd(cmd_list, ctx->hdr.cmd);
514 /* check if response */
515 if (is_resp) {
516 /* get completion code */
517 cc_val = tvb_get_uint8(tvb, ctx->hdr_len);
519 /* get completion code desc */
520 cc_str = ipmi_get_completion_code(cc_val, cmd);
521 } else {
522 cc_val = 0;
523 cc_str = NULL;
526 /* check if not inside a message */
527 if (!data->curr_level) {
528 /* add packet info */
529 add_command_info(pinfo, cmd, is_resp, cc_val, cc_str,
530 ctx->flags & IPMI_D_BROADCAST ? true : false);
533 if (tree) {
534 /* add parent node */
535 if (!data->curr_level) {
536 ti = proto_tree_add_item(tree, hf_parent_item, tvb, 0, -1, ENC_NA);
537 cmd_tree = proto_item_add_subtree(ti, ett_tree);
538 } else {
539 char str[ITEM_LABEL_LENGTH];
541 if (is_resp) {
542 snprintf(str, ITEM_LABEL_LENGTH, "Rsp, %s, %s",
543 cmd->desc, cc_str);
544 } else {
545 snprintf(str, ITEM_LABEL_LENGTH, "Req, %s", cmd->desc);
547 if (proto_registrar_get_ftype(hf_parent_item) == FT_STRING) {
548 ti = proto_tree_add_string(tree, hf_parent_item, tvb, 0, -1, str);
549 cmd_tree = proto_item_add_subtree(ti, ett_tree);
551 else
552 cmd_tree = proto_tree_add_subtree(tree, tvb, 0, -1, ett_tree, NULL, str);
555 if (data->curr_level < MAX_NEST_LEVEL) {
556 /* check if response */
557 if (ctx->hdr.netfn & 1) {
558 /* get current command data */
559 ipmi_cmd_data_t * rs_data =
560 data->curr_frame->cmd_data[data->curr_level];
562 if (rs_data->matched_frame_num) {
563 nstime_t ns;
565 /* add "Request to:" field */
566 ti = proto_tree_add_uint(cmd_tree, hf_ipmi_response_to,
567 tvb, 0, 0, rs_data->matched_frame_num);
569 /* mark field as a generated one */
570 proto_item_set_generated(ti);
572 /* calculate delta time */
573 nstime_delta(&ns, &pinfo->abs_ts,
574 &get_frame_data(data,
575 rs_data->matched_frame_num)->ts);
577 /* add "Response time" field */
578 ti = proto_tree_add_time(cmd_tree, hf_ipmi_response_time,
579 tvb, 0, 0, &ns);
581 /* mark field as a generated one */
582 proto_item_set_generated(ti);
584 } else {
585 /* get current command data */
586 ipmi_cmd_data_t * rq_data =
587 data->curr_frame->cmd_data[data->curr_level];
589 if (rq_data->matched_frame_num) {
590 /* add "Response in:" field */
591 ti = proto_tree_add_uint(cmd_tree, hf_ipmi_response_in,
592 tvb, 0, 0, rq_data->matched_frame_num);
594 /* mark field as a generated one */
595 proto_item_set_generated(ti);
600 /* set starting offset */
601 offset = 0;
603 /* check if message is broadcast */
604 if (ctx->flags & IPMI_D_BROADCAST) {
605 /* skip first byte */
606 offset++;
609 /* check if session handle is specified */
610 if (ctx->flags & IPMI_D_SESSION_HANDLE) {
611 /* add session handle field */
612 proto_tree_add_item(cmd_tree, hf_ipmi_session_handle,
613 tvb, offset++, 1, ENC_LITTLE_ENDIAN);
616 /* check if responder address is specified */
617 if (ctx->flags & IPMI_D_TRG_SA) {
618 /* add response address field */
619 proto_tree_add_item(cmd_tree, hf_ipmi_header_trg, tvb,
620 offset++, 1, ENC_LITTLE_ENDIAN);
623 /* get NetFn string */
624 netfn_str = ipmi_getnetfnname(pinfo->pool, ctx->hdr.netfn, cmd_list);
626 /* Network function + target LUN */
627 tmp_tree = proto_tree_add_subtree_format(cmd_tree, tvb, offset, 1,
628 ett_header_byte_1, NULL, "Target LUN: 0x%02x, NetFN: %s %s (0x%02x)",
629 ctx->hdr.rs_lun, netfn_str,
630 is_resp ? "Response" : "Request", ctx->hdr.netfn);
632 /* add Net Fn */
633 proto_tree_add_uint_format(tmp_tree, hf_ipmi_header_netfn, tvb,
634 offset, 1, ctx->hdr.netfn << 2,
635 "NetFn: %s %s (0x%02x)", netfn_str,
636 is_resp ? "Response" : "Request", ctx->hdr.netfn);
638 proto_tree_add_item(tmp_tree, hf_ipmi_header_trg_lun, tvb,
639 offset++, 1, ENC_LITTLE_ENDIAN);
641 /* check if cks1 is specified */
642 if (!(ctx->flags & IPMI_D_NO_CKS)) {
643 uint8_t cks = tvb_get_uint8(tvb, offset);
645 /* Header checksum */
646 if (ctx->cks1) {
647 uint8_t correct = cks - ctx->cks1;
649 proto_tree_add_uint_format_value(cmd_tree, hf_ipmi_header_crc,
650 tvb, offset++, 1, cks,
651 "0x%02x (incorrect, expected 0x%02x)", cks, correct);
652 } else {
653 proto_tree_add_uint_format_value(cmd_tree, hf_ipmi_header_crc,
654 tvb, offset++, 1, cks,
655 "0x%02x (correct)", cks);
659 /* check if request address is specified */
660 if (!(ctx->flags & IPMI_D_NO_RQ_SA)) {
661 /* add request address field */
662 proto_tree_add_item(cmd_tree, hf_ipmi_header_src, tvb,
663 offset++, 1, ENC_LITTLE_ENDIAN);
666 /* check if request sequence is specified */
667 if (!(ctx->flags & IPMI_D_NO_SEQ)) {
668 /* Sequence number + source LUN */
669 tmp_tree = proto_tree_add_subtree_format(cmd_tree, tvb, offset, 1,
670 ett_header_byte_4, NULL, "%s: 0x%02x, SeqNo: 0x%02x",
671 (ctx->flags & IPMI_D_TMODE) ? "Bridged" : "Source LUN",
672 ctx->hdr.rq_lun, ctx->hdr.rq_seq);
674 if (ctx->flags & IPMI_D_TMODE) {
675 proto_tree_add_item(tmp_tree, hf_ipmi_header_bridged,
676 tvb, offset, 1, ENC_LITTLE_ENDIAN);
677 } else {
678 proto_tree_add_item(tmp_tree, hf_ipmi_header_src_lun,
679 tvb, offset, 1, ENC_LITTLE_ENDIAN);
682 /* print seq no */
683 proto_tree_add_item(tmp_tree, hf_ipmi_header_sequence, tvb,
684 offset++, 1, ENC_LITTLE_ENDIAN);
687 /* command code */
688 proto_tree_add_uint_format_value(cmd_tree, hf_ipmi_header_command,
689 tvb, offset++, 1, ctx->hdr.cmd, "%s (0x%02x)",
690 cmd->desc, ctx->hdr.cmd);
692 if (is_resp) {
693 /* completion code */
694 proto_tree_add_uint_format_value(cmd_tree,
695 hf_ipmi_header_completion, tvb, offset++, 1,
696 cc_val, "%s (0x%02x)", cc_str, cc_val);
699 if (siglen) {
700 /* command prefix (if present) */
701 ti = proto_tree_add_item(cmd_tree, hf_ipmi_header_sig, tvb,
702 offset, siglen, ENC_NA);
703 proto_item_append_text(ti, " (%s)", netfn_str);
707 if (tree || (cmd->flags & CMD_CALLRQ)) {
708 /* calculate message data length */
709 unsigned data_len = tvb_captured_length(tvb)
710 - ctx->hdr_len
711 - siglen
712 - (is_resp ? 1 : 0)
713 - !(ctx->flags & IPMI_D_NO_CKS);
715 /* create data subset */
716 tvbuff_t * data_tvb = tvb_new_subset_length(tvb,
717 ctx->hdr_len + siglen + (is_resp ? 1 : 0), data_len);
719 /* Select sub-handler */
720 ipmi_cmd_handler_t hnd = is_resp ? cmd->parse_resp : cmd->parse_req;
722 if (hnd && tvb_captured_length(data_tvb)) {
723 /* create data field */
724 tmp_tree = proto_tree_add_subtree(cmd_tree, data_tvb, 0, -1, ett_data, NULL, "Data");
726 /* save current command */
727 data->curr_hdr = &ctx->hdr;
729 /* save current completion code */
730 data->curr_ccode = cc_val;
732 /* call command parser */
733 hnd(data_tvb, pinfo, tmp_tree);
737 /* check if cks2 is specified */
738 if (tree && !(ctx->flags & IPMI_D_NO_CKS)) {
739 uint8_t cks;
741 /* get cks2 offset */
742 offset = tvb_captured_length(tvb) - 1;
744 /* get cks2 */
745 cks = tvb_get_uint8(tvb, offset);
747 /* Header checksum */
748 if (ctx->cks2) {
749 uint8_t correct = cks - ctx->cks2;
751 proto_tree_add_uint_format_value(cmd_tree, hf_ipmi_data_crc,
752 tvb, offset, 1, cks,
753 "0x%02x (incorrect, expected 0x%02x)", cks, correct);
754 } else {
755 proto_tree_add_uint_format_value(cmd_tree, hf_ipmi_data_crc,
756 tvb, offset, 1, cks,
757 "0x%02x (correct)", cks);
761 /* decrement next nest level */
762 data->next_level = data->curr_level;
764 /* restore previous nest level */
765 data->curr_level = prev_level;
767 return tvb_captured_length(tvb);
770 /* Get currently parsed message header */
771 const ipmi_header_t * ipmi_get_hdr(packet_info * pinfo)
773 ipmi_packet_data_t * data = get_packet_data(pinfo);
774 return data->curr_hdr;
777 /* Get completion code for currently parsed message */
778 uint8_t ipmi_get_ccode(packet_info * pinfo)
780 ipmi_packet_data_t * data = get_packet_data(pinfo);
781 return data->curr_ccode;
784 /* Save request data for later use in response */
785 void ipmi_set_data(packet_info *pinfo, unsigned idx, uint32_t value)
787 ipmi_packet_data_t * data = get_packet_data(pinfo);
789 /* check bounds */
790 if (data->curr_level >= MAX_NEST_LEVEL || idx >= NSAVED_DATA || !data->curr_frame ) {
791 return;
794 /* save data */
795 data->curr_frame->cmd_data[data->curr_level]->saved_data[idx] = value;
798 /* Get saved request data */
799 bool ipmi_get_data(packet_info *pinfo, unsigned idx, uint32_t * value)
801 ipmi_packet_data_t * data = get_packet_data(pinfo);
803 /* check bounds */
804 if (data->curr_level >= MAX_NEST_LEVEL || idx >= NSAVED_DATA || !data->curr_frame ) {
805 return false;
808 /* get data */
809 *value = data->curr_frame->cmd_data[data->curr_level]->saved_data[idx];
810 return true;
813 /* ----------------------------------------------------------------
814 Support for Type/Length fields parsing.
815 ---------------------------------------------------------------- */
817 static void
818 get_len_binary(unsigned *clen, unsigned *blen, tvbuff_t *tvb _U_, unsigned offs _U_,
819 unsigned len, bool len_is_bytes _U_)
821 *clen = len * 3;
822 *blen = len;
825 static void
826 parse_binary(char *p, tvbuff_t *tvb, unsigned offs, unsigned len)
828 static const char hex[] = "0123456789ABCDEF";
829 uint8_t v;
830 unsigned i;
832 for (i = 0; i < len / 3; i++) {
833 v = tvb_get_uint8(tvb, offs + i);
834 *p++ = hex[v >> 4];
835 *p++ = hex[v & 0xf];
836 *p++ = ' ';
839 if (i) {
840 *--p = '\0';
844 static struct ipmi_parse_typelen ptl_binary = {
845 get_len_binary, parse_binary, "Binary"
848 static void
849 get_len_bcdplus(unsigned *clen, unsigned *blen, tvbuff_t *tvb _U_, unsigned offs _U_,
850 unsigned len, bool len_is_bytes)
852 if (len_is_bytes) {
853 *clen = len * 2;
854 *blen = len;
855 } else {
856 *blen = (len + 1) / 2;
857 *clen = len;
861 static void
862 parse_bcdplus(char *p, tvbuff_t *tvb, unsigned offs, unsigned len)
864 static const char bcd[] = "0123456789 -.:,_";
865 unsigned i, msk = 0xf0, shft = 4;
866 uint8_t v;
868 for (i = 0; i < len; i++) {
869 v = (tvb_get_uint8(tvb, offs + i / 2) & msk) >> shft;
870 *p++ = bcd[v];
871 msk ^= 0xff;
872 shft = 4 - shft;
876 static struct ipmi_parse_typelen ptl_bcdplus = {
877 get_len_bcdplus, parse_bcdplus, "BCD+"
880 static void
881 get_len_6bit_ascii(unsigned *clen, unsigned *blen, tvbuff_t *tvb _U_, unsigned offs _U_,
882 unsigned len, bool len_is_bytes)
884 if (len_is_bytes) {
885 *clen = len * 4 / 3;
886 *blen = len;
887 } else {
888 *blen = (len * 3 + 3) / 4;
889 *clen = len;
893 static void
894 parse_6bit_ascii(char *p, tvbuff_t *tvb, unsigned offs, unsigned len)
896 uint32_t v;
897 unsigned i;
899 /* First, handle "full" triplets of bytes, 4 characters each */
900 for (i = 0; i < len / 4; i++) {
901 v = tvb_get_letoh24(tvb, offs + i * 3);
902 p[0] = ' ' + (v & 0x3f);
903 p[1] = ' ' + ((v >> 6) & 0x3f);
904 p[2] = ' ' + ((v >> 12) & 0x3f);
905 p[3] = ' ' + ((v >> 18) & 0x3f);
906 p += 4;
909 /* Do we have any characters left? */
910 offs += len / 4;
911 len &= 0x3;
912 switch (len) {
913 case 3:
914 v = (tvb_get_uint8(tvb, offs + 2) << 4) | (tvb_get_uint8(tvb, offs + 1) >> 4);
915 p[2] = ' ' + (v & 0x3f);
916 /* Fall thru */
917 case 2:
918 v = (tvb_get_uint8(tvb, offs + 1) << 2) | (tvb_get_uint8(tvb, offs) >> 6);
919 p[1] = ' ' + (v & 0x3f);
920 /* Fall thru */
921 case 1:
922 v = tvb_get_uint8(tvb, offs) & 0x3f;
923 p[0] = ' ' + (v & 0x3f);
927 static struct ipmi_parse_typelen ptl_6bit_ascii = {
928 get_len_6bit_ascii, parse_6bit_ascii, "6-bit ASCII"
931 static void
932 get_len_8bit_ascii(unsigned *clen, unsigned *blen, tvbuff_t *tvb, unsigned offs,
933 unsigned len, bool len_is_bytes _U_)
935 unsigned i;
936 uint8_t ch;
938 *blen = len; /* One byte is one character */
939 *clen = 0;
940 for (i = 0; i < len; i++) {
941 ch = tvb_get_uint8(tvb, offs + i);
942 *clen += (ch >= 0x20 && ch <= 0x7f) ? 1 : 4;
946 static void
947 parse_8bit_ascii(char *p, tvbuff_t *tvb, unsigned offs, unsigned len)
949 uint8_t ch;
950 char *pmax;
952 pmax = p + len;
953 while (p < pmax) {
954 ch = tvb_get_uint8(tvb, offs++);
955 if (ch >= 0x20 && ch <= 0x7f) {
956 *p++ = ch;
957 } else {
958 snprintf(p, 5, "\\x%02x", ch);
959 p += 4;
964 static struct ipmi_parse_typelen ptl_8bit_ascii = {
965 get_len_8bit_ascii, parse_8bit_ascii, "ASCII+Latin1"
968 static void
969 get_len_unicode(unsigned *clen, unsigned *blen, tvbuff_t *tvb _U_, unsigned offs _U_,
970 unsigned len _U_, bool len_is_bytes)
972 if (len_is_bytes) {
973 *clen = len * 3; /* Each 2 bytes result in 6 chars printed: \Uxxxx */
974 *blen = len;
975 } else {
976 *clen = len * 6;
977 *blen = len * 2;
981 static void
982 parse_unicode(char *p, tvbuff_t *tvb, unsigned offs, unsigned len)
984 char *pmax = p + len;
985 uint8_t ch0, ch1;
987 while (p < pmax) {
988 ch0 = tvb_get_uint8(tvb, offs++);
989 ch1 = tvb_get_uint8(tvb, offs++);
990 snprintf(p, 7, "\\U%02x%02x", ch0, ch1);
991 p += 6;
995 static struct ipmi_parse_typelen ptl_unicode = {
996 get_len_unicode, parse_unicode, "Unicode"
999 void
1000 ipmi_add_typelen(packet_info *pinfo, proto_tree *tree, int hf_string, int hf_type, int hf_length, tvbuff_t *tvb,
1001 unsigned offs, bool is_fru)
1003 static struct ipmi_parse_typelen *fru_eng[4] = {
1004 &ptl_binary, &ptl_bcdplus, &ptl_6bit_ascii, &ptl_8bit_ascii
1006 static struct ipmi_parse_typelen *fru_noneng[4] = {
1007 &ptl_binary, &ptl_bcdplus, &ptl_6bit_ascii, &ptl_unicode
1009 static struct ipmi_parse_typelen *ipmi[4] = {
1010 &ptl_unicode, &ptl_bcdplus, &ptl_6bit_ascii, &ptl_8bit_ascii
1012 struct ipmi_parse_typelen *ptr;
1013 proto_tree *s_tree;
1014 unsigned type, msk, clen, blen, len;
1015 const char *unit;
1016 char *str;
1017 uint8_t typelen;
1019 typelen = tvb_get_uint8(tvb, offs);
1020 type = typelen >> 6;
1021 if (is_fru) {
1022 msk = 0x3f;
1023 ptr = (fru_langcode_is_english ? fru_eng : fru_noneng)[type];
1024 unit = "bytes";
1025 } else {
1026 msk = 0x1f;
1027 ptr = ipmi[type];
1028 unit = "characters";
1031 len = typelen & msk;
1032 ptr->get_len(&clen, &blen, tvb, offs + 1, len, is_fru);
1034 str = (char *)wmem_alloc(pinfo->pool, clen + 1);
1035 ptr->parse(str, tvb, offs + 1, clen);
1036 str[clen] = '\0';
1038 s_tree = proto_tree_add_subtree_format(tree, tvb, offs, 1, ett_typelen, NULL,
1039 "%s Type/Length byte: %s, %d %s", (proto_registrar_get_nth(hf_string))->name, ptr->desc, len, unit);
1040 proto_tree_add_uint_format_value(s_tree, hf_type, tvb, offs, 1, type, "%s (0x%02x)",
1041 ptr->desc, type);
1042 proto_tree_add_uint_format_value(s_tree, hf_length, tvb, offs, 1, len, "%d %s",
1043 len, unit);
1045 proto_tree_add_string_format_value(tree, hf_string, tvb, offs + 1, blen, str,
1046 "[%s] '%s'", ptr->desc, str);
1049 /* ----------------------------------------------------------------
1050 Timestamp, IPMI-style.
1051 ---------------------------------------------------------------- */
1052 void
1053 ipmi_add_timestamp(packet_info *pinfo, proto_tree *tree, int hf, tvbuff_t *tvb, unsigned offset)
1055 uint32_t ts = tvb_get_letohl(tvb, offset);
1057 if (ts == 0xffffffff) {
1058 proto_tree_add_uint_format_value(tree, hf, tvb, offset, 4,
1059 ts, "Unspecified/Invalid");
1060 } else if (ts <= 0x20000000) {
1061 proto_tree_add_uint_format_value(tree, hf, tvb, offset, 4,
1062 ts, "%s since SEL device's initialization",
1063 unsigned_time_secs_to_str(pinfo->pool, ts));
1064 } else {
1065 proto_tree_add_uint_format_value(tree, hf, tvb, offset, 4,
1066 ts, "%s", abs_time_secs_to_str(pinfo->pool, ts, ABSOLUTE_TIME_UTC, true));
1070 /* ----------------------------------------------------------------
1071 GUID, IPMI-style.
1072 ---------------------------------------------------------------- */
1074 void
1075 ipmi_add_guid(proto_tree *tree, int hf, tvbuff_t *tvb, unsigned offset)
1077 e_guid_t guid;
1078 int i;
1080 guid.data1 = tvb_get_letohl(tvb, offset + 12);
1081 guid.data2 = tvb_get_letohs(tvb, offset + 10);
1082 guid.data3 = tvb_get_letohs(tvb, offset + 8);
1083 for (i = 0; i < 8; i++) {
1084 guid.data4[i] = tvb_get_uint8(tvb, offset + 7 - i);
1086 proto_tree_add_guid(tree, hf, tvb, offset, 16, &guid);
1089 /* ----------------------------------------------------------------
1090 Routines for registering/looking up command parsers.
1091 ---------------------------------------------------------------- */
1093 static void
1094 ipmi_netfn_setdesc(uint32_t netfn, const char *desc, uint32_t siglen)
1096 struct ipmi_netfn_root *inr;
1098 inr = &ipmi_cmd_tab[netfn >> 1];
1099 inr->desc = desc;
1100 inr->siglen = siglen;
1103 void
1104 ipmi_register_netfn_cmdtab(uint32_t netfn, unsigned oem_selector,
1105 const uint8_t *sig, uint32_t siglen, const char *desc,
1106 const ipmi_cmd_t *cmdtab, uint32_t cmdtablen)
1108 struct ipmi_netfn_root *inr;
1109 ipmi_netfn_t *inh;
1111 netfn >>= 1; /* Requests and responses grouped together */
1112 if (netfn >= IPMI_NETFN_MAX) {
1113 return;
1116 inr = &ipmi_cmd_tab[netfn];
1117 if (inr->siglen != siglen) {
1118 return;
1121 inh = wmem_new(wmem_epan_scope(), struct ipmi_netfn_handler);
1122 inh->desc = desc;
1123 inh->oem_selector = oem_selector;
1124 inh->sig = sig;
1125 inh->cmdtab = cmdtab;
1126 inh->cmdtablen = cmdtablen;
1128 inh->next = inr->list;
1129 inr->list = inh;
1132 uint32_t
1133 ipmi_getsiglen(uint32_t netfn)
1135 return ipmi_cmd_tab[netfn >> 1].siglen;
1138 const char *
1139 ipmi_getnetfnname(wmem_allocator_t *pool, uint32_t netfn, ipmi_netfn_t *nf)
1141 const char *dn, *db;
1143 dn = ipmi_cmd_tab[netfn >> 1].desc ?
1144 ipmi_cmd_tab[netfn >> 1].desc : "Reserved";
1145 db = nf ? nf->desc : NULL;
1146 if (db) {
1147 return wmem_strdup_printf(pool, "%s (%s)", db, dn);
1148 } else {
1149 return dn;
1153 ipmi_netfn_t *
1154 ipmi_getnetfn(uint32_t netfn, const uint8_t *sig)
1156 struct ipmi_netfn_root *inr;
1157 ipmi_netfn_t *inh;
1159 inr = &ipmi_cmd_tab[netfn >> 1];
1160 for (inh = inr->list; inh; inh = inh->next) {
1161 if ((inh->oem_selector == selected_oem || inh->oem_selector == IPMI_OEM_NONE)
1162 && (!inr->siglen || !memcmp(sig, inh->sig, inr->siglen))) {
1163 return inh;
1167 /* Either unknown netFn or signature does not match */
1168 return NULL;
1171 const ipmi_cmd_t *
1172 ipmi_getcmd(ipmi_netfn_t *nf, uint32_t cmd)
1174 static const ipmi_cmd_t ipmi_cmd_unknown = {
1175 0x00, /* Code */
1176 ipmi_notimpl, /* request */
1177 ipmi_notimpl, /* response */
1178 NULL, /* command codes */
1179 NULL, /* subfunctions */
1180 "Unknown command",
1181 0 /* flag */
1183 const ipmi_cmd_t *ic;
1184 size_t i, len;
1186 if (nf) {
1187 len = nf->cmdtablen;
1188 for (ic = nf->cmdtab, i = 0; i < len; i++, ic++) {
1189 if (ic->cmd == cmd) {
1190 return ic;
1195 return &ipmi_cmd_unknown;
1198 /* ----------------------------------------------------------------
1199 Various utility functions.
1200 ---------------------------------------------------------------- */
1202 void
1203 ipmi_notimpl(tvbuff_t *tvb, packet_info *pinfo _U_, proto_tree *tree)
1205 proto_tree_add_expert(tree, pinfo, &ei_impi_parser_not_implemented, tvb, 0, -1);
1208 void
1209 ipmi_fmt_10ms_1based(char *s, uint32_t v)
1211 snprintf(s, ITEM_LABEL_LENGTH, "%d.%03d seconds", v / 100, (v % 100) * 10);
1214 void
1215 ipmi_fmt_500ms_0based(char *s, uint32_t v)
1217 ipmi_fmt_500ms_1based(s, ++v);
1220 void
1221 ipmi_fmt_500ms_1based(char *s, uint32_t v)
1223 snprintf(s, ITEM_LABEL_LENGTH, "%d.%03d seconds", v / 2, (v % 2) * 500);
1226 void
1227 ipmi_fmt_1s_0based(char *s, uint32_t v)
1229 ipmi_fmt_1s_1based(s, ++v);
1232 void
1233 ipmi_fmt_1s_1based(char *s, uint32_t v)
1235 snprintf(s, ITEM_LABEL_LENGTH, "%d seconds", v);
1238 void
1239 ipmi_fmt_2s_0based(char *s, uint32_t v)
1241 snprintf(s, ITEM_LABEL_LENGTH, "%d seconds", (v + 1) * 2);
1244 void
1245 ipmi_fmt_5s_1based(char *s, uint32_t v)
1247 snprintf(s, ITEM_LABEL_LENGTH, "%d seconds", v * 5);
1250 void
1251 ipmi_fmt_version(char *s, uint32_t v)
1253 snprintf(s, ITEM_LABEL_LENGTH, "%d.%d", v & 0x0f, (v >> 4) & 0x0f);
1256 void
1257 ipmi_fmt_channel(char *s, uint32_t v)
1259 static const value_string chan_vals[] = {
1260 { 0x00, "Primary IPMB (IPMB-0)" },
1261 { 0x07, "IPMB-L" },
1262 { 0x0e, "Current channel" },
1263 { 0x0f, "System Interface" },
1264 { 0, NULL }
1266 char* tmp_str;
1268 tmp_str = val_to_str_wmem(NULL, v, chan_vals, "Channel #%d");
1269 snprintf(s, ITEM_LABEL_LENGTH, "%s (0x%02x)", tmp_str, v);
1270 wmem_free(NULL, tmp_str);
1273 void
1274 ipmi_fmt_udpport(char *s, uint32_t v)
1276 char* port_str = udp_port_to_display(NULL, v);
1277 snprintf(s, ITEM_LABEL_LENGTH, "%s (%d)", port_str, v);
1278 wmem_free(NULL, port_str);
1281 void
1282 ipmi_fmt_percent(char *s, uint32_t v)
1284 snprintf(s, ITEM_LABEL_LENGTH, "%d%%", v);
1287 const char *
1288 ipmi_get_completion_code(uint8_t completion, const ipmi_cmd_t *cmd)
1290 static const value_string std_completion_codes[] = {
1291 { 0x00, "Command Completed Normally" },
1292 { 0xc0, "Node Busy" },
1293 { 0xc1, "Invalid Command" },
1294 { 0xc2, "Command invalid for given LUN" },
1295 { 0xc3, "Timeout while processing command, response unavailable" },
1296 { 0xc4, "Out of space" },
1297 { 0xc5, "Reservation Canceled or Invalid Reservation ID" },
1298 { 0xc6, "Request data truncated" },
1299 { 0xc7, "Request data length invalid" },
1300 { 0xc8, "Request data field length limit exceeded" },
1301 { 0xc9, "Parameter out of range" },
1302 { 0xca, "Cannot return number of requested data bytes" },
1303 { 0xcb, "Requested Sensor, data, or record not present" },
1304 { 0xcc, "Invalid data field in Request" },
1305 { 0xcd, "Command illegal for specified sensor or record type" },
1306 { 0xce, "Command response could not be provided" },
1307 { 0xcf, "Cannot execute duplicated request" },
1308 { 0xd0, "Command response could not be provided: SDR Repository in update mode" },
1309 { 0xd1, "Command response could not be provided: device in firmware update mode" },
1310 { 0xd2, "Command response could not be provided: BMC initialization or initialization agent in progress" },
1311 { 0xd3, "Destination unavailable" },
1312 { 0xd4, "Cannot execute command: insufficient privilege level or other security-based restriction" },
1313 { 0xd5, "Cannot execute command: command, or request parameter(s), not supported in present state" },
1314 { 0xd6, "Cannot execute command: parameter is illegal because subfunction is disabled or unavailable" },
1315 { 0xff, "Unspecified error" },
1317 { 0, NULL }
1319 const char *res;
1321 if (completion >= 0x01 && completion <= 0x7e) {
1322 return "Device specific (OEM) completion code";
1325 if (completion >= 0x80 && completion <= 0xbe) {
1326 if (cmd && cmd->cs_cc && (res = try_val_to_str(completion, cmd->cs_cc)) != NULL) {
1327 return res;
1329 return "Standard command-specific code";
1332 return val_to_str_const(completion, std_completion_codes, "Unknown");
1335 static int
1336 dissect_tmode(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
1338 ipmi_dissect_arg_t * arg = (ipmi_dissect_arg_t *) data;
1339 ipmi_context_t ctx;
1340 unsigned tvb_len = tvb_captured_length(tvb);
1341 uint8_t tmp;
1343 /* TMode message is at least 3 bytes length */
1344 if (tvb_len < 3) {
1345 return 0;
1348 memset(&ctx, 0, sizeof(ctx));
1350 /* get Net Fn/RS LUN field */
1351 tmp = tvb_get_uint8(tvb, 0);
1353 /* set Net Fn */
1354 ctx.hdr.netfn = tmp >> 2;
1357 * NOTE: request/response matching code swaps RQ LUN with RS LUN
1358 * fields in IPMB-like manner in order to find corresponding request
1359 * so, we set both RS LUN and RQ LUN here for correct
1360 * request/response matching
1362 ctx.hdr.rq_lun = tmp & 3;
1363 ctx.hdr.rs_lun = tmp & 3;
1365 /* get RQ Seq field */
1366 ctx.hdr.rq_seq = tvb_get_uint8(tvb, 1) >> 2;
1369 * NOTE: bridge field is ignored in request/response matching
1372 /* get command code */
1373 ctx.hdr.cmd = tvb_get_uint8(tvb, 2);
1375 /* set dissect flags */
1376 ctx.flags = IPMI_D_TMODE|IPMI_D_NO_CKS|IPMI_D_NO_RQ_SA;
1378 /* set header length */
1379 ctx.hdr_len = 3;
1381 /* copy channel number and direction */
1382 ctx.hdr.context = arg ? arg->context : IPMI_E_NONE;
1383 ctx.hdr.channel = arg ? arg->channel : 0;
1384 ctx.hdr.dir = arg ? arg->flags >> 7 : ctx.hdr.netfn & 1;
1386 if (ctx.hdr.context == IPMI_E_NONE) {
1387 /* set source column */
1388 col_set_str(pinfo->cinfo, COL_DEF_SRC,
1389 ctx.hdr.dir ? "Console" : "BMC");
1391 /* set destination column */
1392 col_set_str(pinfo->cinfo, COL_DEF_DST,
1393 ctx.hdr.dir ? "BMC" : "Console");
1396 /* dissect IPMI command */
1397 return dissect_ipmi_cmd(tvb, pinfo, tree, proto_tmode, ett_ipmi, &ctx);
1400 static int
1401 dissect_kcs(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
1403 ipmi_dissect_arg_t * arg = (ipmi_dissect_arg_t *) data;
1404 ipmi_context_t ctx;
1405 unsigned tvb_len = tvb_captured_length(tvb);
1406 uint8_t tmp;
1408 /* KCS message is at least 2 bytes length */
1409 if (tvb_len < 2) {
1410 return 0;
1413 memset(&ctx, 0, sizeof(ctx));
1415 /* get Net Fn/RS LUN field */
1416 tmp = tvb_get_uint8(tvb, 0);
1418 /* set Net Fn */
1419 ctx.hdr.netfn = tmp >> 2;
1422 * NOTE: request/response matching code swaps RQ LUN with RS LUN
1423 * fields in IPMB-like manner in order to find corresponding request
1424 * so, we set both RS LUN and RQ LUN here for correct
1425 * request/response matching
1427 ctx.hdr.rq_lun = tmp & 3;
1428 ctx.hdr.rs_lun = tmp & 3;
1430 /* get command code */
1431 ctx.hdr.cmd = tvb_get_uint8(tvb, 1);
1433 /* set dissect flags */
1434 ctx.flags = IPMI_D_NO_CKS|IPMI_D_NO_RQ_SA|IPMI_D_NO_SEQ;
1436 /* set header length */
1437 ctx.hdr_len = 2;
1439 /* copy channel number and direction */
1440 ctx.hdr.context = arg ? arg->context : 0;
1441 ctx.hdr.channel = arg ? arg->channel : 0;
1442 ctx.hdr.dir = arg ? arg->flags >> 7 : ctx.hdr.netfn & 1;
1444 if (ctx.hdr.context == IPMI_E_NONE) {
1445 /* set source column */
1446 col_set_str(pinfo->cinfo, COL_DEF_SRC, ctx.hdr.dir ? "HOST" : "BMC");
1448 /* set destination column */
1449 col_set_str(pinfo->cinfo, COL_DEF_DST, ctx.hdr.dir ? "BMC" : "HOST");
1452 /* dissect IPMI command */
1453 return dissect_ipmi_cmd(tvb, pinfo, tree, proto_kcs, ett_ipmi, &ctx);
1456 static uint8_t calc_cks(uint8_t start, tvbuff_t * tvb, unsigned off, unsigned len)
1458 while (len--) {
1459 start += tvb_get_uint8(tvb, off++);
1462 return start;
1465 static bool guess_imb_format(tvbuff_t *tvb, uint8_t env,
1466 uint8_t channel, unsigned * imb_flags, uint8_t * cks1, uint8_t * cks2)
1468 bool check_bc = false;
1469 bool check_sh = false;
1470 bool check_sa = false;
1471 unsigned tvb_len;
1472 unsigned sh_len;
1473 unsigned sa_len;
1474 unsigned rs_sa;
1476 if (message_format == MSGFMT_NONE) {
1477 return false;
1478 } else if (message_format == MSGFMT_IPMB) {
1479 *imb_flags = IPMI_D_TRG_SA;
1480 } else if (message_format == MSGFMT_LAN) {
1481 *imb_flags = IPMI_D_TRG_SA|IPMI_D_SESSION_HANDLE;
1482 /* channel 0 is primary IPMB */
1483 } else if (!channel) {
1484 /* check for broadcast if not in send message command */
1485 if (env == IPMI_E_NONE) {
1486 /* check broadcast */
1487 check_bc = 1;
1489 /* slave address must be present */
1490 *imb_flags = IPMI_D_TRG_SA;
1491 /* check if in send message command */
1492 } else if (env != IPMI_E_GETMSG) {
1493 /* slave address must be present */
1494 *imb_flags = IPMI_D_TRG_SA;
1495 } else /* IPMI_E_GETMSG */ {
1496 *imb_flags = 0;
1498 /* channel 15 is System Interface */
1499 } else if (channel == 15) {
1500 /* slave address must be present */
1501 *imb_flags = IPMI_D_TRG_SA;
1503 /* check if in get message command */
1504 if (env == IPMI_E_GETMSG) {
1505 /* session handle must be present */
1506 *imb_flags |= IPMI_D_SESSION_HANDLE;
1508 /* for other channels */
1509 } else {
1510 if (env == IPMI_E_NONE) {
1511 /* check broadcast */
1512 check_bc = 1;
1514 /* slave address must be present */
1515 *imb_flags = IPMI_D_TRG_SA;
1516 } else if (env == IPMI_E_SENDMSG_RQ) {
1517 /* check session handle */
1518 check_sh = 1;
1520 /* slave address must be present */
1521 *imb_flags = IPMI_D_TRG_SA;
1522 } else if (env == IPMI_E_SENDMSG_RS) {
1523 /* slave address must be present */
1524 *imb_flags = IPMI_D_TRG_SA;
1525 } else /* IPMI_E_GETMSG */ {
1526 /* check session handle */
1527 check_sh = 1;
1529 /* check slave address presence */
1530 check_sa = 1;
1532 /* no pre-requisites */
1533 *imb_flags = 0;
1537 /* get message length */
1538 tvb_len = tvb_captured_length(tvb);
1541 * broadcast message starts with null,
1542 * does not contain session handle
1543 * but contains responder address
1545 if (check_bc
1546 && tvb_len >= 8
1547 && !tvb_get_uint8(tvb, 0)
1548 && !calc_cks(0, tvb, 1, 3)
1549 && !calc_cks(0, tvb, 4, tvb_len - 4)) {
1550 *imb_flags = IPMI_D_BROADCAST|IPMI_D_TRG_SA;
1551 *cks1 = 0;
1552 *cks2 = 0;
1553 return true;
1557 * message with the starts with session handle
1558 * and contain responder address
1560 if (check_sh
1561 && tvb_len >= 8
1562 && !calc_cks(0, tvb, 1, 3)
1563 && !calc_cks(0, tvb, 4, tvb_len - 4)) {
1564 *imb_flags = IPMI_D_SESSION_HANDLE|IPMI_D_TRG_SA;
1565 *cks1 = 0;
1566 *cks2 = 0;
1567 return true;
1571 * message with responder address
1573 if (check_sa
1574 && tvb_len >= 7
1575 && !calc_cks(0, tvb, 0, 3)
1576 && !calc_cks(0, tvb, 3, tvb_len - 3)) {
1577 *imb_flags = IPMI_D_TRG_SA;
1578 *cks1 = 0;
1579 *cks2 = 0;
1580 return true;
1584 if (*imb_flags & IPMI_D_SESSION_HANDLE) {
1585 sh_len = 1;
1586 sa_len = 1;
1587 rs_sa = 0;
1588 } else if (*imb_flags & IPMI_D_TRG_SA) {
1589 sh_len = 0;
1590 sa_len = 1;
1591 rs_sa = 0;
1592 } else {
1593 sh_len = 0;
1594 sa_len = 0;
1595 rs_sa = 0x20;
1598 /* check message length */
1599 if (tvb_len < 6 + sh_len + sa_len) {
1600 return false;
1603 /* calculate checksum deltas */
1604 *cks1 = calc_cks(rs_sa, tvb, sh_len, sa_len + 2);
1605 *cks2 = calc_cks(0, tvb, sh_len + sa_len + 2,
1606 tvb_len - sh_len - sa_len - 2);
1608 return true;
1612 do_dissect_ipmb(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
1613 int hf_parent_item, int ett_tree, ipmi_dissect_arg_t * arg)
1615 ipmi_context_t ctx;
1616 unsigned offset = 0;
1617 uint8_t tmp;
1619 col_set_str(pinfo->cinfo, COL_PROTOCOL, "IPMB");
1621 memset(&ctx, 0, sizeof(ctx));
1623 /* copy message context and channel */
1624 ctx.hdr.context = arg ? arg->context : 0;
1625 ctx.hdr.channel = arg ? arg->channel : 0;
1627 /* guess IPMB message format */
1628 if (!guess_imb_format(tvb, ctx.hdr.context, ctx.hdr.channel,
1629 &ctx.flags, &ctx.cks1, &ctx.cks2)) {
1630 return 0;
1633 /* check if message is broadcast */
1634 if (ctx.flags & IPMI_D_BROADCAST) {
1635 /* skip first byte */
1636 offset++;
1639 /* check is session handle is specified */
1640 if (ctx.flags & IPMI_D_SESSION_HANDLE) {
1641 ctx.hdr.session = tvb_get_uint8(tvb, offset++);
1644 /* check is response address is specified */
1645 if (ctx.flags & IPMI_D_TRG_SA) {
1646 ctx.hdr.rs_sa = tvb_get_uint8(tvb, offset++);
1647 } else {
1648 ctx.hdr.rs_sa = 0x20;
1651 /* get Net Fn/RS LUN field */
1652 tmp = tvb_get_uint8(tvb, offset++);
1654 /* set Net Fn and RS LUN */
1655 ctx.hdr.netfn = tmp >> 2;
1656 ctx.hdr.rs_lun = tmp & 3;
1658 /* skip cks1 */
1659 offset++;
1661 /* get RQ SA */
1662 ctx.hdr.rq_sa = tvb_get_uint8(tvb, offset++);
1664 /* get RQ Seq/RQ LUN field */
1665 tmp = tvb_get_uint8(tvb, offset++);
1667 /* set RQ Seq and RQ LUN */
1668 ctx.hdr.rq_seq = tmp >> 2;
1669 ctx.hdr.rq_lun = tmp & 3;
1671 /* get command code */
1672 ctx.hdr.cmd = tvb_get_uint8(tvb, offset++);
1674 /* set header length */
1675 ctx.hdr_len = offset;
1677 /* copy direction */
1678 ctx.hdr.dir = arg ? arg->flags >> 7 : ctx.hdr.netfn & 1;
1680 if (ctx.hdr.context == IPMI_E_NONE) {
1681 unsigned red = arg ? (arg->flags & 0x40) : 0;
1683 if (!ctx.hdr.channel) {
1684 col_add_fstr(pinfo->cinfo, COL_DEF_SRC,
1685 "0x%02x(%s)", ctx.hdr.rq_sa, red ? "IPMB-B" : "IPMB-A");
1686 } else {
1687 col_add_fstr(pinfo->cinfo, COL_DEF_SRC,
1688 "0x%02x", ctx.hdr.rq_sa);
1691 col_add_fstr(pinfo->cinfo, COL_DEF_DST, "0x%02x", ctx.hdr.rs_sa);
1694 /* dissect IPMI command */
1695 return dissect_ipmi_cmd(tvb, pinfo, tree, hf_parent_item, ett_tree, &ctx);
1698 static int
1699 dissect_ipmi(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
1701 return do_dissect_ipmb(tvb, pinfo, tree, proto_ipmb, ett_ipmi,
1702 (ipmi_dissect_arg_t *) data);
1705 static int
1706 dissect_i2c_ipmi(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
1708 if (pinfo->pseudo_header->i2c.flags & 0x00000001) {
1709 /* Master-receive transactions are not possible on IPMB */
1710 return 0;
1713 return do_dissect_ipmb(tvb, pinfo, tree, proto_ipmb, ett_ipmi,
1714 (ipmi_dissect_arg_t *) data);
1718 /* Register IPMB protocol.
1720 void
1721 proto_register_ipmi(void)
1723 static hf_register_info hf[] = {
1724 { &hf_ipmi_command_data, { "Bus command data", "ipmi.bus_command_data", FT_BYTES, BASE_NONE, NULL, 0, NULL, HFILL }},
1725 { &hf_ipmi_session_handle, { "Session handle", "ipmi.session_handle", FT_UINT8, BASE_HEX, NULL, 0, NULL, HFILL }},
1726 { &hf_ipmi_header_trg, { "Target Address", "ipmi.header.target", FT_UINT8, BASE_HEX, NULL, 0x0, NULL, HFILL }},
1727 { &hf_ipmi_header_trg_lun, { "Target LUN", "ipmi.header.trg_lun", FT_UINT8, BASE_HEX, NULL, 0x03, NULL, HFILL }},
1728 { &hf_ipmi_header_netfn, { "NetFN", "ipmi.header.netfn", FT_UINT8, BASE_HEX, NULL, 0xfc, NULL, HFILL }},
1729 { &hf_ipmi_header_crc, { "Header Checksum", "ipmi.header.crc", FT_UINT8, BASE_HEX, NULL, 0, NULL, HFILL }},
1730 { &hf_ipmi_header_src, { "Source Address", "ipmi.header.source", FT_UINT8, BASE_HEX, NULL, 0, NULL, HFILL }},
1731 { &hf_ipmi_header_src_lun, { "Source LUN", "ipmi.header.src_lun", FT_UINT8, BASE_HEX, NULL, 0x03, NULL, HFILL }},
1732 { &hf_ipmi_header_bridged, { "Bridged", "ipmi.header.bridged", FT_UINT8, BASE_HEX, NULL, 0x03, NULL, HFILL }},
1733 { &hf_ipmi_header_sequence, { "Sequence Number", "ipmi.header.sequence", FT_UINT8, BASE_HEX, NULL, 0xfc, NULL, HFILL }},
1734 { &hf_ipmi_header_command, { "Command", "ipmi.header.command", FT_UINT8, BASE_HEX, NULL, 0, NULL, HFILL }},
1735 { &hf_ipmi_header_completion, { "Completion Code", "ipmi.header.completion", FT_UINT8, BASE_HEX, NULL, 0, NULL, HFILL }},
1736 { &hf_ipmi_header_sig, { "Signature", "ipmi.header.signature", FT_BYTES, BASE_NONE, NULL, 0, NULL, HFILL }},
1737 { &hf_ipmi_data_crc, { "Data checksum", "ipmi.data.crc", FT_UINT8, BASE_HEX, NULL, 0, NULL, HFILL }},
1738 { &hf_ipmi_response_to, { "Response to", "ipmi.response_to", FT_FRAMENUM, BASE_NONE, FRAMENUM_TYPE(FT_FRAMENUM_REQUEST), 0, NULL, HFILL }},
1739 { &hf_ipmi_response_in, { "Response in", "ipmi.response_in", FT_FRAMENUM, BASE_NONE, FRAMENUM_TYPE(FT_FRAMENUM_RESPONSE), 0, NULL, HFILL }},
1740 { &hf_ipmi_response_time, { "Responded in", "ipmi.response_time", FT_RELATIVE_TIME, BASE_NONE, NULL, 0, NULL, HFILL }}
1742 static int *ett[] = {
1743 &ett_ipmi,
1744 &ett_header,
1745 &ett_header_byte_1,
1746 &ett_header_byte_4,
1747 &ett_data,
1748 &ett_typelen
1750 static const enum_val_t msgfmt_vals[] = {
1751 { "none", "None", MSGFMT_NONE },
1752 { "ipmb", "IPMB", MSGFMT_IPMB },
1753 { "lan", "Session-based (LAN, ...)", MSGFMT_LAN },
1754 { "guess", "Use heuristics", MSGFMT_GUESS },
1755 { NULL, NULL, 0 }
1757 static const enum_val_t oemsel_vals[] = {
1758 { "none", "None", IPMI_OEM_NONE },
1759 { "pps", "Pigeon Point Systems", IPMI_OEM_PPS },
1760 { NULL, NULL, 0 }
1763 static ei_register_info ei[] = {
1764 { &ei_impi_parser_not_implemented, { "ipmi.parser_not_implemented", PI_UNDECODED, PI_WARN, "[PARSER NOT IMPLEMENTED]", EXPFILL }},
1767 module_t *module;
1768 expert_module_t* expert_ipmi;
1769 uint32_t i;
1771 proto_ipmi = proto_register_protocol("Intelligent Platform Management Interface",
1772 "IPMI",
1773 "ipmi");
1775 proto_ipmb = proto_register_protocol("Intelligent Platform Management Bus",
1776 "IPMB",
1777 "ipmb");
1778 proto_kcs = proto_register_protocol("Keyboard Controller Style Interface",
1779 "KCS",
1780 "kcs");
1781 proto_tmode = proto_register_protocol("Serial Terminal Mode Interface",
1782 "TMode",
1783 "tmode");
1785 proto_register_field_array(proto_ipmi, hf, array_length(hf));
1786 proto_register_subtree_array(ett, array_length(ett));
1788 expert_ipmi = expert_register_protocol(proto_ipmi);
1789 expert_register_field_array(expert_ipmi, ei, array_length(ei));
1791 ipmi_netfn_setdesc(IPMI_CHASSIS_REQ, "Chassis", 0);
1792 ipmi_netfn_setdesc(IPMI_BRIDGE_REQ, "Bridge", 0);
1793 ipmi_netfn_setdesc(IPMI_SE_REQ, "Sensor/Event", 0);
1794 ipmi_netfn_setdesc(IPMI_APP_REQ, "Application", 0);
1795 ipmi_netfn_setdesc(IPMI_UPDATE_REQ, "Firmware Update", 0);
1796 ipmi_netfn_setdesc(IPMI_STORAGE_REQ, "Storage", 0);
1797 ipmi_netfn_setdesc(IPMI_TRANSPORT_REQ, "Transport", 0);
1798 ipmi_netfn_setdesc(IPMI_GROUP_REQ, "Group", 1);
1799 ipmi_netfn_setdesc(IPMI_OEM_REQ, "OEM/Group", 3);
1800 for (i = 0x30; i < 0x40; i += 2) {
1801 ipmi_netfn_setdesc(i, "OEM", 0);
1804 register_dissector("ipmi", dissect_ipmi, proto_ipmi);
1805 ipmi_i2c_handle = register_dissector("ipmi.i2c", dissect_i2c_ipmi, proto_ipmi );
1806 register_dissector("ipmb", dissect_ipmi, proto_ipmb);
1807 register_dissector("kcs", dissect_kcs, proto_kcs);
1808 register_dissector("tmode", dissect_tmode, proto_tmode);
1810 module = prefs_register_protocol(proto_ipmi, NULL);
1811 prefs_register_bool_preference(module, "dissect_bus_commands", "Dissect bus commands",
1812 "Dissect IPMB commands",
1813 &dissect_bus_commands);
1814 prefs_register_bool_preference(module, "fru_langcode_is_english", "FRU Language Code is English",
1815 "FRU Language Code is English; strings are ASCII+LATIN1 (vs. Unicode)",
1816 &fru_langcode_is_english);
1817 prefs_register_uint_preference(module, "response_after_req", "Maximum delay of response message",
1818 "Do not search for responses coming after this timeout (milliseconds)",
1819 10, &response_after_req);
1820 prefs_register_uint_preference(module, "response_before_req", "Response ahead of request",
1821 "Allow for responses before requests (milliseconds)",
1822 10, &response_before_req);
1823 prefs_register_enum_preference(module, "msgfmt", "Format of embedded messages",
1824 "Format of messages embedded into Send/Get/Forward Message",
1825 &message_format, msgfmt_vals, false);
1826 prefs_register_enum_preference(module, "selected_oem", "OEM commands parsed as",
1827 "Selects which OEM format is used for commands that IPMI does not define",
1828 &selected_oem, oemsel_vals, false);
1831 void proto_reg_handoff_ipmi(void)
1833 dissector_add_for_decode_as("i2c.message", ipmi_i2c_handle );
1837 * Editor modelines - https://www.wireshark.org/tools/modelines.html
1839 * Local variables:
1840 * c-basic-offset: 8
1841 * tab-width: 8
1842 * indent-tabs-mode: t
1843 * End:
1845 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
1846 * :indentSize=8:tabSize=8:noTabs=false: