new_register_dissector -> register_dissector for dissector directory.
[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  * Wireshark - Network traffic analyzer
6  * By Gerald Combs <gerald@wireshark.org>
7  * Copyright 1998 Gerald Combs
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23
24 #include "config.h"
25
26 #include <epan/packet.h>
27
28 #include <epan/llcsaps.h>
29 #include "packet-llc.h"
30
31 void proto_register_rpl(void);
32 void proto_reg_handoff_rpl(void);
33
34 static int proto_rpl          = -1;
35
36 static int hf_rpl_type        = -1;
37 static int hf_rpl_len         = -1;
38 static int hf_rpl_corrval     = -1;
39 static int hf_rpl_respval     = -1;
40 static int hf_rpl_maxframe    = -1;
41 static int hf_rpl_connclass   = -1;
42 static int hf_rpl_lmac        = -1;
43 static int hf_rpl_smac        = -1;
44 static int hf_rpl_sap         = -1;
45 static int hf_rpl_equipment   = -1;
46 static int hf_rpl_memsize     = -1;
47 static int hf_rpl_bsmversion  = -1;
48 static int hf_rpl_adapterid   = -1;
49 static int hf_rpl_shortname   = -1;
50 static int hf_rpl_laddress    = -1;
51 static int hf_rpl_xaddress    = -1;
52 static int hf_rpl_sequence    = -1;
53 static int hf_rpl_config      = -1;
54 static int hf_rpl_flags       = -1;
55 static int hf_rpl_data        = -1;
56 static int hf_rpl_ec          = -1;
57
58 static gint ett_rpl           = -1;
59 static gint ett_rpl_0004      = -1;
60 static gint ett_rpl_0008      = -1;
61 static gint ett_rpl_4003      = -1;
62 static gint ett_rpl_4006      = -1;
63 static gint ett_rpl_4007      = -1;
64 static gint ett_rpl_4009      = -1;
65 static gint ett_rpl_400a      = -1;
66 static gint ett_rpl_400b      = -1;
67 static gint ett_rpl_400c      = -1;
68 static gint ett_rpl_4011      = -1;
69 static gint ett_rpl_4018      = -1;
70 static gint ett_rpl_c005      = -1;
71 static gint ett_rpl_c014      = -1;
72 static gint ett_rpl_unkn      = -1;
73
74 static const value_string rpl_type_vals[] = {
75         { 1,            "FIND Command" },
76         { 2,            "FOUND Frame" },
77         { 4,            "Search Vector" },
78         { 8,            "Connect Info Vector" },
79         { 0x10,         "Send File Request" },
80         { 0x20,         "File Data Response" },
81         { 0x4003,       "Correlator Vector" },
82         { 0x4006,       "Loader Address Vector" },
83         { 0x4007,       "Loader SAP Vector" },
84         { 0x4009,       "Frame Size Sub-Vector" },
85         { 0x400a,       "Connect Class Sub-Vector" },
86         { 0x400b,       "Response Correlator" },
87         { 0x400c,       "Set Address Vector" },
88         { 0x4011,       "Sequence Header" },
89         { 0x4018,       "File Data Vector" },
90         { 0xc005,       "Loader Info Sub-Vector" },
91         { 0xc014,       "Loader Header" },
92         { 0x0,  NULL }
93 };
94
95 static dissector_handle_t data_handle;
96
97 static void
98 dissect_rpl_container(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
99 {
100         guint16 len, type, sublen, subtyp;
101         proto_tree *rpl_container_tree;
102         guint16 offset;
103         gint ett_type;
104         gint length, reported_length;
105
106         len = tvb_get_ntohs(tvb, 0);
107         proto_tree_add_item(tree, hf_rpl_len, tvb, 0, 2, ENC_BIG_ENDIAN);
108
109         type = tvb_get_ntohs(tvb, 2);
110         proto_tree_add_item(tree, hf_rpl_type, tvb, 2, 2, ENC_BIG_ENDIAN);
111         offset = 4;
112
113         switch (type) {
114                 case 1:
115                 case 2:
116                 case 4:
117                 case 8:
118                 case 0x10:
119                 case 0x20:
120                         while (len >= offset+4) {
121                                 sublen = tvb_get_ntohs(tvb, offset);
122                                 subtyp = tvb_get_ntohs(tvb, offset+2);
123                                 ett_type = ett_rpl_unkn;
124                                 if(subtyp == 0x0004) ett_type = ett_rpl_0004;
125                                 if(subtyp == 0x0008) ett_type = ett_rpl_0008;
126                                 if(subtyp == 0x4003) ett_type = ett_rpl_4003;
127                                 if(subtyp == 0x4006) ett_type = ett_rpl_4006;
128                                 if(subtyp == 0x4007) ett_type = ett_rpl_4007;
129                                 if(subtyp == 0x4009) ett_type = ett_rpl_4009;
130                                 if(subtyp == 0x400a) ett_type = ett_rpl_400a;
131                                 if(subtyp == 0x400b) ett_type = ett_rpl_400b;
132                                 if(subtyp == 0x400c) ett_type = ett_rpl_400c;
133                                 if(subtyp == 0x4011) ett_type = ett_rpl_4011;
134                                 if(subtyp == 0x4018) ett_type = ett_rpl_4018;
135                                 if(subtyp == 0xc005) ett_type = ett_rpl_c005;
136                                 if(subtyp == 0xc014) ett_type = ett_rpl_c014;
137                                 rpl_container_tree = proto_tree_add_subtree(tree, tvb,
138                                         offset, sublen, ett_type, NULL,
139                                         val_to_str_const(subtyp,
140                                                 rpl_type_vals,
141                                                 "Unknown Type"));
142                                 length = tvb_captured_length_remaining(tvb, offset);
143                                 if (length > sublen)
144                                         length = sublen;
145                                 reported_length = tvb_reported_length_remaining(tvb, offset);
146                                 if (reported_length > sublen)
147                                         reported_length = sublen;
148                                 if ( length > 0) {
149                                   dissect_rpl_container(tvb_new_subset(tvb,
150                                         offset, length, reported_length),
151                                         pinfo, rpl_container_tree);
152                                   offset += reported_length;
153                                 } else {
154                                   /* no more data, exit the loop */
155                                   offset += reported_length;
156                                   break;
157                                 }
158                         }
159                         break;
160
161                 case 0x4003:
162                         proto_tree_add_item(tree, hf_rpl_corrval,
163                                 tvb, offset, 4, ENC_BIG_ENDIAN);
164                         offset += 4;
165                         break;
166
167                 case 0x4006:
168                         proto_tree_add_item(tree, hf_rpl_lmac,
169                                 tvb, offset, 6, ENC_NA);
170                         offset += 6;
171                         break;
172
173                 case 0x4007:
174                         proto_tree_add_item(tree, hf_rpl_sap,
175                                 tvb, offset, 1, ENC_BIG_ENDIAN);
176                         offset ++;
177                         break;
178
179                 case 0x4009:
180                         proto_tree_add_item(tree, hf_rpl_maxframe,
181                                 tvb, offset, 2, ENC_BIG_ENDIAN);
182                         offset += 2;
183                         break;
184
185                 case 0x400a:
186                         proto_tree_add_item(tree, hf_rpl_connclass,
187                                 tvb, offset, 2, ENC_BIG_ENDIAN);
188                         offset += 2;
189                         break;
190
191                 case 0x400b:
192                         proto_tree_add_item(tree, hf_rpl_respval,
193                                 tvb, offset, 1, ENC_BIG_ENDIAN);
194                         offset ++;
195                         break;
196
197                 case 0x400c:
198                         proto_tree_add_item(tree, hf_rpl_smac,
199                                 tvb, offset, 6, ENC_NA);
200                         offset += 6;
201                         break;
202
203                 case 0x4011:
204                         proto_tree_add_item(tree, hf_rpl_sequence,
205                                 tvb, offset, 4, ENC_BIG_ENDIAN);
206                         offset += 4;
207                         break;
208
209                 case 0x4018:
210                         proto_tree_add_item(tree, hf_rpl_data,
211                                 tvb, offset, len-4, ENC_NA);
212                         offset += len - 4;
213                         break;
214
215                 case 0xc005:
216                         proto_tree_add_item(tree, hf_rpl_config,
217                                 tvb, offset, 8, ENC_NA);
218                         offset += 8;
219                         proto_tree_add_item(tree, hf_rpl_equipment,
220                                 tvb, offset, 2, ENC_BIG_ENDIAN);
221                         offset += 2;
222                         proto_tree_add_item(tree, hf_rpl_memsize,
223                                 tvb, offset, 2, ENC_BIG_ENDIAN);
224                         offset += 2;
225                         proto_tree_add_item(tree, hf_rpl_bsmversion,
226                                 tvb, offset, 2, ENC_BIG_ENDIAN);
227                         offset += 2;
228                         proto_tree_add_item(tree, hf_rpl_ec,
229                                 tvb, offset, 6, ENC_NA);
230                         offset += 6;
231                         proto_tree_add_item(tree, hf_rpl_adapterid,
232                                 tvb, offset, 2, ENC_BIG_ENDIAN);
233                         offset += 2;
234                         proto_tree_add_item(tree, hf_rpl_shortname,
235                                 tvb, offset, 10, ENC_NA);
236                         offset += 10;
237                         break;
238
239                 case 0xc014:
240                         proto_tree_add_item(tree, hf_rpl_laddress,
241                                 tvb, offset, 4, ENC_BIG_ENDIAN);
242                         offset += 4;
243                         proto_tree_add_item(tree, hf_rpl_xaddress,
244                                 tvb, offset, 4, ENC_BIG_ENDIAN);
245                         offset += 4;
246                         proto_tree_add_item(tree, hf_rpl_flags,
247                                 tvb, offset, 1, ENC_BIG_ENDIAN);
248                         offset ++;
249                         break;
250
251                 default:
252                         call_dissector(data_handle,
253                                 tvb_new_subset_remaining(tvb, 4), pinfo,
254                                 tree);
255                         break;
256         }
257         if (tvb_reported_length(tvb) > offset)
258                 call_dissector(data_handle,
259                         tvb_new_subset_remaining(tvb, offset), pinfo, tree);
260 }
261
262 static int
263 dissect_rpl(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data _U_)
264 {
265         guint16 rpl_len, rpl_type;
266         proto_item *ti;
267         proto_tree *rpl_tree;
268         tvbuff_t *next_tvb;
269
270         col_set_str(pinfo->cinfo, COL_PROTOCOL, "RPL");
271
272         rpl_len  = tvb_get_ntohs(tvb, 0);
273         rpl_type = tvb_get_ntohs(tvb, 2);
274
275         col_set_str(pinfo->cinfo, COL_INFO,
276                     val_to_str_const(rpl_type, rpl_type_vals, "Unknown Type"));
277
278         ti = proto_tree_add_item(tree, proto_rpl, tvb, 0,
279                 rpl_len, ENC_NA);
280         rpl_tree = proto_item_add_subtree(ti, ett_rpl);
281         next_tvb = tvb_new_subset_remaining(tvb, 0);
282         set_actual_length(next_tvb, rpl_len);
283         dissect_rpl_container(next_tvb, pinfo, rpl_tree);
284
285         if (tvb_reported_length(tvb) > rpl_len)
286                 call_dissector(data_handle,
287                         tvb_new_subset_remaining(tvb, rpl_len), pinfo,
288                                 tree);
289
290         return tvb_captured_length(tvb);
291 }
292
293 void
294 proto_register_rpl(void)
295 {
296         static hf_register_info hf[] = {
297                 { &hf_rpl_type,
298                         { "Type", "rpl.type",
299                                 FT_UINT16, BASE_DEC, VALS(rpl_type_vals), 0x0,
300                                 "RPL Packet Type", HFILL }},
301                 { &hf_rpl_len,
302                         { "Length", "rpl.len",
303                                 FT_UINT16, BASE_DEC, NULL, 0x0,
304                                 "RPL Packet Length", HFILL }},
305                 { &hf_rpl_corrval,
306                         { "Correlator Value", "rpl.corrval",
307                                 FT_UINT32, BASE_HEX, NULL, 0x0,
308                                 "RPL Correlator Value", HFILL }},
309                 { &hf_rpl_respval,
310                         { "Response Code", "rpl.respval",
311                                 FT_UINT8, BASE_DEC, NULL, 0x0,
312                                 "RPL Response Code", HFILL }},
313                 { &hf_rpl_maxframe,
314                         { "Maximum Frame Size", "rpl.maxframe",
315                                 FT_UINT16, BASE_DEC, NULL, 0x0,
316                                 "RPL Maximum Frame Size", HFILL }},
317                 { &hf_rpl_connclass,
318                         { "Connection Class", "rpl.connclass",
319                                 FT_UINT16, BASE_HEX, NULL, 0x0,
320                                 "RPL Connection Class", HFILL }},
321                 { &hf_rpl_lmac,
322                         { "Loader MAC Address", "rpl.lmac",
323                                 FT_ETHER, BASE_NONE, NULL, 0x0,
324                                 "RPL Loader MAC Address", HFILL }},
325                 { &hf_rpl_smac,
326                         { "Set MAC Address", "rpl.smac",
327                                 FT_ETHER, BASE_NONE, NULL, 0x0,
328                                 "RPL Set MAC Address", HFILL }},
329                 { &hf_rpl_sap,
330                         { "SAP", "rpl.sap",
331                                 FT_UINT8, BASE_HEX, VALS(sap_vals), 0x0,
332                                 "RPL SAP", HFILL }},
333                 { &hf_rpl_equipment,
334                         { "Equipment", "rpl.equipment",
335                                 FT_UINT16, BASE_HEX, NULL, 0x0,
336                                 "RPL Equipment - AX from INT 11h", HFILL }},
337                 { &hf_rpl_memsize,
338                         { "Memory Size", "rpl.memsize",
339                                 FT_UINT16, BASE_DEC, NULL, 0x0,
340                                 "RPL Memory Size - AX from INT 12h MINUS 32k MINUS the Boot ROM Size", HFILL }},
341                 { &hf_rpl_bsmversion,
342                         { "BSM Version", "rpl.bsmversion",
343                                 FT_UINT16, BASE_HEX, NULL, 0x0,
344                                 "RPL Version of BSM.obj", HFILL }},
345                 { &hf_rpl_adapterid,
346                         { "Adapter ID", "rpl.adapterid",
347                                 FT_UINT16, BASE_HEX, NULL, 0x0,
348                                 "RPL Adapter ID", HFILL }},
349                 { &hf_rpl_shortname,
350                         { "Short Name", "rpl.shortname",
351                                 FT_BYTES, BASE_NONE, NULL, 0x0,
352                                 "RPL BSM Short Name", HFILL }},
353                 { &hf_rpl_laddress,
354                         { "Locate Address", "rpl.laddress",
355                                 FT_UINT32, BASE_HEX, NULL, 0x0,
356                                 "RPL Locate Address", HFILL }},
357                 { &hf_rpl_xaddress,
358                         { "XFER Address", "rpl.xaddress",
359                                 FT_UINT32, BASE_HEX, NULL, 0x0,
360                                 "RPL Transfer Control Address", HFILL }},
361                 { &hf_rpl_sequence,
362                         { "Sequence Number", "rpl.sequence",
363                                 FT_UINT32, BASE_HEX, NULL, 0x0,
364                                 "RPL Sequence Number", HFILL }},
365                 { &hf_rpl_config,
366                         { "Configuration", "rpl.config",
367                                 FT_BYTES, BASE_NONE, NULL, 0x0,
368                                  "RPL Configuration", HFILL }},
369                 { &hf_rpl_flags,
370                         { "Flags", "rpl.flags",
371                                 FT_UINT8, BASE_HEX, NULL, 0x0,
372                                 "RPL Bit Significant Option Flags", HFILL }},
373                 { &hf_rpl_data,
374                         { "Data", "rpl.data",
375                                 FT_BYTES, BASE_NONE, NULL, 0x0,
376                                 "RPL Binary File Data", HFILL }},
377                 { &hf_rpl_ec,
378                         { "EC", "rpl.ec",
379                                 FT_BYTES, BASE_NONE, NULL, 0x0,
380                                 "RPL EC", HFILL }},
381         };
382
383         static gint *ett[] = {
384                 &ett_rpl,
385                 &ett_rpl_0004,
386                 &ett_rpl_0008,
387                 &ett_rpl_4003,
388                 &ett_rpl_4006,
389                 &ett_rpl_4007,
390                 &ett_rpl_4009,
391                 &ett_rpl_400a,
392                 &ett_rpl_400b,
393                 &ett_rpl_400c,
394                 &ett_rpl_4011,
395                 &ett_rpl_4018,
396                 &ett_rpl_c005,
397                 &ett_rpl_c014,
398                 &ett_rpl_unkn
399         };
400
401         proto_rpl = proto_register_protocol("Remote Program Load",
402             "RPL", "rpl");
403         proto_register_field_array(proto_rpl, hf, array_length(hf));
404         proto_register_subtree_array(ett, array_length(ett));
405         register_dissector("rpl", dissect_rpl, proto_rpl);
406 }
407
408 void
409 proto_reg_handoff_rpl(void)
410 {
411         dissector_handle_t rpl_handle;
412
413         data_handle = find_dissector("data");
414         rpl_handle = find_dissector("rpl");
415         dissector_add_uint("llc.dsap", SAP_RPL, rpl_handle);
416 }
417
418 /*
419  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
420  *
421  * Local variables:
422  * c-basic-offset: 8
423  * tab-width: 8
424  * indent-tabs-mode: t
425  * End:
426  *
427  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
428  * :indentSize=8:tabSize=8:noTabs=false:
429  */