Trivial warning fixes
[obnox/wireshark/wip.git] / epan / dissectors / packet-igap.c
1 /* packet-igap.c
2  * Routines for IGMP/IGAP packet disassembly
3  * 2003, Endoh Akria (see AUTHORS for email)
4  *
5  * $Id$
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998 Gerald Combs
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  */
25
26 /*
27         IGAP "Internet Group membership Authentication Protocol"
28         is defined in draft-hayashi-igap-03.txt.
29
30         (Author's memo)
31          Type  Subtype  Message                     Msize
32         -----------------------------------------------------
33          ----   0x02    User password               variable
34          ----   0x03    ----                        00
35          ----   0x04    Result of MD5 calculation   16
36          0x41   0x23    Challenge value             ??
37          0x41   0x24    Authentication result code   1
38          0x41   0x25    Accounting status code       1
39          ----   0x42    User password               variable
40          ----   0x43    ----                        00
41          ----   0x44    Result of MD5 calculation   16
42
43 */
44
45 #ifdef HAVE_CONFIG_H
46 # include "config.h"
47 #endif
48
49 #include <glib.h>
50 #include <epan/packet.h>
51 #include <epan/strutil.h>
52 #include "packet-igmp.h"
53 #include "packet-igap.h"
54
55
56 static int proto_igap      = -1;
57 static int hf_type         = -1;
58 static int hf_max_resp     = -1;
59 static int hf_checksum     = -1;
60 static int hf_checksum_bad = -1;
61 static int hf_maddr        = -1;
62 static int hf_version      = -1;
63 static int hf_subtype      = -1;
64 static int hf_challengeid  = -1;
65 static int hf_asize        = -1;
66 static int hf_msize        = -1;
67 static int hf_account      = -1;
68
69 static int ett_igap = -1;
70
71
72 static const value_string igap_types[] = {
73     {IGMP_IGAP_JOIN,  "Membership Report (Join)"},
74     {IGMP_IGAP_QUERY, "Membership Query"},
75     {IGMP_IGAP_LEAVE, "Leave Group"},
76     {0, NULL}
77 };
78
79 #define IGAP_VERSION_1 0x10
80 static const value_string igap_version[] = {
81     {IGAP_VERSION_1, "1"},
82     {0, NULL}
83 };
84
85 #define IGAP_SUBTYPE_PASSWORD_JOIN            0x02
86 #define IGAP_SUBTYPE_CHALLENGE_REQUEST_JOIN   0x03
87 #define IGAP_SUBTYPE_CHALLENGE_RESPONSE_JOIN  0x04
88 #define IGAP_SUBTYPE_BASIC_QUERY              0x21
89 #define IGAP_SUBTYPE_CHALLENGE                0x23
90 #define IGAP_SUBTYPE_AUTH_MESSAGE             0x24
91 #define IGAP_SUBTYPE_ACCOUNTING_MESSAGE       0x25
92 #define IGAP_SUBTYPE_BASIC_LEAVE              0x41
93 #define IGAP_SUBTYPE_PASSWORD_LEAVE           0x42
94 #define IGAP_SUBTYPE_CHALLENGE_REQUEST_LEAVE  0x43
95 #define IGAP_SUBTYPE_CHALLENGE_RESPONSE_LEAVE 0x44
96 static const value_string igap_subtypes[] = {
97     {IGAP_SUBTYPE_PASSWORD_JOIN,            "Password Mechanism Join (Password-Join)"},
98     {IGAP_SUBTYPE_CHALLENGE_REQUEST_JOIN,   "Challenge-Response Mechanism Join Request (Challenge-Request-Join)"},
99     {IGAP_SUBTYPE_CHALLENGE_RESPONSE_JOIN,  "Challenge-Response Mechanism Join Response (Challenge-Response-Join)"},
100     {IGAP_SUBTYPE_BASIC_QUERY,              "Basic Query"},
101     {IGAP_SUBTYPE_CHALLENGE,                "Challenge-Response Mechanism Challenge (Challenge)"},
102     {IGAP_SUBTYPE_AUTH_MESSAGE,             "Authentication Message"},
103     {IGAP_SUBTYPE_ACCOUNTING_MESSAGE,       "Accounting Message"},
104     {IGAP_SUBTYPE_BASIC_LEAVE,              "Basic Leave"},
105     {IGAP_SUBTYPE_PASSWORD_LEAVE,           "Password Mechanism Leave (Password-Leave)"},
106     {IGAP_SUBTYPE_CHALLENGE_REQUEST_LEAVE,  "Challenge-Response Mechanism Leave Challenge Request (Challenge-Request-Leave)"},
107     {IGAP_SUBTYPE_CHALLENGE_RESPONSE_LEAVE, "Challenge-Response Mechanism Response (Challenge-Response-Leave)"},
108     {0, NULL}
109 };
110
111 #define IGAP_AUTH_SUCCESS 0x11
112 #define IGAP_AUTH_FAIL    0x21
113 static const value_string igap_auth_result[] = {
114     {IGAP_AUTH_SUCCESS, "Authentication success"},
115     {IGAP_AUTH_FAIL,    "Authentication failure"},
116     {0, NULL}
117 };
118
119 #define IGAP_ACCOUNT_START 0x11
120 #define IGAP_ACCOUNT_STOP  0x21
121 static const value_string igap_account_status[] = {
122     {IGAP_ACCOUNT_START, "Accounting start"},
123     {IGAP_ACCOUNT_STOP,  "Accounting stop"},
124     {0, NULL}
125 };
126
127 #define ACCOUNT_SIZE    16
128 #define MESSAGE_SIZE    64
129
130 /* This function is only called from the IGMP dissector */
131 int
132 dissect_igap(tvbuff_t *tvb, packet_info *pinfo, proto_tree *parent_tree, int offset)
133 {
134     proto_tree *tree;
135     proto_item *item;
136     guint8 type, tsecs, subtype, asize, msize;
137     guchar account[ACCOUNT_SIZE+1], message[MESSAGE_SIZE+1];
138
139     if (!proto_is_protocol_enabled(find_protocol_by_id(proto_igap))) {
140         /* we are not enabled, skip entire packet to be nice
141            to the igmp layer. (so clicking on IGMP will display the data)
142            */
143         return offset + tvb_length_remaining(tvb, offset);
144     }
145
146     item = proto_tree_add_item(parent_tree, proto_igap, tvb, offset, -1, FALSE);
147     tree = proto_item_add_subtree(item, ett_igap);
148
149     if (check_col(pinfo->cinfo, COL_PROTOCOL)) {
150         col_set_str(pinfo->cinfo, COL_PROTOCOL, "IGAP");
151     }
152     if (check_col(pinfo->cinfo, COL_INFO)) {
153         col_clear(pinfo->cinfo, COL_INFO);
154     }
155
156     type = tvb_get_guint8(tvb, offset);
157     if (check_col(pinfo->cinfo, COL_INFO)) {
158         col_add_str(pinfo->cinfo, COL_INFO, 
159                      val_to_str(type, igap_types, "Unknown Type: 0x%02x"));
160     }
161     proto_tree_add_uint(tree, hf_type, tvb, offset, 1, type);
162     offset += 1;
163
164     tsecs = tvb_get_guint8(tvb, offset);
165     proto_tree_add_uint_format(tree, hf_max_resp, tvb, offset, 1, tsecs,
166         "Max Response Time: %.1f sec (0x%02x)", tsecs * 0.1, tsecs);
167     offset += 1;
168
169     igmp_checksum(tree, tvb, hf_checksum, hf_checksum_bad, pinfo, 0);
170     offset += 2;
171
172     proto_tree_add_item(tree, hf_maddr, tvb, offset, 4, FALSE);
173     offset += 4;
174
175     proto_tree_add_uint(tree, hf_version, tvb, offset, 1, 
176         tvb_get_guint8(tvb, offset));
177     offset += 1;
178
179     subtype = tvb_get_guint8(tvb, offset);
180     proto_tree_add_uint(tree, hf_subtype, tvb, offset, 1, subtype);
181     offset += 2;
182
183     proto_tree_add_uint(tree, hf_challengeid, tvb, offset, 1,
184         tvb_get_guint8(tvb, offset));
185     offset += 1;
186
187     asize = tvb_get_guint8(tvb, offset);
188     proto_tree_add_uint(tree, hf_asize, tvb, offset, 1, asize);
189     offset += 1;
190
191     msize = tvb_get_guint8(tvb, offset);
192     proto_tree_add_uint(tree, hf_msize, tvb, offset, 1, msize);
193     offset += 3;
194
195     if (asize > 0) {
196         if (asize > ACCOUNT_SIZE) {
197             /* Bogus account size.
198                XXX - flag this? */
199             asize = ACCOUNT_SIZE;
200         }
201         tvb_memcpy(tvb, account, offset, asize);
202         account[asize] = '\0';
203         proto_tree_add_string(tree, hf_account, tvb, offset, asize, account);
204     }
205     offset += ACCOUNT_SIZE;
206
207     if (msize > 0) {
208         if (msize > MESSAGE_SIZE) {
209             /* Bogus message size.
210                XXX - flag this? */
211             msize = MESSAGE_SIZE;
212         }
213         tvb_memcpy(tvb, message, offset, msize);
214         switch (subtype) {
215         case IGAP_SUBTYPE_PASSWORD_JOIN:
216         case IGAP_SUBTYPE_PASSWORD_LEAVE:
217             /* Challenge field is user's password */
218             message[msize] = '\0';
219             proto_tree_add_text(tree, tvb, offset, msize,
220                                 "User password: %s", message);
221             break;
222         case IGAP_SUBTYPE_CHALLENGE_RESPONSE_JOIN:
223         case IGAP_SUBTYPE_CHALLENGE_RESPONSE_LEAVE:
224             /* Challenge field is the results of MD5 calculation */
225             proto_tree_add_text(tree, tvb, offset, msize,
226                                 "Result of MD5 calculation: 0x%s",
227                                 bytes_to_str(message, msize));
228             break;
229         case IGAP_SUBTYPE_CHALLENGE:
230             /* Challenge field is the challenge value */
231             proto_tree_add_text(tree, tvb, offset, msize,
232                                 "Challenge: 0x%s",
233                                 bytes_to_str(message, msize));
234             break;
235         case IGAP_SUBTYPE_AUTH_MESSAGE:
236             /* Challenge field indicates the result of the authenticaion */
237             proto_tree_add_text(tree, tvb, offset, msize,
238                                 "Authentication result: %s (0x%x)",
239                                 val_to_str(message[0], igap_auth_result,
240                                 "Unknown"), message[0]);
241             break;
242         case IGAP_SUBTYPE_ACCOUNTING_MESSAGE:
243             /* Challenge field indicates the accounting status */
244             proto_tree_add_text(tree, tvb, offset, msize,
245                                 "Accounting status: %s (0x%x)",
246                                 val_to_str(message[0], igap_account_status,
247                                 "Unknown"), message[0]);
248             break;
249         default:
250             proto_tree_add_text(tree, tvb, offset, msize,
251                                 "Message: (Unknown)");
252         }
253     }
254     offset += MESSAGE_SIZE;
255
256     if (item) proto_item_set_len(item, offset);
257     return offset;
258 }
259
260
261 void
262 proto_register_igap(void)
263 {
264     static hf_register_info hf[] = {
265         { &hf_type,
266           { "Type", "igap.type", FT_UINT8, BASE_HEX,
267             VALS(igap_types), 0, "IGAP Packet Type", HFILL }
268         },
269
270         { &hf_max_resp,
271           { "Max Resp Time", "igap.max_resp", FT_UINT8, BASE_DEC,
272             NULL, 0, "Max Response Time", HFILL }
273         },
274
275         { &hf_checksum,
276           { "Checksum", "igap.checksum", FT_UINT16, BASE_HEX,
277             NULL, 0, "Checksum", HFILL }
278         },
279
280         { &hf_checksum_bad,
281           { "Bad Checksum", "igap.checksum_bad", FT_BOOLEAN, BASE_NONE,
282             NULL, 0, "Bad Checksum", HFILL }
283         },
284
285         { &hf_maddr,
286           { "Multicast group address", "igap.maddr", FT_IPv4, BASE_NONE,
287             NULL, 0, "Multicast group address", HFILL }
288         },
289
290         { &hf_version,
291           { "Version", "igap.version", FT_UINT8, BASE_HEX,
292             VALS(igap_version), 0, "IGAP protocol version", HFILL }
293         },
294
295         { &hf_subtype,
296           { "Subtype", "igap.subtype", FT_UINT8, BASE_HEX,
297             VALS(igap_subtypes), 0, "Subtype", HFILL }
298         },
299
300         { &hf_challengeid,
301           { "Challenge ID", "igap.challengeid", FT_UINT8, BASE_HEX,
302             NULL, 0, "Challenge ID", HFILL }
303         },
304
305         { &hf_asize,
306           { "Account Size", "igap.asize", FT_UINT8, BASE_DEC,
307             NULL, 0, "Length of the User Account field", HFILL }
308         },
309
310         { &hf_msize,
311           { "Message Size", "igap.msize", FT_UINT8, BASE_DEC,
312             NULL, 0, "Length of the Message field", HFILL }
313         },
314
315         { &hf_account,
316           { "User Account", "igap.account", FT_STRING, BASE_NONE,
317             NULL, 0, "User account", HFILL }
318         }
319     };
320
321     static gint *ett[] = {
322         &ett_igap
323     };
324
325     proto_igap = proto_register_protocol
326         ("Internet Group membership Authentication Protocol",
327          "IGAP", "igap");
328     proto_register_field_array(proto_igap, hf, array_length(hf));
329     proto_register_subtree_array(ett, array_length(ett));
330 }