Call subdissectors even if we're not building a protocol tree.
[obnox/wireshark/wip.git] / packet-rpl.c
1 /* packet-rpl.c
2  * Routines for RPL
3  * Jochen Friedrich <jochen@scram.de>
4  *
5  * $Id: packet-rpl.c,v 1.5 2003/03/02 21:52:19 guy Exp $
6  *
7  * Ethereal - Network traffic analyzer
8  * By Gerald Combs <gerald@ethereal.com>
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 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <glib.h>
31 #include <epan/packet.h>
32
33 #include "llcsaps.h"
34 #include "packet-llc.h"
35
36 static int proto_rpl          = -1;
37
38 static int hf_rpl_type        = -1;
39 static int hf_rpl_corrval     = -1;
40 static int hf_rpl_respval     = -1;
41 static int hf_rpl_maxframe    = -1;
42 static int hf_rpl_connclass   = -1;
43 static int hf_rpl_lmac        = -1;
44 static int hf_rpl_smac        = -1;
45 static int hf_rpl_sap         = -1;
46 static int hf_rpl_equipment   = -1;
47 static int hf_rpl_memsize     = -1;
48 static int hf_rpl_bsmversion  = -1;
49 static int hf_rpl_adapterid   = -1;
50 static int hf_rpl_shortname   = -1;
51 static int hf_rpl_laddress    = -1;
52 static int hf_rpl_xaddress    = -1;
53 static int hf_rpl_sequence    = -1;
54 static int hf_rpl_config      = -1;
55 static int hf_rpl_flags       = -1;
56 static int hf_rpl_data        = -1;
57 static int hf_rpl_ec          = -1;
58
59 static gint ett_rpl           = -1;
60 static gint ett_rpl_0004      = -1;
61 static gint ett_rpl_0008      = -1;
62 static gint ett_rpl_4003      = -1;
63 static gint ett_rpl_4006      = -1;
64 static gint ett_rpl_4007      = -1;
65 static gint ett_rpl_4009      = -1;
66 static gint ett_rpl_400a      = -1;
67 static gint ett_rpl_400b      = -1;
68 static gint ett_rpl_400c      = -1;
69 static gint ett_rpl_4011      = -1;
70 static gint ett_rpl_4018      = -1;
71 static gint ett_rpl_c005      = -1;
72 static gint ett_rpl_c014      = -1;
73 static gint ett_rpl_unkn      = -1;
74
75 static const value_string rpl_type_vals[] = {
76         { 1,            "FIND Command" },
77         { 2,            "FOUND Frame" },
78         { 4,            "Search Vector" },
79         { 8,            "Connect Info Vector" },
80         { 0x10,         "Send File Request" },
81         { 0x20,         "File Data Response" },
82         { 0x4003,       "Correlator Vector" },
83         { 0x4006,       "Loader Address Vector" },
84         { 0x4007,       "Loader SAP Vector" },
85         { 0x4009,       "Frame Size Sub-Vector" },
86         { 0x400a,       "Connect Class Sub-Vector" },
87         { 0x400b,       "Response Correlator" },
88         { 0x400c,       "Set Address Vector" },
89         { 0x4011,       "Sequence Header" },
90         { 0x4018,       "File Data Vector" },
91         { 0xc005,       "Loader Info Sub-Vector" },
92         { 0xc014,       "Loader Header" },
93         { 0x0,  NULL }
94 };
95
96 static dissector_handle_t data_handle;
97
98 static void
99 dissect_rpl_container(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
100 {
101         guint16 len, type, sublen, subtyp;
102         proto_item *ti;
103         proto_tree *rpl_container_tree;
104         guint16 offset;
105         gint ett_type;
106
107         len = tvb_get_ntohs(tvb, 0);
108         proto_tree_add_text(tree, tvb, 0, 2, "Length: %u", len);
109
110         type = tvb_get_ntohs(tvb, 2);
111         proto_tree_add_text(tree, tvb, 2, 2, "Type: %s", 
112                 val_to_str(type, rpl_type_vals, "Unknown Type"));
113         offset = 4;
114
115         switch (type) {
116                 case 1:
117                 case 2:
118                 case 4:
119                 case 8:
120                 case 0x10:
121                 case 0x20:
122                         while (len >= offset+4) {
123                                 sublen = tvb_get_ntohs(tvb, offset);
124                                 subtyp = tvb_get_ntohs(tvb, offset+2);
125                                 ett_type = ett_rpl_unkn;
126                                 if(subtyp == 0x0004) ett_type = ett_rpl_0004;
127                                 if(subtyp == 0x0008) ett_type = ett_rpl_0008;
128                                 if(subtyp == 0x4003) ett_type = ett_rpl_4003;
129                                 if(subtyp == 0x4006) ett_type = ett_rpl_4006;
130                                 if(subtyp == 0x4007) ett_type = ett_rpl_4007;
131                                 if(subtyp == 0x4009) ett_type = ett_rpl_4009;
132                                 if(subtyp == 0x400a) ett_type = ett_rpl_400a;
133                                 if(subtyp == 0x400b) ett_type = ett_rpl_400b;
134                                 if(subtyp == 0x400c) ett_type = ett_rpl_400c;
135                                 if(subtyp == 0x4011) ett_type = ett_rpl_4011;
136                                 if(subtyp == 0x4018) ett_type = ett_rpl_4018;
137                                 if(subtyp == 0xc005) ett_type = ett_rpl_c005;
138                                 if(subtyp == 0xc014) ett_type = ett_rpl_c014;
139                                 ti = proto_tree_add_text(tree, tvb,
140                                         offset, sublen, val_to_str(subtyp, 
141                                         rpl_type_vals, "Unknown Type"));
142                                 rpl_container_tree = proto_item_add_subtree(ti, 
143                                         ett_type);
144                                 dissect_rpl_container(tvb_new_subset(tvb, 
145                                         offset, sublen, -1), pinfo, 
146                                         rpl_container_tree);
147
148                                 offset += sublen;
149                         }       
150                         break;
151
152                 case 0x4003:
153                         proto_tree_add_item(tree, hf_rpl_corrval, 
154                                 tvb, offset, 4, FALSE);
155                         offset += 4;
156                         break;
157
158                 case 0x4006:
159                         proto_tree_add_item(tree, hf_rpl_lmac, 
160                                 tvb, offset, 6, FALSE);
161                         offset += 6;
162                         break;
163
164                 case 0x4007:
165                         proto_tree_add_item(tree, hf_rpl_sap, 
166                                 tvb, offset, 1, FALSE);
167                         offset ++;
168                         break;
169
170                 case 0x4009:
171                         proto_tree_add_item(tree, hf_rpl_maxframe, 
172                                 tvb, offset, 2, FALSE);
173                         offset += 2;
174                         break;
175
176                 case 0x400a:
177                         proto_tree_add_item(tree, hf_rpl_connclass, 
178                                 tvb, offset, 2, FALSE);
179                         offset += 2;
180                         break;
181
182                 case 0x400b:
183                         proto_tree_add_item(tree, hf_rpl_respval, 
184                                 tvb, offset, 1, FALSE);
185                         offset ++;
186                         break;
187
188                 case 0x400c:
189                         proto_tree_add_item(tree, hf_rpl_smac, 
190                                 tvb, offset, 6, FALSE);
191                         offset += 6;
192                         break;
193
194                 case 0x4011:
195                         proto_tree_add_item(tree, hf_rpl_sequence, 
196                                 tvb, offset, 4, FALSE);
197                         offset += 4;
198                         break;
199
200                 case 0x4018:
201                         proto_tree_add_item(tree, hf_rpl_data, 
202                                 tvb, offset, len-4, FALSE);
203                         offset += len - 4;
204                         break;
205
206                 case 0xc005:
207                         proto_tree_add_item(tree, hf_rpl_config, 
208                                 tvb, offset, 8, FALSE);
209                         offset += 8;
210                         proto_tree_add_item(tree, hf_rpl_equipment, 
211                                 tvb, offset, 2, FALSE);
212                         offset += 2;
213                         proto_tree_add_item(tree, hf_rpl_memsize, 
214                                 tvb, offset, 2, FALSE);
215                         offset += 2;
216                         proto_tree_add_item(tree, hf_rpl_bsmversion, 
217                                 tvb, offset, 2, FALSE);
218                         offset += 2;
219                         proto_tree_add_item(tree, hf_rpl_ec, 
220                                 tvb, offset, 6, FALSE);
221                         offset += 6;
222                         proto_tree_add_item(tree, hf_rpl_adapterid, 
223                                 tvb, offset, 2, FALSE);
224                         offset += 2;
225                         proto_tree_add_item(tree, hf_rpl_shortname, 
226                                 tvb, offset, 10, FALSE);
227                         offset += 10;
228                         break;
229
230                 case 0xc014:
231                         proto_tree_add_item(tree, hf_rpl_laddress, 
232                                 tvb, offset, 4, FALSE);
233                         offset += 4;
234                         proto_tree_add_item(tree, hf_rpl_xaddress, 
235                                 tvb, offset, 4, FALSE);
236                         offset += 4;
237                         proto_tree_add_item(tree, hf_rpl_flags, 
238                                 tvb, offset, 1, FALSE);
239                         offset ++;
240                         break;
241
242                 default:
243                         call_dissector(data_handle, 
244                                 tvb_new_subset(tvb, 4, -1, -1), pinfo,
245                                 tree);
246                         break;
247         }
248         if (tvb_reported_length(tvb) > offset)
249                 call_dissector(data_handle, 
250                         tvb_new_subset(tvb, offset, -1, -1), pinfo, tree);
251 }
252
253 static void
254 dissect_rpl(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
255 {
256         guint16 rpl_len, rpl_type;
257         proto_item *ti;
258         proto_tree *rpl_tree;
259
260         if (check_col(pinfo->cinfo, COL_PROTOCOL))
261                 col_set_str(pinfo->cinfo, COL_PROTOCOL, "RPL");
262
263         rpl_len  = tvb_get_ntohs(tvb, 0);
264         rpl_type = tvb_get_ntohs(tvb, 2);
265
266         if (check_col(pinfo->cinfo, COL_INFO)) {
267                 col_set_str(pinfo->cinfo, COL_INFO,
268                 val_to_str(rpl_type, rpl_type_vals, "Unknown Type"));
269         }
270         if (tree) {
271                 ti = proto_tree_add_item(tree, proto_rpl, tvb, 0, 
272                         rpl_len, FALSE);
273                 rpl_tree = proto_item_add_subtree(ti, ett_rpl);
274                 proto_tree_add_uint_hidden(rpl_tree, hf_rpl_type, tvb, 2, 2,
275                         rpl_type);
276                 dissect_rpl_container(tvb_new_subset(tvb, 0, rpl_len, -1),
277                         pinfo, rpl_tree);
278         
279                 if (tvb_reported_length(tvb) > rpl_len)
280                         call_dissector(data_handle, 
281                                 tvb_new_subset(tvb, rpl_len, -1, -1), pinfo,
282                                 tree);
283         }
284 }
285
286 void
287 proto_register_rpl(void)
288 {
289         static hf_register_info hf[] = {
290                 { &hf_rpl_type,
291                         { "Type", "rpl.type", 
292                                 FT_UINT16, BASE_DEC, NULL, 0x0,
293                                 "RPL Packet Type", HFILL }},
294                 { &hf_rpl_corrval,
295                         { "Correlator Value", "rpl.corrval", 
296                                 FT_UINT32, BASE_HEX, NULL, 0x0,
297                                 "RPL Correlator Value", HFILL }},
298                 { &hf_rpl_respval,
299                         { "Response Code", "rpl.respval", 
300                                 FT_UINT8, BASE_DEC, NULL, 0x0,
301                                 "RPL Response Code", HFILL }},
302                 { &hf_rpl_maxframe,
303                         { "Maximum Frame Size", "rpl.maxframe", 
304                                 FT_UINT16, BASE_DEC, NULL, 0x0,
305                                 "RPL Maximum Frame Size", HFILL }},
306                 { &hf_rpl_connclass,
307                         { "Connection Class", "rpl.connclass", 
308                                 FT_UINT16, BASE_HEX, NULL, 0x0,
309                                 "RPL Connection Class", HFILL }},
310                 { &hf_rpl_lmac,
311                         { "Loader MAC Address", "rpl.lmac", 
312                                 FT_ETHER, BASE_NONE, NULL, 0x0,
313                                 "RPL Loader MAC Address", HFILL }},
314                 { &hf_rpl_smac,
315                         { "Set MAC Address", "rpl.smac", 
316                                 FT_ETHER, BASE_NONE, NULL, 0x0,
317                                 "RPL Set MAC Address", HFILL }},
318                 { &hf_rpl_sap,
319                         { "SAP", "rpl.sap", 
320                                 FT_UINT8, BASE_HEX, VALS(sap_vals), 0x0,
321                                 "RPL SAP", HFILL }},
322                 { &hf_rpl_equipment,
323                         { "Equipment", "rpl.equipment", 
324                                 FT_UINT16, BASE_HEX, NULL, 0x0,
325                                 "RPL Equipment - AX from INT 11h", HFILL }},
326                 { &hf_rpl_memsize,
327                         { "Memory Size", "rpl.memsize", 
328                                 FT_UINT16, BASE_DEC, NULL, 0x0,
329                                 "RPL Memory Size - AX from INT 12h MINUS 32k"
330                                 " MINUS the Boot ROM Size", HFILL }},
331                 { &hf_rpl_bsmversion,
332                         { "BSM Version", "rpl.bsmversion", 
333                                 FT_UINT16, BASE_HEX, NULL, 0x0,
334                                 "RPL Version of BSM.obj", HFILL }},
335                 { &hf_rpl_adapterid,
336                         { "Adapter ID", "rpl.adapterid", 
337                                 FT_UINT16, BASE_HEX, NULL, 0x0,
338                                 "RPL Adapter ID", HFILL }},
339                 { &hf_rpl_shortname,
340                         { "Short Name", "rpl.shortname", 
341                                 FT_BYTES, BASE_HEX, NULL, 0x0,
342                                 "RPL BSM Short Name", HFILL }},
343                 { &hf_rpl_laddress,
344                         { "Locate Address", "rpl.laddress", 
345                                 FT_UINT32, BASE_HEX, NULL, 0x0,
346                                 "RPL Locate Address", HFILL }},
347                 { &hf_rpl_xaddress,
348                         { "XFER Address", "rpl.xaddress", 
349                                 FT_UINT32, BASE_HEX, NULL, 0x0,
350                                 "RPL Transfer Control Address", HFILL }},
351                 { &hf_rpl_sequence,
352                         { "Sequence Number", "rpl.sequence", 
353                                 FT_UINT32, BASE_HEX, NULL, 0x0,
354                                 "RPL Sequence Number", HFILL }},
355                 { &hf_rpl_config,
356                         { "Configuration", "rpl.config", 
357                                 FT_BYTES, BASE_HEX, NULL, 0x0,
358                                  "RPL Configuration", HFILL }},
359                 { &hf_rpl_flags,
360                         { "Flags", "rpl.flags", 
361                                 FT_UINT8, BASE_HEX, NULL, 0x0,
362                                 "RPL Bit Significant Option Flags", HFILL }},
363                 { &hf_rpl_data,
364                         { "Data", "rpl.data", 
365                                 FT_BYTES, BASE_HEX, NULL, 0x0,
366                                 "RPL Binary File Data", HFILL }},
367                 { &hf_rpl_ec,
368                         { "EC", "rpl.ec",
369                                 FT_BYTES, BASE_HEX, NULL, 0x0,
370                                 "RPL EC", HFILL }},
371         };
372
373         static gint *ett[] = {
374                 &ett_rpl,
375                 &ett_rpl_0004,
376                 &ett_rpl_0008,
377                 &ett_rpl_4003,
378                 &ett_rpl_4006,
379                 &ett_rpl_4007,
380                 &ett_rpl_4009,
381                 &ett_rpl_400a,
382                 &ett_rpl_400b,
383                 &ett_rpl_400c,
384                 &ett_rpl_4011,
385                 &ett_rpl_4018,
386                 &ett_rpl_c005,
387                 &ett_rpl_c014,
388                 &ett_rpl_unkn
389         };
390
391         proto_rpl = proto_register_protocol("Remote Program Load",
392             "RPL", "rpl");
393         proto_register_field_array(proto_rpl, hf, array_length(hf));
394         proto_register_subtree_array(ett, array_length(ett));
395         register_dissector("rpl", dissect_rpl, proto_rpl);
396 }
397
398 void
399 proto_reg_handoff_rpl(void)
400 {
401         dissector_handle_t rpl_handle;
402
403         data_handle = find_dissector("data");
404         rpl_handle = find_dissector("rpl");
405         dissector_add("llc.dsap", SAP_RPL, rpl_handle);
406 }