Use ENC_NA as encoding for proto_tree_add_item() calls which directly reference an...
[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         col_add_str(pinfo->cinfo, COL_INFO,
167                 val_to_str(type, asf_type_vals, "Unknown (0x%02x)"));
168
169         if (tree) {
170                 ti = proto_tree_add_item(tree, proto_asf, tvb, 0, 8,ENC_BIG_ENDIAN);
171                 asf_tree = proto_item_add_subtree(ti, ett_asf);
172                 proto_tree_add_item(asf_tree, hf_asf_iana, tvb, 0, 4,ENC_BIG_ENDIAN);
173                 proto_tree_add_item(asf_tree, hf_asf_type, tvb, 4, 1,ENC_BIG_ENDIAN);
174                 proto_tree_add_item(asf_tree, hf_asf_tag, tvb, 5, 1,ENC_BIG_ENDIAN);
175                 proto_tree_add_item(asf_tree, hf_asf_len, tvb, 7, 1,ENC_BIG_ENDIAN);
176         }
177
178         if (len) {
179                 switch(type) {
180                 case ASF_TYPE_OPEN_SESS_RQST:
181                         dissect_asf_open_session_request(tvb, asf_tree, 8, len);
182                         break;
183                 case ASF_TYPE_OPEN_SESS_RESP:
184                         dissect_asf_open_session_response(tvb, asf_tree, 8, len);
185                         break;
186
187                 /* TODO: Add the rest as captures become available to test. */
188
189                 default:
190                         next_tvb = tvb_new_subset(tvb, 8, len, len);
191                         call_dissector(data_handle, next_tvb, pinfo, tree);
192                         break;
193                 }
194         }
195         return 8 + len;
196 }
197
198 static void
199 dissect_asf_open_session_request(tvbuff_t *tvb, proto_tree *tree,
200         gint offset, gint len)
201 {
202         proto_tree_add_item(tree, hf_asf_mgt_console_id, tvb, offset, 4,ENC_BIG_ENDIAN);
203         offset += 4;
204         len    -= 4;
205         dissect_asf_payloads(tvb, tree, offset, len);
206 }
207
208 static void
209 dissect_asf_open_session_response(tvbuff_t *tvb, proto_tree *tree,
210         gint offset, gint len)
211 {
212         proto_tree_add_item(tree, hf_asf_rssp_status_code, tvb, offset, 1,ENC_BIG_ENDIAN);
213         proto_tree_add_item(tree, hf_asf_mgt_console_id, tvb, offset + 4, 4,ENC_BIG_ENDIAN);
214         proto_tree_add_item(tree, hf_asf_client_id, tvb, offset + 8, 4,ENC_BIG_ENDIAN);
215         offset += 12;
216         len    -= 12;
217         dissect_asf_payloads(tvb, tree, offset, len);
218 }
219
220 static void
221 dissect_asf_payloads(tvbuff_t *tvb, proto_tree *tree,
222         gint offset, gint len)
223 {
224         guint8      ptype;
225         guint16     plen;
226         proto_item *ti;
227         proto_tree *ptree;
228
229         while ( len >= 4 )
230         {
231                 ptype = tvb_get_guint8(tvb, offset);
232                 plen = tvb_get_ntohs(tvb, offset + 2);
233
234                 ti = proto_tree_add_none_format(tree, hf_asf_payload, tvb, offset,
235                         plen, "%s: %u bytes",
236                         val_to_str(ptype, asf_payload_type_vals, "Unknown (%u)"), plen);
237                 ptree = proto_item_add_subtree(ti, ett_asf_payload);
238                 proto_tree_add_item(ptree, hf_asf_payload_type, tvb, offset, 1,ENC_BIG_ENDIAN);
239                 proto_tree_add_item(ptree, hf_asf_payload_len, tvb, offset + 2, 2,ENC_BIG_ENDIAN);
240                 if ( ptype && (plen > 4) )
241                 {
242                         switch ( ptype )
243                         {
244                                 case ASF_PAYLOAD_TYPE_AUTHENTICATION:
245                                         dissect_asf_payload_authentication(tvb, ptree,
246                                                 offset + 4, plen - 4);
247                                         break;
248                                 case ASF_PAYLOAD_TYPE_INTEGRITY:
249                                         dissect_asf_payload_integrity(tvb, ptree,
250                                                 offset + 4, plen - 4);
251                                         break;
252                                 default:
253                                         proto_tree_add_item(ptree, hf_asf_payload_data, tvb,
254                                                 offset + 4, plen - 4,ENC_NA);
255                                         break;
256                         }
257                 }
258                 offset += plen;
259                 len    -= plen;
260         }
261 }
262
263 static void
264 dissect_asf_payload_authentication(tvbuff_t *tvb, proto_tree *tree,
265         gint offset, gint len)
266 {
267         guint8      alg;
268         proto_item *ti;
269         proto_tree *atree;
270
271         alg = tvb_get_guint8(tvb, offset);
272         ti = proto_tree_add_none_format(tree, hf_asf_payload_data, tvb, offset,
273                 len, "Authentication Algorithm: %s",
274                 val_to_str(alg, asf_authentication_type_vals, "Unknown (%u)"));
275         atree = proto_item_add_subtree(ti, ett_asf_alg_payload);
276         proto_tree_add_item(atree, hf_asf_auth_alg, tvb, offset, 1,ENC_BIG_ENDIAN);
277         proto_tree_add_item(atree, hf_asf_reserved, tvb, offset + 1, len - 1,ENC_NA);
278 }
279
280 static void
281 dissect_asf_payload_integrity(tvbuff_t *tvb, proto_tree *tree,
282         gint offset, gint len)
283 {
284         guint8      alg;
285         proto_item *ti;
286         proto_tree *atree;
287
288         alg = tvb_get_guint8(tvb, offset);
289         ti = proto_tree_add_none_format(tree, hf_asf_payload_data, tvb, offset,
290                 len, "Integrity Algorithm: %s",
291                 val_to_str(alg, asf_integrity_type_vals, "Unknown (%u)"));
292         atree = proto_item_add_subtree(ti, ett_asf_alg_payload);
293         proto_tree_add_item(atree, hf_asf_integrity_alg, tvb, offset, 1,ENC_BIG_ENDIAN);
294         proto_tree_add_item(atree, hf_asf_reserved, tvb, offset + 1, len - 1,ENC_NA);
295 }
296
297 void
298 proto_register_asf(void)
299 {
300         static hf_register_info hf[] = {
301                 { &hf_asf_iana, {
302                         "IANA Enterprise Number", "asf.iana",
303                         FT_UINT32, BASE_DEC|BASE_EXT_STRING, &sminmpec_values_ext, 0,
304                         NULL, HFILL }},
305                 { &hf_asf_type, {
306                         "Message Type", "asf.type",
307                         FT_UINT8, BASE_HEX, VALS(asf_type_vals), 0,
308                         "ASF Message Type", HFILL }},
309                 { &hf_asf_tag, {
310                         "Message Tag", "asf.tag",
311                         FT_UINT8, BASE_HEX, NULL, 0,
312                         "ASF Message Tag", HFILL }},
313                 { &hf_asf_len, {
314                         "Data Length", "asf.len",
315                         FT_UINT8, BASE_DEC, NULL, 0,
316                         "ASF Data Length", HFILL }},
317                 { &hf_asf_rssp_status_code, {
318                         "Status Code", "asf.rssp_status_code",
319                         FT_UINT8, BASE_DEC, VALS(asf_rssp_status_code_vals), 0,
320                         "Identifies the status of the previous message", HFILL }},
321                 { &hf_asf_mgt_console_id, {
322                         "Mgt Console Session ID", "asf.mgt_console_id",
323                         FT_UINT32, BASE_DEC, NULL, 0,
324                         NULL, HFILL }},
325                 { &hf_asf_client_id, {
326                         "Managed Client Session ID", "asf.client_id",
327                         FT_UINT32, BASE_DEC, NULL, 0,
328                         NULL, HFILL }},
329                 { &hf_asf_payload, {
330                         "Payload", "asf.payload",
331                         FT_NONE, BASE_NONE, NULL, 0,
332                         NULL, HFILL }},
333                 { &hf_asf_payload_type, {
334                         "Payload Type", "asf.payload.type",
335                         FT_UINT8, BASE_DEC, VALS(asf_payload_type_vals), 0,
336                         "Identifies the type of payload that follows", HFILL }},
337                 { &hf_asf_payload_len, {
338                         "Payload Length", "asf.payload.len",
339                         FT_UINT16, BASE_DEC, NULL, 0,
340                         "The total length in bytes of the payload including the header",
341                         HFILL }},
342                 { &hf_asf_payload_data, {
343                         "Data", "asf.payload.data",
344                         FT_NONE, BASE_NONE, NULL, 0,
345                         NULL, HFILL }},
346                 { &hf_asf_auth_alg, {
347                         "Authentication Algorithm", "asf.auth_alg",
348                         FT_UINT8, BASE_DEC, VALS(asf_authentication_type_vals), 0,
349                         NULL, HFILL }},
350                 { &hf_asf_integrity_alg, {
351                         "Integrity Algorithm", "asf.integrity_alg",
352                         FT_UINT8, BASE_DEC, VALS(asf_integrity_type_vals), 0,
353                         NULL, HFILL }},
354                 { &hf_asf_reserved, {
355                         "Reserved", "asf.reserved",
356                         FT_NONE, BASE_NONE, NULL, 0,
357                         NULL, HFILL }},
358         };
359         static gint *ett[] = {
360                 &ett_asf,
361                 &ett_asf_payload,
362                 &ett_asf_alg_payload
363         };
364
365         proto_asf = proto_register_protocol(
366                 "Alert Standard Forum", "ASF", "asf");
367
368         proto_register_field_array(proto_asf, hf, array_length(hf));
369         proto_register_subtree_array(ett, array_length(ett));
370 }
371
372 void
373 proto_reg_handoff_asf(void)
374 {
375         dissector_handle_t asf_handle;
376
377         data_handle = find_dissector("data");
378
379         asf_handle  = new_create_dissector_handle(dissect_asf, proto_asf);
380         dissector_add_uint("rmcp.class", RMCP_CLASS_ASF, asf_handle);
381 }