Rename the routines that handle dissector tables with unsigned integer
[obnox/wireshark/wip.git] / epan / dissectors / packet-asf.c
1 /* packet-asf.c
2  * Routines for ASF packet dissection
3  *
4  * Duncan Laurie <duncan@sun.com>
5  *
6  * $Id$
7  *
8  * Wireshark - Network traffic analyzer
9  * By Gerald Combs <gerald@wireshark.org>
10  * Copyright 1998 Gerald Combs
11  *
12  * Copied from packet-rmcp.c
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., 59 Temple Place - Suite 330, Boston, MA
27  * 02111-1307, USA.
28  */
29
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33
34 #include <glib.h>
35 #include <epan/packet.h>
36 #include <epan/sminmpec.h>
37
38 /*
39  * See
40  *      http://www.dmtf.org/standards/standard_alert.php
41  *      http://www.dmtf.org/standards/documents/ASF/DSP0136.pdf
42  */
43
44 #define RMCP_CLASS_ASF 0x06
45
46 static int proto_asf = -1;
47 static int hf_asf_iana = -1;
48 static int hf_asf_type = -1;
49 static int hf_asf_tag = -1;
50 static int hf_asf_len = -1;
51 static int hf_asf_rssp_status_code = -1;
52 static int hf_asf_mgt_console_id = -1;
53 static int hf_asf_client_id = -1;
54 static int hf_asf_payload = -1;
55 static int hf_asf_payload_type = -1;
56 static int hf_asf_payload_len = -1;
57 static int hf_asf_payload_data = -1;
58 static int hf_asf_auth_alg = -1;
59 static int hf_asf_integrity_alg = -1;
60 static int hf_asf_reserved = -1;
61
62 static dissector_handle_t data_handle;
63 static gint ett_asf = -1;
64 static gint ett_asf_payload = -1;
65 static gint ett_asf_alg_payload = -1;
66
67 #define ASF_TYPE_RESET                  0x10
68 #define ASF_TYPE_PWR_UP                 0x11
69 #define ASF_TYPE_PWR_DOWN               0x12
70 #define ASF_TYPE_PWR_CYCLE              0x13
71 #define ASF_TYPE_PRES_PONG              0x40
72 #define ASF_TYPE_CAP_RESP               0x41
73 #define ASF_TYPE_SYS_STATE_RESP         0x42
74 #define ASF_TYPE_OPEN_SESS_RESP         0x43
75 #define ASF_TYPE_CLOSE_SESS_RESP        0x44
76 #define ASF_TYPE_PRES_PING              0x80
77 #define ASF_TYPE_CAP_RQST               0x81
78 #define ASF_TYPE_SYS_STATE_RQST         0x82
79 #define ASF_TYPE_OPEN_SESS_RQST         0x83
80 #define ASF_TYPE_CLOSE_SESS_RQST        0x84
81 #define ASF_TYPE_RAKP_MSG_1             0xC0
82 #define ASF_TYPE_RAKP_MSG_2             0xC1
83 #define ASF_TYPE_RAKP_MSG_3             0xC2
84
85 static const value_string asf_type_vals[] = {
86         { ASF_TYPE_RESET,           "Reset" },
87         { ASF_TYPE_PWR_UP,          "Power-up" },
88         { ASF_TYPE_PWR_DOWN,        "Unconditional Power-down" },
89         { ASF_TYPE_PWR_CYCLE,       "Power Cycle" },
90         { ASF_TYPE_PRES_PONG,       "Presence Pong" },
91         { ASF_TYPE_CAP_RESP,        "Capabilities Response" },
92         { ASF_TYPE_SYS_STATE_RESP,  "System State Response" },
93         { ASF_TYPE_OPEN_SESS_RESP,  "Open Session Response" },
94         { ASF_TYPE_CLOSE_SESS_RESP, "Close Session Response" },
95         { ASF_TYPE_PRES_PING,       "Presence Ping" },
96         { ASF_TYPE_CAP_RQST,        "Capabilities Request" },
97         { ASF_TYPE_SYS_STATE_RQST,  "System State Request" },
98         { ASF_TYPE_OPEN_SESS_RQST,  "Open Session Request" },
99         { ASF_TYPE_CLOSE_SESS_RQST, "Close Session Request" },
100         { ASF_TYPE_RAKP_MSG_1,      "RAKP Message 1" },
101         { ASF_TYPE_RAKP_MSG_2,      "RAKP Message 2" },
102         { ASF_TYPE_RAKP_MSG_3,      "RAKP Message 3" },
103         { 0x00, NULL }
104 };
105
106 static const value_string asf_rssp_status_code_vals[] = {
107         { 0x00, "No errors" },
108         { 0x01, "Insufficient resources to create a session" },
109         { 0x02, "Invalid session ID" },
110         { 0x03, "Invalid payload type" },
111         { 0x04, "Invalid authentication algorithm" },
112         { 0x05, "Invalid integrity algorithm" },
113         { 0x06, "No matching authentication payload" },
114         { 0x07, "No matching integrity payload" },
115         { 0x00, NULL }
116 };
117
118 #define ASF_PAYLOAD_TYPE_NONE           0x00
119 #define ASF_PAYLOAD_TYPE_AUTHENTICATION 0x01
120 #define ASF_PAYLOAD_TYPE_INTEGRITY      0x02
121
122 static const value_string asf_payload_type_vals[] = {
123         { ASF_PAYLOAD_TYPE_NONE,           "No payload present (end of list)" },
124         { ASF_PAYLOAD_TYPE_AUTHENTICATION, "Authentication algorithm payload" },
125         { ASF_PAYLOAD_TYPE_INTEGRITY,      "Integrity algorithm payload" },
126         { 0x00, NULL }
127 };
128
129 static const value_string asf_authentication_type_vals[] = {
130         { 0x01, "RAKP-HMAC-SHA1" },
131         { 0x00, NULL }
132 };
133
134 static const value_string asf_integrity_type_vals[] = {
135         { 0x01, "HMAC-SHA1-96" },
136         { 0x00, NULL }
137 };
138
139 static void dissect_asf_open_session_request(tvbuff_t *tvb, proto_tree *tree,
140         gint offset, gint len);
141 static void dissect_asf_open_session_response(tvbuff_t *tvb, proto_tree *tree,
142         gint offset, gint len);
143 static void dissect_asf_payloads(tvbuff_t *tvb, proto_tree *tree,
144         gint offset, gint len);
145 static void dissect_asf_payload_authentication(tvbuff_t *tvb, proto_tree *tree,
146         gint offset, gint len);
147 static void dissect_asf_payload_integrity(tvbuff_t *tvb, proto_tree *tree,
148         gint offset, gint len);
149
150 static int
151 dissect_asf(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
152 {
153         proto_tree *asf_tree = NULL;
154         proto_item *ti;
155         guint8      type;
156         guint8      len;
157         tvbuff_t   *next_tvb;
158
159         col_set_str(pinfo->cinfo, COL_PROTOCOL, "ASF");
160
161         col_clear(pinfo->cinfo, COL_INFO);
162
163         type = tvb_get_guint8(tvb, 4);
164         len = tvb_get_guint8(tvb, 7);
165
166         if (check_col(pinfo->cinfo, COL_INFO))
167                 col_add_str(pinfo->cinfo, COL_INFO,
168                         val_to_str(type, asf_type_vals, "Unknown (0x%02x)"));
169
170         if (tree) {
171                 ti = proto_tree_add_item(tree, proto_asf, tvb, 0, 8, FALSE);
172                 asf_tree = proto_item_add_subtree(ti, ett_asf);
173                 proto_tree_add_item(asf_tree, hf_asf_iana, tvb, 0, 4, FALSE);
174                 proto_tree_add_item(asf_tree, hf_asf_type, tvb, 4, 1, FALSE);
175                 proto_tree_add_item(asf_tree, hf_asf_tag, tvb, 5, 1, FALSE);
176                 proto_tree_add_item(asf_tree, hf_asf_len, tvb, 7, 1, FALSE);
177         }
178
179         if (len) {
180                 switch(type) {
181                 case ASF_TYPE_OPEN_SESS_RQST:
182                         dissect_asf_open_session_request(tvb, asf_tree, 8, len);
183                         break;
184                 case ASF_TYPE_OPEN_SESS_RESP:
185                         dissect_asf_open_session_response(tvb, asf_tree, 8, len);
186                         break;
187
188                 /* TODO: Add the rest as captures become available to test. */
189
190                 default:
191                         next_tvb = tvb_new_subset(tvb, 8, len, len);
192                         call_dissector(data_handle, next_tvb, pinfo, tree);
193                         break;
194                 }
195         }
196         return 8 + len;
197 }
198
199 static void
200 dissect_asf_open_session_request(tvbuff_t *tvb, proto_tree *tree,
201         gint offset, gint len)
202 {
203         proto_tree_add_item(tree, hf_asf_mgt_console_id, tvb, offset, 4, FALSE);
204         offset += 4;
205         len    -= 4;
206         dissect_asf_payloads(tvb, tree, offset, len);
207 }
208
209 static void
210 dissect_asf_open_session_response(tvbuff_t *tvb, proto_tree *tree,
211         gint offset, gint len)
212 {
213         proto_tree_add_item(tree, hf_asf_rssp_status_code, tvb, offset, 1, FALSE);
214         proto_tree_add_item(tree, hf_asf_mgt_console_id, tvb, offset + 4, 4, FALSE);
215         proto_tree_add_item(tree, hf_asf_client_id, tvb, offset + 8, 4, FALSE);
216         offset += 12;
217         len    -= 12;
218         dissect_asf_payloads(tvb, tree, offset, len);
219 }
220
221 static void
222 dissect_asf_payloads(tvbuff_t *tvb, proto_tree *tree,
223         gint offset, gint len)
224 {
225         guint8      ptype;
226         guint16     plen;
227         proto_item *ti;
228         proto_tree *ptree;
229
230         while ( len >= 4 )
231         {
232                 ptype = tvb_get_guint8(tvb, offset);
233                 plen = tvb_get_ntohs(tvb, offset + 2);
234
235                 ti = proto_tree_add_none_format(tree, hf_asf_payload, tvb, offset,
236                         plen, "%s: %u bytes",
237                         val_to_str(ptype, asf_payload_type_vals, "Unknown (%u)"), plen);
238                 ptree = proto_item_add_subtree(ti, ett_asf_payload);
239                 proto_tree_add_item(ptree, hf_asf_payload_type, tvb, offset, 1, FALSE);
240                 proto_tree_add_item(ptree, hf_asf_payload_len, tvb, offset + 2, 2, FALSE);
241                 if ( ptype && (plen > 4) )
242                 {
243                         switch ( ptype )
244                         {
245                                 case ASF_PAYLOAD_TYPE_AUTHENTICATION:
246                                         dissect_asf_payload_authentication(tvb, ptree,
247                                                 offset + 4, plen - 4);
248                                         break;
249                                 case ASF_PAYLOAD_TYPE_INTEGRITY:
250                                         dissect_asf_payload_integrity(tvb, ptree,
251                                                 offset + 4, plen - 4);
252                                         break;
253                                 default:
254                                         proto_tree_add_item(ptree, hf_asf_payload_data, tvb,
255                                                 offset + 4, plen - 4, FALSE);
256                                         break;
257                         }
258                 }
259                 offset += plen;
260                 len    -= plen;
261         }
262 }
263
264 static void
265 dissect_asf_payload_authentication(tvbuff_t *tvb, proto_tree *tree,
266         gint offset, gint len)
267 {
268         guint8      alg;
269         proto_item *ti;
270         proto_tree *atree;
271
272         alg = tvb_get_guint8(tvb, offset);
273         ti = proto_tree_add_none_format(tree, hf_asf_payload_data, tvb, offset,
274                 len, "Authentication Algorithm: %s",
275                 val_to_str(alg, asf_authentication_type_vals, "Unknown (%u)"));
276         atree = proto_item_add_subtree(ti, ett_asf_alg_payload);
277         proto_tree_add_item(atree, hf_asf_auth_alg, tvb, offset, 1, FALSE);
278         proto_tree_add_item(atree, hf_asf_reserved, tvb, offset + 1, len - 1, FALSE);
279 }
280
281 static void
282 dissect_asf_payload_integrity(tvbuff_t *tvb, proto_tree *tree,
283         gint offset, gint len)
284 {
285         guint8      alg;
286         proto_item *ti;
287         proto_tree *atree;
288
289         alg = tvb_get_guint8(tvb, offset);
290         ti = proto_tree_add_none_format(tree, hf_asf_payload_data, tvb, offset,
291                 len, "Integrity Algorithm: %s",
292                 val_to_str(alg, asf_integrity_type_vals, "Unknown (%u)"));
293         atree = proto_item_add_subtree(ti, ett_asf_alg_payload);
294         proto_tree_add_item(atree, hf_asf_integrity_alg, tvb, offset, 1, FALSE);
295         proto_tree_add_item(atree, hf_asf_reserved, tvb, offset + 1, len - 1, FALSE);
296 }
297
298 void
299 proto_register_asf(void)
300 {
301         static hf_register_info hf[] = {
302                 { &hf_asf_iana, {
303                         "IANA Enterprise Number", "asf.iana",
304                         FT_UINT32, BASE_DEC|BASE_EXT_STRING, &sminmpec_values_ext, 0,
305                         NULL, HFILL }},
306                 { &hf_asf_type, {
307                         "Message Type", "asf.type",
308                         FT_UINT8, BASE_HEX, VALS(asf_type_vals), 0,
309                         "ASF Message Type", HFILL }},
310                 { &hf_asf_tag, {
311                         "Message Tag", "asf.tag",
312                         FT_UINT8, BASE_HEX, NULL, 0,
313                         "ASF Message Tag", HFILL }},
314                 { &hf_asf_len, {
315                         "Data Length", "asf.len",
316                         FT_UINT8, BASE_DEC, NULL, 0,
317                         "ASF Data Length", HFILL }},
318                 { &hf_asf_rssp_status_code, {
319                         "Status Code", "asf.rssp_status_code",
320                         FT_UINT8, BASE_DEC, VALS(asf_rssp_status_code_vals), 0,
321                         "Identifies the status of the previous message", HFILL }},
322                 { &hf_asf_mgt_console_id, {
323                         "Mgt Console Session ID", "asf.mgt_console_id",
324                         FT_UINT32, BASE_DEC, NULL, 0,
325                         NULL, HFILL }},
326                 { &hf_asf_client_id, {
327                         "Managed Client Session ID", "asf.client_id",
328                         FT_UINT32, BASE_DEC, NULL, 0,
329                         NULL, HFILL }},
330                 { &hf_asf_payload, {
331                         "Payload", "asf.payload",
332                         FT_NONE, BASE_NONE, NULL, 0,
333                         NULL, HFILL }},
334                 { &hf_asf_payload_type, {
335                         "Payload Type", "asf.payload.type",
336                         FT_UINT8, BASE_DEC, VALS(asf_payload_type_vals), 0,
337                         "Identifies the type of payload that follows", HFILL }},
338                 { &hf_asf_payload_len, {
339                         "Payload Length", "asf.payload.len",
340                         FT_UINT16, BASE_DEC, NULL, 0,
341                         "The total length in bytes of the payload including the header",
342                         HFILL }},
343                 { &hf_asf_payload_data, {
344                         "Data", "asf.payload.data",
345                         FT_NONE, BASE_NONE, NULL, 0,
346                         NULL, HFILL }},
347                 { &hf_asf_auth_alg, {
348                         "Authentication Algorithm", "asf.auth_alg",
349                         FT_UINT8, BASE_DEC, VALS(asf_authentication_type_vals), 0,
350                         NULL, HFILL }},
351                 { &hf_asf_integrity_alg, {
352                         "Integrity Algorithm", "asf.integrity_alg",
353                         FT_UINT8, BASE_DEC, VALS(asf_integrity_type_vals), 0,
354                         NULL, HFILL }},
355                 { &hf_asf_reserved, {
356                         "Reserved", "asf.reserved",
357                         FT_NONE, BASE_NONE, NULL, 0,
358                         NULL, HFILL }},
359         };
360         static gint *ett[] = {
361                 &ett_asf,
362                 &ett_asf_payload,
363                 &ett_asf_alg_payload
364         };
365
366         proto_asf = proto_register_protocol(
367                 "Alert Standard Forum", "ASF", "asf");
368
369         proto_register_field_array(proto_asf, hf, array_length(hf));
370         proto_register_subtree_array(ett, array_length(ett));
371 }
372
373 void
374 proto_reg_handoff_asf(void)
375 {
376         dissector_handle_t asf_handle;
377
378         data_handle = find_dissector("data");
379
380         asf_handle  = new_create_dissector_handle(dissect_asf, proto_asf);
381         dissector_add_uint("rmcp.class", RMCP_CLASS_ASF, asf_handle);
382 }