Fixup: tvb_get_string(z) -> tvb_get_string(z)_enc
[metze/wireshark/wip.git] / epan / dissectors / packet-olsr.c
1 /* packet-olsr.c
2  * Routines for OLSR (IPv4 & IPv6 compatible) RFC parsing
3  * Compatible with RFC-compliant OLSR implementations such as
4  * NRLOLSRD (http://pf.itd.nrl.navy.mil/projects/olsr/).
5  * Parser created by Aaron Woo <woo@itd.nrl.navy.mil> of
6  * the Naval Research Laboratory
7  * Currently maintained by Jeff Weston <weston@itd.nrl.navy.mil>.
8  *
9  * Updated to Olsr.org and NRLOLSR packages by Henning Rogge <rogge@fgan.de>
10  * http://www.ietf.org/rfc/rfc3626.txt
11  *
12  * Wireshark - Network traffic analyzer
13  * By Gerald Combs <gerald@wireshark.org>
14  * Copyright 1998 Gerald Combs
15  *
16  * This program is free software; you can redistribute it and/or
17  * modify it under the terms of the GNU General Public License
18  * as published by the Free Software Foundation; either version 2
19  * of the License, or (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, write to the Free Software
28  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29  */
30
31 #include "config.h"
32
33 #include <string.h>
34
35 #include <glib.h>
36
37 #include <epan/packet.h>
38 #include <epan/prefs.h>
39 #include <epan/expert.h>
40 #include <epan/to_str.h>
41
42 void proto_register_olsr(void);
43 void proto_reg_handoff_olsr(void);
44
45 #define UDP_PORT_OLSR   698
46 #define HELLO   1
47 #define TC      2
48 #define MID     3
49 #define HNA     4
50
51 #define OLSR_ORG_NAMESERVICE 130
52 #define OLSR_ORG_LQ_HELLO    201
53 #define OLSR_ORG_LQ_TC       202
54 #define NRLOLSR_TC_EXTRA     241
55
56 /* Initialize the protocol and registered fields */
57 static int proto_olsr = -1;
58
59 static int hf_olsr_packet_len = -1;
60 static int hf_olsr_packet_seq_num = -1;
61
62 static int hf_olsr_message = -1;
63 static int hf_olsr_message_type = -1;
64 static int hf_olsr_vtime = -1;
65 static int hf_olsr_message_size = -1;
66 static int hf_olsr_ttl = -1;
67 static int hf_olsr_hop_count = -1;
68 static int hf_olsr_message_seq_num = -1;
69
70 static int hf_olsr_htime = -1;
71 static int hf_olsr_willingness = -1;
72
73 static int hf_olsr_link_type = -1;
74 static int hf_olsr_link_message_size = -1;
75 static int hf_olsr_ansn = -1;
76
77 static int hf_olsr_neighbor = -1;
78 static int hf_olsr_origin_addr = -1;
79 static int hf_olsr_neighbor_addr = -1;
80 static int hf_olsr_interface_addr = -1;
81 static int hf_olsr_netmask = -1;
82 static int hf_olsr_network_addr = -1;
83
84 /* static int hf_olsr_neighbor6 = -1; */
85 static int hf_olsr_origin6_addr = -1;
86 static int hf_olsr_neighbor6_addr = -1;
87 static int hf_olsr_interface6_addr = -1;
88 static int hf_olsr_netmask6 = -1;
89 static int hf_olsr_network6_addr = -1;
90
91 static int hf_olsrorg_lq = -1;
92 static int hf_olsrorg_nlq = -1;
93 static int hf_nrlolsr_f1 = -1;
94 static int hf_nrlolsr_f2 = -1;
95
96 static int hf_olsrorg_ns_version = -1;
97 static int hf_olsrorg_ns_count = -1;
98
99 static int hf_olsrorg_ns = -1;
100 static int hf_olsrorg_ns_type = -1;
101 static int hf_olsrorg_ns_length = -1;
102 static int hf_olsrorg_ns_ip = -1;
103 static int hf_olsrorg_ns_ip6 = -1;
104 static int hf_olsrorg_ns_content = -1;
105
106 static int hf_olsr_data = -1;
107
108 /* Initialize the subtree pointers*/
109 static gint ett_olsr = -1;
110 static gint ett_olsr_message[G_MAXUINT8 + 1];
111 static gint ett_olsr_message_linktype = -1;
112 static gint ett_olsr_message_neigh = -1;
113 static gint ett_olsr_message_neigh6 = -1;
114 static gint ett_olsr_message_ns = -1;
115
116 static expert_field ei_olsr_not_enough_bytes = EI_INIT;
117 static expert_field ei_olsrorg_ns_version = EI_INIT;
118 static expert_field ei_olsr_data_misaligned = EI_INIT;
119
120 static const value_string message_type_vals[] = {
121     { HELLO,                "HELLO" },
122     { TC,                   "TC" },
123     { MID,                  "MID" },
124     { HNA,                  "HNA" },
125     { OLSR_ORG_LQ_HELLO,    "HELLO (LQ, olsr.org)" },
126     { OLSR_ORG_LQ_TC,       "TC (LQ, olsr.org)" },
127     { OLSR_ORG_NAMESERVICE, "Nameservice (olsr.org)" },
128     { NRLOLSR_TC_EXTRA,     "TC (LQ, nrlolsr)" },
129     { 0, NULL }
130 };
131
132 static const value_string link_type_vals[] = {
133     {  0, "Unspecified Link" },
134     {  1, "Asymmetric Link" },
135     {  3, "Lost Link" },
136     {  5, "Pending Link" },
137     {  6, "Symmetric Link" },
138     { 10, "MPR Link" },
139     { 0, NULL }
140 };
141
142 static const value_string willingness_type_vals[] = {
143     { 0, "never" },
144     { 7, "always" },
145     { 0, NULL }
146 };
147
148 static const value_string nameservice_type_vals[] = {
149     { 0, "HOST" },
150     { 1, "FORWARDER" },
151     { 2, "SERVICE" },
152     { 3, "LATLON" },
153     { 0, NULL }
154 };
155
156 static gboolean global_olsr_olsrorg = TRUE;
157 static gboolean global_olsr_nrlolsr = TRUE;
158
159 static double getOlsrTime(guint8 timeval) {
160   int high_bits, low_bits;
161
162   high_bits = ((timeval & 0xF0) >> 4);
163   low_bits  = (timeval & 0x0F);
164   return ((G_GUINT64_CONSTANT(1) << low_bits) / 16.0) * (1 + (high_bits / 16.0));
165 }
166
167 /*------------------------- TC Dissecting Code-------------------------*/
168 static int dissect_olsr_tc(tvbuff_t *tvb, packet_info *pinfo, proto_tree *olsr_tree, int offset, int message_end) {
169   if (message_end - offset < 4) {
170     proto_tree_add_expert_format(olsr_tree, pinfo, &ei_olsr_not_enough_bytes, tvb, offset, message_end - offset,
171         "Not enough bytes for TC");
172     return message_end;
173   }
174
175   proto_tree_add_item(olsr_tree, hf_olsr_ansn, tvb, offset, 2, ENC_BIG_ENDIAN);
176   offset += 4;
177
178   while (offset < message_end) {
179     if (message_end - offset < pinfo->src.len) {
180       proto_tree_add_expert_format(olsr_tree, pinfo, &ei_olsr_not_enough_bytes, tvb, offset, message_end - offset,
181           "Not enough bytes for last neighbor");
182       return message_end;
183     }
184     if (pinfo->src.type == AT_IPv4) {
185       proto_tree_add_item(olsr_tree, hf_olsr_neighbor_addr, tvb, offset, 4, ENC_BIG_ENDIAN);
186       offset += 4;
187     } else if (pinfo->src.type == AT_IPv6) {
188       proto_tree_add_item(olsr_tree, hf_olsr_neighbor6_addr, tvb, offset, 16, ENC_NA);
189       offset += 16;
190     } else {
191       break; /* unknown address type should be handled in dissect_olsr, just be sure */
192     }
193   }
194   return message_end;
195 }
196
197 static int dissect_olsrorg_lq_tc(tvbuff_t *tvb, packet_info *pinfo, proto_tree *olsr_tree, int offset, int message_end) {
198
199   if (message_end - offset < 4) {
200     proto_tree_add_expert_format(olsr_tree, pinfo, &ei_olsr_not_enough_bytes, tvb, offset, message_end - offset,
201         "Not enough bytes for Olsr.org LQ-TC");
202     return message_end;
203   }
204   proto_tree_add_item(olsr_tree, hf_olsr_ansn, tvb, offset, 2, ENC_BIG_ENDIAN);
205   offset += 4;
206
207   while (offset < message_end) {
208     proto_item *address_group;
209     proto_tree *address_tree;
210     guint8 lq, nlq;
211
212     if (pinfo->src.type == AT_IPv4) {
213       if (message_end - offset < 8) {
214         proto_tree_add_expert_format(olsr_tree, pinfo, &ei_olsr_not_enough_bytes, tvb, offset, message_end - offset,
215             "Not enough bytes for last entry (need 8 bytes)");
216         return message_end;
217       }
218       lq = tvb_get_guint8(tvb, offset + 4);
219       nlq = tvb_get_guint8(tvb, offset + 5);
220
221       address_group = proto_tree_add_bytes_format_value(olsr_tree, hf_olsr_neighbor, tvb, offset, 8,
222           NULL, "%s (%d/%d)", tvb_ip_to_str(tvb, offset), lq, nlq);
223
224       address_tree = proto_item_add_subtree(address_group, ett_olsr_message_neigh);
225
226       proto_tree_add_item(address_tree, hf_olsr_neighbor_addr, tvb, offset, 4, ENC_BIG_ENDIAN);
227       offset += 4;
228     } else if (pinfo->src.type == AT_IPv6) {
229       if (message_end - offset < 20) {
230         proto_tree_add_expert_format(olsr_tree, pinfo, &ei_olsr_not_enough_bytes, tvb, offset, message_end - offset,
231             "Not enough bytes for last entry (need 20 bytes)");
232         return message_end;
233       }
234       lq = tvb_get_guint8(tvb, offset + 16);
235       nlq = tvb_get_guint8(tvb, offset + 17);
236
237       address_group = proto_tree_add_bytes_format_value(olsr_tree, hf_olsr_neighbor, tvb, offset, 20,
238           NULL, "%s (%d/%d)", tvb_ip6_to_str(tvb, offset), lq, nlq);
239
240       address_tree = proto_item_add_subtree(address_group, ett_olsr_message_neigh);
241
242       proto_tree_add_item(address_tree, hf_olsr_neighbor6_addr, tvb, offset, 16, ENC_NA);
243       offset += 16;
244     } else {
245       break; /* unknown address type should be handled in dissect_olsr, just be sure */
246     }
247
248     proto_tree_add_item(address_tree, hf_olsrorg_lq, tvb, offset++, 1, ENC_BIG_ENDIAN);
249     proto_tree_add_item(address_tree, hf_olsrorg_nlq, tvb, offset++, 1, ENC_BIG_ENDIAN);
250     offset += 2;
251   }
252   return message_end;
253 }
254
255 static int dissect_nrlolsr_tc(tvbuff_t *tvb, packet_info *pinfo, proto_tree *olsr_tree, int offset, int message_end) {
256   int field1Ptr, field2Ptr, saneEnd;
257   if (message_end - offset < 4) {
258     proto_tree_add_expert_format(olsr_tree, pinfo, &ei_olsr_not_enough_bytes, tvb, offset, message_end - offset,
259         "Not enough bytes for NRLOLSR LQ-TC");
260     return message_end;
261   }
262
263   proto_tree_add_item(olsr_tree, hf_olsr_ansn, tvb, offset, 2, ENC_BIG_ENDIAN);
264   offset += 4;
265
266   field1Ptr = offset + (message_end - offset) / (pinfo->src.len + 2) * (pinfo->src.len);
267   field2Ptr = offset + (message_end - offset) / (pinfo->src.len + 2) * (pinfo->src.len + 1);
268
269   saneEnd = message_end - ((message_end - offset) % (pinfo->src.len + 2));
270   while (field2Ptr < saneEnd) {
271     if (pinfo->src.type == AT_IPv4) {
272       proto_tree_add_item(olsr_tree, hf_olsr_neighbor_addr, tvb, offset, 4, ENC_BIG_ENDIAN);
273       offset += 4;
274     } else if (pinfo->src.type == AT_IPv6) {
275       proto_tree_add_item(olsr_tree, hf_olsr_neighbor6_addr, tvb, offset, 16, ENC_NA);
276       offset += 16;
277     } else {
278       break; /* unknown address type should be handled in dissect_olsr, just be sure */
279     }
280
281     proto_tree_add_item(olsr_tree, hf_nrlolsr_f1, tvb, field1Ptr++, 1, ENC_BIG_ENDIAN);
282     proto_tree_add_item(olsr_tree, hf_nrlolsr_f2, tvb, field2Ptr++, 1, ENC_BIG_ENDIAN);
283   }
284   return message_end;
285 }
286
287 /*------------------------- Hello Dissecting Code-------------------------*/
288 static int dissect_olsr_hello(tvbuff_t *tvb, packet_info *pinfo, proto_tree *olsr_tree, int offset, int message_end,
289     int(*handleNeighbors)(tvbuff_t *, packet_info *, proto_tree *, int, int)) {
290   double      hTime;
291   proto_item *ti;
292   proto_tree *link_type_tree;
293
294   guint16     message_size = 0;
295
296   if (message_end - offset < 4) {
297     proto_tree_add_expert_format(olsr_tree, pinfo, &ei_olsr_not_enough_bytes, tvb, offset, message_end - offset,
298         "Not enough bytes for Hello");
299     return message_end;
300   }
301
302   offset += 2;
303
304   /*---------------------Dissect Hello Emission Invertal-------------------*/
305   hTime = getOlsrTime(tvb_get_guint8(tvb, offset));
306   proto_tree_add_double_format_value(olsr_tree, hf_olsr_htime, tvb, offset, 1, hTime,
307       "%.3f (in seconds)", hTime);
308   offset += 1;
309
310   /*-------------------------Dissect Willingness---------------------------*/
311   proto_tree_add_item(olsr_tree, hf_olsr_willingness, tvb, offset++, 1, ENC_BIG_ENDIAN);
312
313   while (offset < message_end) {
314     if (message_end - offset < 4) {
315       proto_tree_add_expert_format(olsr_tree, pinfo, &ei_olsr_not_enough_bytes, tvb, offset, message_end - offset,
316           "Not enough bytes for last Hello entry");
317       return message_end;
318     }
319
320     /*------------------------------Dissect Link Type---------------------------------- */
321     ti = proto_tree_add_item(olsr_tree, hf_olsr_link_type, tvb, offset++, 1, ENC_BIG_ENDIAN);
322     link_type_tree = proto_item_add_subtree(ti, ett_olsr_message_linktype);
323
324     /* reserved byte */
325     offset += 1;
326
327     /*----------------------Dissect Link Message Size--------------------------*/
328     message_size = tvb_get_ntohs(tvb, offset);
329     ti = proto_tree_add_item(link_type_tree, hf_olsr_link_message_size, tvb, offset, 2, ENC_BIG_ENDIAN);
330     offset += 2;
331
332     if (message_size < 4) {
333       proto_item_append_string(ti, "(too short, must be >= 4)");
334       return message_end;
335     }
336     offset = handleNeighbors(tvb, pinfo, link_type_tree, offset, offset + message_size - 4);
337   }
338   return message_end;
339 }
340
341 static int handle_olsr_hello_rfc(tvbuff_t *tvb, packet_info *pinfo, proto_tree *olsr_tree, int offset,
342     int link_message_end) {
343   /*-------------------Dissect Neighbor Addresses--------------------*/
344   while (offset < link_message_end) {
345     if (link_message_end - offset < pinfo->src.len) {
346       proto_tree_add_expert_format(olsr_tree, pinfo, &ei_olsr_not_enough_bytes, tvb, offset, link_message_end - offset,
347           "Not enough bytes for last Hello entry");
348       return link_message_end;
349     }
350     if (pinfo->src.type == AT_IPv4) {
351       proto_tree_add_item(olsr_tree, hf_olsr_neighbor_addr, tvb, offset, 4, ENC_BIG_ENDIAN);
352       offset += 4;
353     } else if (pinfo->src.type == AT_IPv6) {
354       proto_tree_add_item(olsr_tree, hf_olsr_neighbor6_addr, tvb, offset, 16, ENC_NA);
355       offset += 16;
356     } else {
357       break; /* unknown address type should be handled in dissect_olsr, just be sure */
358     }
359   } /* end while */
360   return link_message_end;
361 }
362
363 static int handle_olsr_hello_olsrorg(tvbuff_t *tvb, packet_info *pinfo, proto_tree *olsr_tree, int offset,
364     int link_message_end) {
365   /*-------------------Dissect Neighbor Addresses--------------------*/
366
367   while (offset < link_message_end) {
368     proto_item *address_group;
369     proto_tree *address_tree;
370     guint8      lq, nlq;
371
372     if (link_message_end - offset < pinfo->src.len + 4) {
373       proto_tree_add_expert_format(olsr_tree, pinfo, &ei_olsr_not_enough_bytes, tvb, offset, link_message_end - offset,
374           "Not enough bytes for last Olsr.org LQ-Hello entry");
375       return link_message_end;
376     }
377
378     if (pinfo->src.type == AT_IPv4) {
379       lq = tvb_get_guint8(tvb, offset + 4);
380       nlq = tvb_get_guint8(tvb, offset + 5);
381
382       address_group = proto_tree_add_bytes_format_value(olsr_tree, hf_olsr_neighbor, tvb, offset, 8,
383            NULL, "%s (%d/%d)", tvb_ip_to_str(tvb, offset), lq, nlq);
384
385       address_tree = proto_item_add_subtree(address_group, ett_olsr_message_neigh);
386
387       proto_tree_add_item(address_tree, hf_olsr_neighbor_addr, tvb, offset, 4, ENC_BIG_ENDIAN);
388       offset += 4;
389     } else if (pinfo->src.type == AT_IPv6) {
390       lq = tvb_get_guint8(tvb, offset + 16);
391       nlq = tvb_get_guint8(tvb, offset + 17);
392
393       address_group = proto_tree_add_bytes_format_value(olsr_tree, hf_olsr_neighbor, tvb, offset, 20,
394           NULL, "%s (%d/%d)", tvb_ip6_to_str(tvb, offset), lq, nlq);
395
396       address_tree = proto_item_add_subtree(address_group, ett_olsr_message_neigh);
397
398       proto_tree_add_item(address_tree, hf_olsr_neighbor6_addr, tvb, offset, 16, ENC_NA);
399       offset += 16;
400     } else {
401       break; /* unknown address type should be handled in dissect_olsr, just be sure */
402     }
403
404     proto_tree_add_item(address_tree, hf_olsrorg_lq, tvb, offset++, 1, ENC_BIG_ENDIAN);
405     proto_tree_add_item(address_tree, hf_olsrorg_nlq, tvb, offset++, 1, ENC_BIG_ENDIAN);
406     offset += 2;
407   } /* end while */
408   return link_message_end;
409 }
410
411 /*------------------------- MID Dissecting Code-------------------------*/
412 static int dissect_olsr_mid(tvbuff_t *tvb, packet_info *pinfo, proto_tree *olsr_tree, int offset, int message_end) {
413   while (offset < message_end) {
414     if (message_end - offset < pinfo->src.len) {
415       proto_tree_add_expert_format(olsr_tree, pinfo, &ei_olsr_not_enough_bytes, tvb, offset, message_end - offset,
416           "Not enough bytes for last MID entry");
417       return message_end;
418     }
419     if (pinfo->src.type == AT_IPv4) {
420       proto_tree_add_item(olsr_tree, hf_olsr_interface_addr, tvb, offset, 4, ENC_BIG_ENDIAN);
421       offset += 4;
422     } else if (pinfo->src.type == AT_IPv6) {
423       proto_tree_add_item(olsr_tree, hf_olsr_interface6_addr, tvb, offset, 16, ENC_NA);
424       offset += 16;
425     } else {
426       break; /* unknown address type should be handled in dissect_olsr, just be sure */
427     }
428   } /* end while for MID */
429   return message_end;
430 }
431
432 /*------------------------- HNA Dissecting Code-------------------------*/
433 static int dissect_olsr_hna(tvbuff_t *tvb, packet_info *pinfo, proto_tree *olsr_tree, int offset, int message_end) {
434   while (offset < message_end) {
435     if (message_end - offset < pinfo->src.len * 2) {
436       proto_tree_add_expert_format(olsr_tree, pinfo, &ei_olsr_not_enough_bytes, tvb, offset, message_end - offset,
437           "Not enough bytes for last HNA entry");
438       return message_end;
439     }
440
441     if (pinfo->src.type == AT_IPv4) {
442       proto_tree_add_item(olsr_tree, hf_olsr_network_addr, tvb, offset, 4, ENC_BIG_ENDIAN);
443       offset += 4;
444       proto_tree_add_item(olsr_tree, hf_olsr_netmask, tvb, offset, 4, ENC_BIG_ENDIAN);
445       offset += 4;
446     } else if (pinfo->src.type == AT_IPv6) {
447       proto_tree_add_item(olsr_tree, hf_olsr_network6_addr, tvb, offset, 4, ENC_NA);
448       offset += 16;
449       proto_tree_add_item(olsr_tree, hf_olsr_netmask6, tvb, offset, 4, ENC_NA);
450       offset += 16;
451     } else {
452       break; /* unknown address type should be handled in dissect_olsr, just be sure */
453     }
454   } /* end while for HNA */
455   return message_end;
456 }
457
458 /*------------------------- MID Dissecting Code-------------------------*/
459 static int dissect_olsrorg_nameservice(tvbuff_t *tvb, packet_info *pinfo, proto_tree *olsr_tree, int offset,
460     int message_end) {
461   guint16     version, count;
462
463   proto_item *olsr_ns_item, *ti;
464   proto_tree *olsr_ns_tree;
465
466   if (message_end - offset < 4) {
467     proto_tree_add_expert_format(olsr_tree, pinfo, &ei_olsr_not_enough_bytes, tvb, offset, message_end - offset,
468         "Not enough bytes for Olsr.org Nameservice message");
469     return message_end;
470   }
471
472   version = tvb_get_ntohs(tvb, offset);
473   ti = proto_tree_add_item(olsr_tree, hf_olsrorg_ns_version, tvb, offset, 2, ENC_BIG_ENDIAN);
474
475   count = tvb_get_ntohs(tvb, offset + 2);
476   proto_tree_add_item(olsr_tree, hf_olsrorg_ns_count, tvb, offset + 2, 2, ENC_BIG_ENDIAN);
477
478   offset += 4;
479
480   if (version != 1) {
481
482     expert_add_info(pinfo, ti, &ei_olsrorg_ns_version);
483     proto_tree_add_item(olsr_tree, hf_olsr_data, tvb, offset, message_end - offset, ENC_NA);
484     return message_end;
485   }
486
487   while (offset < message_end && count-- > 0) {
488     guint16 type, length;
489     int total_length;
490
491     if (message_end - offset < 20) {
492       proto_tree_add_expert_format(olsr_tree, pinfo, &ei_olsr_not_enough_bytes, tvb, offset, message_end - offset,
493           "Not enough bytes for last nameservice entry");
494       return message_end;
495     }
496
497     type = tvb_get_ntohs(tvb, offset);
498     length = tvb_get_ntohs(tvb, offset + 2);
499
500     total_length = 4 + 16 + ((length - 1) | 3) + 1;
501
502     olsr_ns_item = proto_tree_add_bytes_format_value(olsr_tree, hf_olsrorg_ns, tvb, offset, total_length,
503         NULL, "%s (%d)", val_to_str_const(type, nameservice_type_vals, "UNKNOWN"), type);
504
505     olsr_ns_tree = proto_item_add_subtree(olsr_ns_item, ett_olsr_message_ns);
506
507     proto_tree_add_item(olsr_ns_tree, hf_olsrorg_ns_type, tvb, offset, 2, ENC_BIG_ENDIAN);
508     proto_tree_add_uint(olsr_ns_tree, hf_olsrorg_ns_length, tvb, offset + 2, 2, length);
509
510     if (pinfo->src.type == AT_IPv4) {
511       proto_tree_add_item(olsr_ns_tree, hf_olsrorg_ns_ip, tvb, offset + 4, 4, ENC_BIG_ENDIAN);
512     } else if (pinfo->src.type == AT_IPv6) {
513       proto_tree_add_item(olsr_ns_tree, hf_olsrorg_ns_ip6, tvb, offset + 4, 16, ENC_NA);
514     } else {
515       break; /* unknown address type should be handled in dissect_olsr, just be sure */
516     }
517
518     if (message_end - offset < total_length) {
519       proto_tree_add_expert_format(olsr_tree, pinfo, &ei_olsr_not_enough_bytes, tvb, offset, message_end - offset,
520           "Not enough bytes for content of last nameservice entry");
521       return message_end;
522     }
523     proto_tree_add_item(olsr_ns_tree, hf_olsrorg_ns_content, tvb, offset + 20, length, ENC_ASCII|ENC_NA);
524     offset += 4 + 16 + ((length - 1) | 3) + 1;
525   }
526   return message_end;
527 }
528
529 /*------------------------- Packet Dissecting Code-------------------------*/
530 static int dissect_olsr(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data _U_) {
531   proto_item *ti;
532   proto_tree *olsr_tree;
533
534   int    offset, message_len, message_end;
535   guint  message_type;
536   double vTime;
537
538   guint16 packet_len;
539
540   /* Does this packet have a valid message type at the beginning? */
541   if (tvb_length(tvb) < 4) {
542     col_add_fstr(pinfo->cinfo, COL_INFO, "OLSR Packet,  Length: %u Bytes (not enough data in packet)",
543           tvb_length(tvb));
544     return 0; /* not enough bytes for the packet length */
545   }
546   packet_len = tvb_get_ntohs(tvb, 0);
547   if (packet_len > tvb_length(tvb)) {
548     col_add_fstr(pinfo->cinfo, COL_INFO, "OLSR Packet,  Length: %u Bytes (not enough data in packet)", packet_len);
549     return 0;
550   }
551   /*-------------Setting the Protocol and Info Columns in the Wireshark Display----------*/
552   col_set_str(pinfo->cinfo, COL_PROTOCOL, "OLSR v1");
553   col_clear(pinfo->cinfo, COL_INFO);
554
555   if ((pinfo->src.type != AT_IPv4) && (pinfo->src.type != AT_IPv6)) {
556     col_add_fstr(pinfo->cinfo, COL_INFO, "OLSR (unknown address type) Packet,  Length: %u Bytes", packet_len);
557     return 0;
558   }
559   if (pinfo->src.type == AT_IPv4) {
560     col_add_fstr(pinfo->cinfo, COL_INFO, "OLSR (IPv4) Packet,  Length: %u Bytes", packet_len);
561   } else if (pinfo->src.type == AT_IPv6) {
562     col_add_fstr(pinfo->cinfo, COL_INFO, "OLSR (IPv6) Packet,  Length: %u Bytes", packet_len);
563   }
564
565   /*-----------------Fetching Info from IP Packet and Adding to Tree------------------------*/
566     ti = proto_tree_add_item(tree, proto_olsr, tvb, 0, -1, ENC_NA);
567     olsr_tree = proto_item_add_subtree(ti, ett_olsr);
568
569     proto_tree_add_item(olsr_tree, hf_olsr_packet_len, tvb, 0, 2, ENC_BIG_ENDIAN);
570     proto_tree_add_item(olsr_tree, hf_olsr_packet_seq_num, tvb, 2, 2, ENC_BIG_ENDIAN);
571
572     offset = 4;
573
574     while (offset < packet_len) {
575       proto_item *message_item;
576       proto_tree *message_tree;
577
578       if (packet_len - offset < 4) {
579         proto_tree_add_expert_format(olsr_tree, pinfo, &ei_olsr_not_enough_bytes, tvb, offset, packet_len - offset,
580             "Message too short !");
581         break;
582       }
583
584       message_type = tvb_get_guint8(tvb, offset);
585       vTime = getOlsrTime(tvb_get_guint8(tvb, offset + 1));
586       message_len = tvb_get_ntohs(tvb, offset + 2);
587
588       message_item = proto_tree_add_bytes_format_value(olsr_tree, hf_olsr_message, tvb, offset, message_len,
589           NULL, "%s (%d)", val_to_str_const(message_type, message_type_vals, "UNKNOWN"),
590           message_type);
591       message_tree = proto_item_add_subtree(message_item, ett_olsr_message[message_type]);
592
593       proto_tree_add_uint(message_tree, hf_olsr_message_type, tvb, offset, 1, message_type);
594       offset += 1;
595
596       /*-------------Dissect Validity Time-------------------------*/
597       proto_tree_add_double_format_value(message_tree, hf_olsr_vtime, tvb, offset, 1, vTime,
598           "%.3f (in seconds)", vTime);
599       offset += 1;
600
601       /*-------------Dissect Message Size---------------------------*/
602       ti = proto_tree_add_item(message_tree, hf_olsr_message_size, tvb, offset, 2, ENC_BIG_ENDIAN);
603       offset += 2;
604
605       if (message_len < 8 + pinfo->src.len) {
606         proto_item_append_text(ti, "(too short, must be >= %d)", 8 + pinfo->src.len);
607         break;
608       }
609
610       message_end = offset + message_len - 4;
611       if (message_end > packet_len) {
612         proto_item_append_string(ti, "(not enough data for message)");
613         break;
614       }
615
616       /*-----------------Dissecting: Origin Addr, TTL, Hop Count, and Message Seq Number*/
617       if (pinfo->src.type == AT_IPv4) {
618         proto_tree_add_item(message_tree, hf_olsr_origin_addr, tvb, offset, 4, ENC_BIG_ENDIAN);
619         offset += 4;
620       } else if (pinfo->src.type == AT_IPv6) {
621         proto_tree_add_item(message_tree, hf_olsr_origin6_addr, tvb, offset, 16, ENC_NA);
622         offset += 16;
623       } else {
624         break; /* unknown address type should be handled before this loop, just be sure */
625       }
626
627       proto_tree_add_item(message_tree, hf_olsr_ttl, tvb, offset, 1, ENC_BIG_ENDIAN);
628       proto_tree_add_item(message_tree, hf_olsr_hop_count, tvb, offset + 1, 1, ENC_BIG_ENDIAN);
629       proto_tree_add_item(message_tree, hf_olsr_message_seq_num, tvb, offset + 2, 2, ENC_BIG_ENDIAN);
630       offset += 4;
631
632       if (offset < message_end) {
633         /* --------------Dissecting TC message--------------------- */
634         if (message_type == TC) {
635           dissect_olsr_tc(tvb, pinfo, message_tree, offset, message_end);
636         }
637         /* -------------Dissect HELLO message----------------------- */
638         else if (message_type == HELLO) {
639           dissect_olsr_hello(tvb, pinfo, message_tree, offset, message_end, &handle_olsr_hello_rfc);
640         }
641         /*---------------------------------Dissect MID Message----------------------------------*/
642         else if (message_type == MID) {
643           dissect_olsr_mid(tvb, pinfo, message_tree, offset, message_end);
644         }
645         /*-----------------------------Dissect HNA Message--------------------------------*/
646         else if (message_type == HNA) {
647           dissect_olsr_hna(tvb, pinfo, message_tree, offset, message_end);
648         }
649         /*-----------------------------Dissect Olsr.org Hello Message--------------------------------*/
650         else if (global_olsr_olsrorg && message_type == OLSR_ORG_LQ_HELLO) {
651           dissect_olsr_hello(tvb, pinfo, message_tree, offset, message_end, &handle_olsr_hello_olsrorg);
652         }
653         /*-----------------------------Dissect Olsr.org TC Message--------------------------------*/
654         else if (global_olsr_olsrorg && message_type == OLSR_ORG_LQ_TC) {
655           dissect_olsrorg_lq_tc(tvb, pinfo, message_tree, offset, message_end);
656         }
657
658         /*-----------------------------Dissect Olsr.org NS Message--------------------------------*/
659         else if (global_olsr_olsrorg && message_type == OLSR_ORG_NAMESERVICE) {
660           dissect_olsrorg_nameservice(tvb, pinfo, message_tree, offset, message_end);
661         }
662         /*-----------------------Dissect NRL OLSR TC Message-------------------------------------*/
663         else if (global_olsr_nrlolsr && message_type == NRLOLSR_TC_EXTRA) {
664           dissect_nrlolsr_tc(tvb, pinfo, message_tree, offset, message_end);
665         }
666
667         /*-----------------------------Undefined message types-----------------------------*/
668         else {
669           ti = proto_tree_add_bytes_format(message_tree, hf_olsr_data, tvb, offset, message_len - 12,
670               NULL, "Data (%u bytes)", message_len - 12);
671           if ((message_len - 12) % 4) {
672               expert_add_info(pinfo, ti, &ei_olsr_data_misaligned);
673             break;
674           }
675           /*offset += message_len - 12;*/
676         } /* end if for undefined message types */
677       }
678       offset = message_end;
679     } /* end while for message alive */
680
681   return tvb_length(tvb);
682 } /* end Dissecting */
683
684 /*-----------Register the Dissector for OLSR--------------*/
685 void proto_register_olsr(void) {
686   static hf_register_info hf[] = {
687     { &hf_olsr_packet_len,
688       { "Packet Length", "olsr.packet_len",
689         FT_UINT16, BASE_DEC, NULL, 0,
690         "Packet Length in Bytes", HFILL
691       }
692     },
693
694     { &hf_olsr_packet_seq_num,
695       { "Packet Sequence Number", "olsr.packet_seq_num",
696         FT_UINT16, BASE_DEC, NULL, 0,
697         NULL, HFILL
698       }
699     },
700
701     { &hf_olsr_message,
702       { "Message", "olsr.message",
703         FT_BYTES, BASE_NONE, NULL, 0,
704         NULL, HFILL
705       }
706     },
707
708     { &hf_olsr_message_type,
709       { "Message Type", "olsr.message_type",
710         FT_UINT8, BASE_DEC, VALS(message_type_vals), 0,
711         NULL, HFILL
712       }
713     },
714
715     { &hf_olsr_message_size,
716       { "Message", "olsr.message_size",
717         FT_UINT16, BASE_DEC, NULL, 0,
718         "Message Size in Bytes", HFILL
719       }
720     },
721
722     { &hf_olsr_message_seq_num,
723       { "Message Sequence Number", "olsr.message_seq_num",
724         FT_UINT16, BASE_DEC, NULL, 0,
725         NULL, HFILL
726       }
727     },
728
729     { &hf_olsr_vtime,
730       { "Validity Time", "olsr.vtime",
731         FT_DOUBLE, BASE_NONE, NULL, 0,
732         "Validity Time in seconds", HFILL
733       }
734     },
735
736     { &hf_olsr_ansn,
737       { "Advertised Neighbor Sequence Number (ANSN)", "olsr.ansn",
738         FT_UINT16, BASE_DEC, NULL, 0,
739         NULL, HFILL
740       }
741     },
742
743     { &hf_olsr_htime,
744       { "Hello Emission Interval", "olsr.htime",
745         FT_DOUBLE, BASE_NONE, NULL, 0,
746         "Hello emission interval in seconds", HFILL
747       }
748     },
749
750     { &hf_olsr_willingness,
751       { "Willingness to forward messages", "olsr.willingness",
752         FT_UINT8, BASE_DEC, VALS(willingness_type_vals), 0,
753         NULL, HFILL
754       }
755     },
756
757     { &hf_olsr_ttl,
758       { "TTL", "olsr.ttl",
759         FT_UINT8, BASE_DEC, NULL, 0,
760         "Time to Live in hops", HFILL
761       }
762     },
763
764     { &hf_olsr_link_type,
765       { "Link Type", "olsr.link_type",
766         FT_UINT8, BASE_DEC, VALS(link_type_vals), 0,
767         NULL, HFILL
768       }
769     },
770
771     { &hf_olsr_link_message_size,
772       { "Link Message Size", "olsr.link_message_size",
773         FT_UINT16, BASE_DEC, NULL, 0,
774         "Link Message Size in bytes", HFILL
775       }
776     },
777
778     { &hf_olsr_hop_count,
779       { "Hop Count", "olsr.hop_count",
780         FT_UINT8, BASE_DEC, NULL, 0,
781         NULL, HFILL
782       }
783     },
784
785     { &hf_olsr_neighbor,
786       { "Neighbor Address", "olsr.neighbor",
787         FT_BYTES, BASE_NONE, NULL, 0,
788         NULL, HFILL
789       }
790     },
791
792     { &hf_olsr_origin_addr,
793       { "Originator Address", "olsr.origin_addr",
794         FT_IPv4, BASE_NONE, NULL, 0,
795         NULL, HFILL
796       }
797     },
798
799     { &hf_olsr_neighbor_addr,
800       { "Neighbor Address", "olsr.neighbor_addr",
801         FT_IPv4, BASE_NONE, NULL, 0,
802         NULL, HFILL
803       }
804     },
805
806     { &hf_olsr_network_addr,
807       { "Network Address", "olsr.network_addr",
808         FT_IPv4, BASE_NONE, NULL, 0,
809         NULL, HFILL
810       }
811     },
812
813     { &hf_olsr_interface_addr,
814       { "Interface Address", "olsr.interface_addr",
815         FT_IPv4, BASE_NONE, NULL, 0,
816         NULL, HFILL
817       }
818     },
819
820     { &hf_olsr_netmask,
821       { "Netmask", "olsr.netmask",
822         FT_IPv4, BASE_NONE, NULL, 0,
823         NULL, HFILL
824       }
825     },
826
827 #if 0
828     { &hf_olsr_neighbor6,
829       { "Neighbor Address", "olsr.neighbor6",
830         FT_BYTES, BASE_NONE, NULL, 0,
831         NULL, HFILL
832       }
833     },
834 #endif
835
836     { &hf_olsr_origin6_addr,
837       { "Originator Address", "olsr.origin6_addr",
838         FT_IPv6, BASE_NONE, NULL, 0,
839         NULL, HFILL
840       }
841     },
842
843     { &hf_olsr_neighbor6_addr,
844       { "Neighbor Address", "olsr.neighbor6_addr",
845         FT_IPv6, BASE_NONE, NULL, 0,
846         NULL, HFILL
847       }
848     },
849
850     { &hf_olsr_network6_addr,
851       { "Network Address", "olsr.network6_addr",
852         FT_IPv6, BASE_NONE, NULL, 0,
853         NULL, HFILL
854       }
855     },
856
857     { &hf_olsr_interface6_addr,
858       { "Interface Address", "olsr.interface6_addr",
859         FT_IPv6, BASE_NONE, NULL, 0,
860         NULL, HFILL
861       }
862     },
863
864     { &hf_olsr_netmask6,
865       { "Netmask", "olsr.netmask6",
866         FT_IPv6, BASE_NONE, NULL, 0,
867         NULL, HFILL
868       }
869     },
870
871     { &hf_olsr_data,
872       { "Data", "olsr.data",
873         FT_BYTES, BASE_NONE, NULL, 0,
874         NULL, HFILL
875       }
876     },
877
878     { &hf_olsrorg_lq,
879       { "LQ", "olsr.lq",
880         FT_UINT8, BASE_DEC, NULL, 0,
881         "Link quality", HFILL
882       }
883     },
884     { &hf_olsrorg_nlq,
885       { "NLQ", "olsr.nlq",
886         FT_UINT8, BASE_DEC, NULL, 0,
887         "Neighbor link quality", HFILL
888       }
889     },
890
891     { &hf_olsrorg_ns,
892       { "Nameservice", "olsr.ns",
893         FT_BYTES, BASE_NONE, NULL, 0,
894         NULL, HFILL
895       }
896     },
897     { &hf_olsrorg_ns_version,
898       { "Version", "olsr.ns.version",
899         FT_UINT16, BASE_DEC, NULL, 0,
900         NULL, HFILL
901       }
902     },
903     { &hf_olsrorg_ns_count,
904       { "Count", "olsr.ns.count",
905         FT_UINT16, BASE_DEC, NULL, 0,
906         "Number of nameservice messages", HFILL
907       }
908     },
909     { &hf_olsrorg_ns_type,
910       { "Message Type", "olsr.ns.type",
911         FT_UINT16, BASE_DEC, VALS(nameservice_type_vals), 0,
912         NULL, HFILL
913       }
914     },
915     { &hf_olsrorg_ns_length,
916       { "Length", "olsr.ns.length",
917         FT_UINT16, BASE_DEC, NULL, 0,
918         NULL, HFILL
919       }
920     },
921     { &hf_olsrorg_ns_ip,
922       { "Address", "olsr.ns.ip",
923         FT_IPv4, BASE_NONE, NULL, 0,
924         NULL, HFILL
925       }
926     },
927     { &hf_olsrorg_ns_ip6,
928       { "Address", "olsr.ns.ip6",
929         FT_IPv6, BASE_NONE, NULL, 0,
930         NULL, HFILL
931       }
932     },
933     { &hf_olsrorg_ns_content,
934       { "Content", "olsr.ns.content",
935         FT_STRING, BASE_NONE, NULL, 0,
936         NULL, HFILL
937       }
938     },
939
940     { &hf_nrlolsr_f1,
941       { "NRL MINMAX", "olsr.nrl.minmax",
942         FT_UINT8, BASE_DEC, NULL, 0,
943         NULL, HFILL
944       }
945     },
946     { &hf_nrlolsr_f2,
947       { "NRL SPF", "olsr.nrl.spf",
948         FT_UINT8, BASE_DEC, NULL, 0,
949         NULL, HFILL
950       }
951     }
952   };
953
954   static gint *ett_base[] = {
955     &ett_olsr,
956     &ett_olsr_message_linktype,
957     &ett_olsr_message_neigh,
958     &ett_olsr_message_neigh6,
959     &ett_olsr_message_ns
960   };
961
962   static ei_register_info ei[] = {
963     { &ei_olsr_not_enough_bytes, { "olsr.not_enough_bytes", PI_MALFORMED, PI_ERROR, "Not enough bytes for field", EXPFILL }},
964     { &ei_olsrorg_ns_version,    { "olsr.ns.version.unknown", PI_PROTOCOL, PI_WARN, "Unknown nameservice protocol version", EXPFILL }},
965     { &ei_olsr_data_misaligned,  { "olsr.data.misaligned", PI_PROTOCOL, PI_WARN, "Must be aligned on 32 bits", EXPFILL }},
966   };
967
968   gint *ett[array_length(ett_base) + (G_MAXUINT8+1)];
969
970   module_t        *olsr_module;
971   expert_module_t *expert_olsr;
972   int              i,j;
973
974   memcpy(ett, ett_base, sizeof(ett_base));
975   j = array_length(ett_base);
976   for (i=0; i<G_MAXUINT8+1; i++) {
977     ett_olsr_message[i] = -1;
978     ett[j++] = &ett_olsr_message[i];
979   }
980
981   proto_olsr = proto_register_protocol("Optimized Link State Routing Protocol", "OLSR", "olsr");
982
983   proto_register_field_array(proto_olsr, hf, array_length(hf));
984   proto_register_subtree_array(ett, array_length(ett));
985   expert_olsr = expert_register_protocol(proto_olsr);
986   expert_register_field_array(expert_olsr, ei, array_length(ei));
987
988   olsr_module = prefs_register_protocol(proto_olsr, NULL);
989   prefs_register_bool_preference(olsr_module, "ff_olsrorg",
990                                  "Dissect olsr.org messages","Dissect custom olsr.org message types (compatible with rfc routing agents)",
991                                  &global_olsr_olsrorg);
992   prefs_register_bool_preference(olsr_module, "nrlolsr",
993                                  "Dissect NRL-Olsr TC messages", "Dissect custom nrlolsr tc message (incompatible with rfc routing agents)",
994                                  &global_olsr_nrlolsr);
995 }
996
997 void proto_reg_handoff_olsr(void) {
998   dissector_handle_t olsr_handle;
999
1000   olsr_handle = new_create_dissector_handle(dissect_olsr, proto_olsr);
1001   dissector_add_uint("udp.port", UDP_PORT_OLSR, olsr_handle);
1002 }
1003
1004 /*
1005  * Editor modelines
1006  *
1007  * Local Variables:
1008  * c-basic-offset: 2
1009  * tab-width: 8
1010  * indent-tabs-mode: nil
1011  * End:
1012  *
1013  * ex: set shiftwidth=2 tabstop=8 expandtab:
1014  * :indentSize=2:tabSize=8:noTabs=true:
1015  */