Define some fcns & vars as static...
[metze/wireshark/wip.git] / epan / dissectors / packet-rpl.c
1 /* packet-rpl.c
2  * Routines for RPL
3  * Jochen Friedrich <jochen@scram.de>
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 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <glib.h>
31 #include <epan/packet.h>
32
33 #include <epan/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         gint length, reported_length;
107
108         len = tvb_get_ntohs(tvb, 0);
109         proto_tree_add_text(tree, tvb, 0, 2, "Length: %u", len);
110
111         type = tvb_get_ntohs(tvb, 2);
112         proto_tree_add_text(tree, tvb, 2, 2, "Type: %s", 
113                 val_to_str(type, rpl_type_vals, "Unknown Type"));
114         offset = 4;
115
116         switch (type) {
117                 case 1:
118                 case 2:
119                 case 4:
120                 case 8:
121                 case 0x10:
122                 case 0x20:
123                         while (len >= offset+4) {
124                                 sublen = tvb_get_ntohs(tvb, offset);
125                                 subtyp = tvb_get_ntohs(tvb, offset+2);
126                                 ett_type = ett_rpl_unkn;
127                                 if(subtyp == 0x0004) ett_type = ett_rpl_0004;
128                                 if(subtyp == 0x0008) ett_type = ett_rpl_0008;
129                                 if(subtyp == 0x4003) ett_type = ett_rpl_4003;
130                                 if(subtyp == 0x4006) ett_type = ett_rpl_4006;
131                                 if(subtyp == 0x4007) ett_type = ett_rpl_4007;
132                                 if(subtyp == 0x4009) ett_type = ett_rpl_4009;
133                                 if(subtyp == 0x400a) ett_type = ett_rpl_400a;
134                                 if(subtyp == 0x400b) ett_type = ett_rpl_400b;
135                                 if(subtyp == 0x400c) ett_type = ett_rpl_400c;
136                                 if(subtyp == 0x4011) ett_type = ett_rpl_4011;
137                                 if(subtyp == 0x4018) ett_type = ett_rpl_4018;
138                                 if(subtyp == 0xc005) ett_type = ett_rpl_c005;
139                                 if(subtyp == 0xc014) ett_type = ett_rpl_c014;
140                                 ti = proto_tree_add_text(tree, tvb,
141                                         offset, sublen, "%s", val_to_str(subtyp, 
142                                         rpl_type_vals, "Unknown Type"));
143                                 rpl_container_tree = proto_item_add_subtree(ti, 
144                                         ett_type);
145                                 length = tvb_length_remaining(tvb, offset);
146                                 if (length > sublen)
147                                         length = sublen;
148                                 reported_length = tvb_reported_length_remaining(tvb, offset);
149                                 if (reported_length > sublen)
150                                         reported_length = sublen;
151                                 if ( length > 0) {
152                                   dissect_rpl_container(tvb_new_subset(tvb, 
153                                         offset, length, reported_length),
154                                         pinfo, rpl_container_tree);
155                                   offset += reported_length;
156                                 } else {
157                                   /* no more data, exit the loop */
158                                   offset += reported_length;
159                                   break;
160                                 }
161                         }       
162                         break;
163
164                 case 0x4003:
165                         proto_tree_add_item(tree, hf_rpl_corrval, 
166                                 tvb, offset, 4, FALSE);
167                         offset += 4;
168                         break;
169
170                 case 0x4006:
171                         proto_tree_add_item(tree, hf_rpl_lmac, 
172                                 tvb, offset, 6, FALSE);
173                         offset += 6;
174                         break;
175
176                 case 0x4007:
177                         proto_tree_add_item(tree, hf_rpl_sap, 
178                                 tvb, offset, 1, FALSE);
179                         offset ++;
180                         break;
181
182                 case 0x4009:
183                         proto_tree_add_item(tree, hf_rpl_maxframe, 
184                                 tvb, offset, 2, FALSE);
185                         offset += 2;
186                         break;
187
188                 case 0x400a:
189                         proto_tree_add_item(tree, hf_rpl_connclass, 
190                                 tvb, offset, 2, FALSE);
191                         offset += 2;
192                         break;
193
194                 case 0x400b:
195                         proto_tree_add_item(tree, hf_rpl_respval, 
196                                 tvb, offset, 1, FALSE);
197                         offset ++;
198                         break;
199
200                 case 0x400c:
201                         proto_tree_add_item(tree, hf_rpl_smac, 
202                                 tvb, offset, 6, FALSE);
203                         offset += 6;
204                         break;
205
206                 case 0x4011:
207                         proto_tree_add_item(tree, hf_rpl_sequence, 
208                                 tvb, offset, 4, FALSE);
209                         offset += 4;
210                         break;
211
212                 case 0x4018:
213                         proto_tree_add_item(tree, hf_rpl_data, 
214                                 tvb, offset, len-4, FALSE);
215                         offset += len - 4;
216                         break;
217
218                 case 0xc005:
219                         proto_tree_add_item(tree, hf_rpl_config, 
220                                 tvb, offset, 8, FALSE);
221                         offset += 8;
222                         proto_tree_add_item(tree, hf_rpl_equipment, 
223                                 tvb, offset, 2, FALSE);
224                         offset += 2;
225                         proto_tree_add_item(tree, hf_rpl_memsize, 
226                                 tvb, offset, 2, FALSE);
227                         offset += 2;
228                         proto_tree_add_item(tree, hf_rpl_bsmversion, 
229                                 tvb, offset, 2, FALSE);
230                         offset += 2;
231                         proto_tree_add_item(tree, hf_rpl_ec, 
232                                 tvb, offset, 6, FALSE);
233                         offset += 6;
234                         proto_tree_add_item(tree, hf_rpl_adapterid, 
235                                 tvb, offset, 2, FALSE);
236                         offset += 2;
237                         proto_tree_add_item(tree, hf_rpl_shortname, 
238                                 tvb, offset, 10, FALSE);
239                         offset += 10;
240                         break;
241
242                 case 0xc014:
243                         proto_tree_add_item(tree, hf_rpl_laddress, 
244                                 tvb, offset, 4, FALSE);
245                         offset += 4;
246                         proto_tree_add_item(tree, hf_rpl_xaddress, 
247                                 tvb, offset, 4, FALSE);
248                         offset += 4;
249                         proto_tree_add_item(tree, hf_rpl_flags, 
250                                 tvb, offset, 1, FALSE);
251                         offset ++;
252                         break;
253
254                 default:
255                         call_dissector(data_handle, 
256                                 tvb_new_subset_remaining(tvb, 4), pinfo,
257                                 tree);
258                         break;
259         }
260         if (tvb_reported_length(tvb) > offset)
261                 call_dissector(data_handle, 
262                         tvb_new_subset_remaining(tvb, offset), pinfo, tree);
263 }
264
265 static void
266 dissect_rpl(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
267 {
268         guint16 rpl_len, rpl_type;
269         proto_item *ti, *hidden_item;
270         proto_tree *rpl_tree;
271         tvbuff_t *next_tvb;
272
273         col_set_str(pinfo->cinfo, COL_PROTOCOL, "RPL");
274
275         rpl_len  = tvb_get_ntohs(tvb, 0);
276         rpl_type = tvb_get_ntohs(tvb, 2);
277
278         if (check_col(pinfo->cinfo, COL_INFO)) {
279                 col_set_str(pinfo->cinfo, COL_INFO,
280                     val_to_str_const(rpl_type, rpl_type_vals, "Unknown Type"));
281         }
282         if (tree) {
283                 ti = proto_tree_add_item(tree, proto_rpl, tvb, 0, 
284                         rpl_len, FALSE);
285                 rpl_tree = proto_item_add_subtree(ti, ett_rpl);
286                 hidden_item = proto_tree_add_uint(rpl_tree, hf_rpl_type, tvb, 2, 2,
287                         rpl_type);
288                 PROTO_ITEM_SET_HIDDEN(hidden_item);
289                 next_tvb = tvb_new_subset_remaining(tvb, 0);
290                 set_actual_length(next_tvb, rpl_len);
291                 dissect_rpl_container(next_tvb, pinfo, rpl_tree);
292         
293                 if (tvb_reported_length(tvb) > rpl_len)
294                         call_dissector(data_handle, 
295                                 tvb_new_subset_remaining(tvb, rpl_len), pinfo,
296                                     tree);
297         }
298 }
299
300 void
301 proto_register_rpl(void)
302 {
303         static hf_register_info hf[] = {
304                 { &hf_rpl_type,
305                         { "Type", "rpl.type", 
306                                 FT_UINT16, BASE_DEC, NULL, 0x0,
307                                 "RPL Packet Type", HFILL }},
308                 { &hf_rpl_corrval,
309                         { "Correlator Value", "rpl.corrval", 
310                                 FT_UINT32, BASE_HEX, NULL, 0x0,
311                                 "RPL Correlator Value", HFILL }},
312                 { &hf_rpl_respval,
313                         { "Response Code", "rpl.respval", 
314                                 FT_UINT8, BASE_DEC, NULL, 0x0,
315                                 "RPL Response Code", HFILL }},
316                 { &hf_rpl_maxframe,
317                         { "Maximum Frame Size", "rpl.maxframe", 
318                                 FT_UINT16, BASE_DEC, NULL, 0x0,
319                                 "RPL Maximum Frame Size", HFILL }},
320                 { &hf_rpl_connclass,
321                         { "Connection Class", "rpl.connclass", 
322                                 FT_UINT16, BASE_HEX, NULL, 0x0,
323                                 "RPL Connection Class", HFILL }},
324                 { &hf_rpl_lmac,
325                         { "Loader MAC Address", "rpl.lmac", 
326                                 FT_ETHER, BASE_NONE, NULL, 0x0,
327                                 "RPL Loader MAC Address", HFILL }},
328                 { &hf_rpl_smac,
329                         { "Set MAC Address", "rpl.smac", 
330                                 FT_ETHER, BASE_NONE, NULL, 0x0,
331                                 "RPL Set MAC Address", HFILL }},
332                 { &hf_rpl_sap,
333                         { "SAP", "rpl.sap", 
334                                 FT_UINT8, BASE_HEX, VALS(sap_vals), 0x0,
335                                 "RPL SAP", HFILL }},
336                 { &hf_rpl_equipment,
337                         { "Equipment", "rpl.equipment", 
338                                 FT_UINT16, BASE_HEX, NULL, 0x0,
339                                 "RPL Equipment - AX from INT 11h", HFILL }},
340                 { &hf_rpl_memsize,
341                         { "Memory Size", "rpl.memsize", 
342                                 FT_UINT16, BASE_DEC, NULL, 0x0,
343                                 "RPL Memory Size - AX from INT 12h MINUS 32k MINUS the Boot ROM Size", HFILL }},
344                 { &hf_rpl_bsmversion,
345                         { "BSM Version", "rpl.bsmversion", 
346                                 FT_UINT16, BASE_HEX, NULL, 0x0,
347                                 "RPL Version of BSM.obj", HFILL }},
348                 { &hf_rpl_adapterid,
349                         { "Adapter ID", "rpl.adapterid", 
350                                 FT_UINT16, BASE_HEX, NULL, 0x0,
351                                 "RPL Adapter ID", HFILL }},
352                 { &hf_rpl_shortname,
353                         { "Short Name", "rpl.shortname", 
354                                 FT_BYTES, BASE_NONE, NULL, 0x0,
355                                 "RPL BSM Short Name", HFILL }},
356                 { &hf_rpl_laddress,
357                         { "Locate Address", "rpl.laddress", 
358                                 FT_UINT32, BASE_HEX, NULL, 0x0,
359                                 "RPL Locate Address", HFILL }},
360                 { &hf_rpl_xaddress,
361                         { "XFER Address", "rpl.xaddress", 
362                                 FT_UINT32, BASE_HEX, NULL, 0x0,
363                                 "RPL Transfer Control Address", HFILL }},
364                 { &hf_rpl_sequence,
365                         { "Sequence Number", "rpl.sequence", 
366                                 FT_UINT32, BASE_HEX, NULL, 0x0,
367                                 "RPL Sequence Number", HFILL }},
368                 { &hf_rpl_config,
369                         { "Configuration", "rpl.config", 
370                                 FT_BYTES, BASE_NONE, NULL, 0x0,
371                                  "RPL Configuration", HFILL }},
372                 { &hf_rpl_flags,
373                         { "Flags", "rpl.flags", 
374                                 FT_UINT8, BASE_HEX, NULL, 0x0,
375                                 "RPL Bit Significant Option Flags", HFILL }},
376                 { &hf_rpl_data,
377                         { "Data", "rpl.data", 
378                                 FT_BYTES, BASE_NONE, NULL, 0x0,
379                                 "RPL Binary File Data", HFILL }},
380                 { &hf_rpl_ec,
381                         { "EC", "rpl.ec",
382                                 FT_BYTES, BASE_NONE, NULL, 0x0,
383                                 "RPL EC", HFILL }},
384         };
385
386         static gint *ett[] = {
387                 &ett_rpl,
388                 &ett_rpl_0004,
389                 &ett_rpl_0008,
390                 &ett_rpl_4003,
391                 &ett_rpl_4006,
392                 &ett_rpl_4007,
393                 &ett_rpl_4009,
394                 &ett_rpl_400a,
395                 &ett_rpl_400b,
396                 &ett_rpl_400c,
397                 &ett_rpl_4011,
398                 &ett_rpl_4018,
399                 &ett_rpl_c005,
400                 &ett_rpl_c014,
401                 &ett_rpl_unkn
402         };
403
404         proto_rpl = proto_register_protocol("Remote Program Load",
405             "RPL", "rpl");
406         proto_register_field_array(proto_rpl, hf, array_length(hf));
407         proto_register_subtree_array(ett, array_length(ett));
408         register_dissector("rpl", dissect_rpl, proto_rpl);
409 }
410
411 void
412 proto_reg_handoff_rpl(void)
413 {
414         dissector_handle_t rpl_handle;
415
416         data_handle = find_dissector("data");
417         rpl_handle = find_dissector("rpl");
418         dissector_add("llc.dsap", SAP_RPL, rpl_handle);
419 }