3b7c912ed2ded5b04c9c0e38cfcde841f7a81058
[obnox/wireshark/wip.git] / packet-ospf.c
1 /* packet-ospf.c
2  * Routines for OSPF packet disassembly
3  * (c) Copyright Hannes R. Boehm <hannes@boehm.org>
4  *
5  * $Id: packet-ospf.c,v 1.58 2002/02/19 21:56:56 ashokn Exp $
6  *
7  * At this time, this module is able to analyze OSPF
8  * packets as specified in RFC2328. MOSPF (RFC1584) and other
9  * OSPF Extensions which introduce new Packet types
10  * (e.g the External Atributes LSA) are not supported.
11  * Furthermore RFC2740 (OSPFv3 - OSPF for IPv6) is now supported
12  *   - (c) 2001 Palle Lyckegaard <palle[AT]lyckegaard.dk>
13  *
14  * TOS - support is not fully implemented
15  * 
16  * Ethereal - Network traffic analyzer
17  * By Gerald Combs <gerald@ethereal.com>
18  * Copyright 1998 Gerald Combs
19  * 
20  * This program is free software; you can redistribute it and/or
21  * modify it under the terms of the GNU General Public License
22  * as published by the Free Software Foundation; either version 2
23  * of the License, or (at your option) any later version.
24  * 
25  * This program is distributed in the hope that it will be useful,
26  * but WITHOUT ANY WARRANTY; without even the implied warranty of
27  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28  * GNU General Public License for more details.
29  * 
30  * You should have received a copy of the GNU General Public License
31  * along with this program; if not, write to the Free Software
32  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
33  */
34  
35 #ifdef HAVE_CONFIG_H
36 # include "config.h"
37 #endif
38
39 #ifdef HAVE_SYS_TYPES_H
40 # include <sys/types.h>
41 #endif
42
43 #ifdef HAVE_NETINET_IN_H
44 # include <netinet/in.h>
45 #endif
46
47 #include <stdio.h>
48 #include <string.h>
49
50 #include <glib.h>
51 #include <epan/packet.h>
52 #include "ipproto.h"
53 #include "in_cksum.h"
54 #include "ieee-float.h"
55
56 #define OSPF_VERSION_2 2
57 #define OSPF_VERSION_3 3
58 #define OSPF_VERSION_2_HEADER_LENGTH    24
59 #define OSPF_VERSION_3_HEADER_LENGTH    16
60
61
62 #define OSPF_HELLO      1
63 #define OSPF_DB_DESC    2
64 #define OSPF_LS_REQ     3
65 #define OSPF_LS_UPD     4
66 #define OSPF_LS_ACK     5
67
68 static const value_string pt_vals[] = {
69         {OSPF_HELLO,   "Hello Packet"   },
70         {OSPF_DB_DESC, "DB Descr."      },
71         {OSPF_LS_REQ,  "LS Request"     },
72         {OSPF_LS_UPD,  "LS Update"      },
73         {OSPF_LS_ACK,  "LS Acknowledge" },
74         {0,             NULL            }
75 };
76
77 #define OSPF_AUTH_NONE          0
78 #define OSPF_AUTH_SIMPLE        1
79 #define OSPF_AUTH_CRYPT         2
80
81 static const value_string auth_vals[] = {
82         {OSPF_AUTH_NONE,   "Null"            },
83         {OSPF_AUTH_SIMPLE, "Simple password" },
84         {OSPF_AUTH_CRYPT,  "Cryptographic"   },
85         {0,                NULL              }
86 };
87
88 #define OSPF_V2_OPTIONS_E               0x02
89 #define OSPF_V2_OPTIONS_MC              0x04
90 #define OSPF_V2_OPTIONS_NP              0x08
91 #define OSPF_V2_OPTIONS_EA              0x10
92 #define OSPF_V2_OPTIONS_DC              0x20
93 #define OSPF_V2_OPTIONS_O               0x40
94 #define OSPF_V2_OPTIONS_DN              0x01
95 #define OSPF_V3_OPTIONS_V6              0x01    
96 #define OSPF_V3_OPTIONS_E               0x02
97 #define OSPF_V3_OPTIONS_MC              0x04
98 #define OSPF_V3_OPTIONS_N               0x08
99 #define OSPF_V3_OPTIONS_R               0x10
100 #define OSPF_V3_OPTIONS_DC              0x20
101
102
103 #define OSPF_DBD_FLAG_MS        1
104 #define OSPF_DBD_FLAG_M         2
105 #define OSPF_DBD_FLAG_I         4
106
107 #define OSPF_LS_REQ_LENGTH      12
108
109 #define OSPF_LSTYPE_ROUTER      1
110 #define OSPF_LSTYPE_NETWORK     2
111 #define OSPF_LSTYPE_SUMMERY     3
112 #define OSPF_LSTYPE_ASBR        4
113 #define OSPF_LSTYPE_ASEXT       5
114 #define OSPF_LSTYPE_GRPMEMBER   6
115 #define OSPF_LSTYPE_ASEXT7      7
116 #define OSPF_LSTYPE_EXTATTR     8
117 #define OSPF_V3_LSTYPE_ROUTER                0x2001
118 #define OSPF_V3_LSTYPE_NETWORK               0x2002
119 #define OSPF_V3_LSTYPE_INTER_AREA_PREFIX     0x2003
120 #define OSPF_V3_LSTYPE_INTER_AREA_ROUTER     0x2004
121 #define OSPF_V3_LSTYPE_AS_EXTERNAL           0x4005
122 #define OSPF_V3_LSTYPE_GROUP_MEMBERSHIP      0x2006     
123 #define OSPF_V3_LSTYPE_TYPE_7                0x2007
124 #define OSPF_V3_LSTYPE_LINK                  0x0008
125 #define OSPF_V3_LSTYPE_INTRA_AREA_PREFIX     0x2009
126
127 /* Opaque LSA types */
128 #define OSPF_LSTYPE_OP_LINKLOCAL 9
129 #define OSPF_LSTYPE_OP_AREALOCAL 10
130 #define OSPF_LSTYPE_OP_ASWIDE    11
131
132 #define OSPF_LINK_PTP           1
133 #define OSPF_LINK_TRANSIT       2
134 #define OSPF_LINK_STUB          3
135 #define OSPF_LINK_VIRTUAL       4
136
137 #define OSPF_V3_LINK_PTP        1
138 #define OSPF_V3_LINK_TRANSIT    2
139 #define OSPF_V3_LINK_RESERVED   3
140 #define OSPF_V3_LINK_VIRTUAL    4
141
142 #define OSPF_LSA_HEADER_LENGTH  20
143
144 /* Known opaque LSAs */
145 #define OSPF_LSA_MPLS_TE        1
146
147
148 static const value_string ls_type_vals[] = {
149         {OSPF_LSTYPE_ROUTER,                  "Router-LSA"                   },
150         {OSPF_LSTYPE_NETWORK,                 "Network-LSA"                  },
151         {OSPF_LSTYPE_SUMMERY,                 "Summary-LSA (IP network)"     },
152         {OSPF_LSTYPE_ASBR,                    "Summary-LSA (ASBR)"           },
153         {OSPF_LSTYPE_ASEXT,                   "AS-External-LSA (ASBR)"       },
154         {OSPF_LSTYPE_GRPMEMBER,               "Group Membership LSA"         },
155         {OSPF_LSTYPE_ASEXT7,                  "NSSA AS-External-LSA"         },
156         {OSPF_LSTYPE_EXTATTR,                 "External Attributes LSA"      },
157         {OSPF_LSTYPE_OP_LINKLOCAL,            "Opaque LSA, Link-local scope" },
158         {OSPF_LSTYPE_OP_AREALOCAL,            "Opaque LSA, Area-local scope" },
159         {0,                                   NULL                           }
160
161 };
162
163 static const value_string v3_ls_type_vals[] = {
164         {OSPF_V3_LSTYPE_ROUTER,               "Router-LSA"                   }, 
165         {OSPF_V3_LSTYPE_NETWORK,              "Network-LSA"                  }, 
166         {OSPF_V3_LSTYPE_INTER_AREA_PREFIX,    "Inter-Area-Prefix-LSA"        }, 
167         {OSPF_V3_LSTYPE_INTER_AREA_ROUTER,    "Inter-Area-Router-LSA"        }, 
168         {OSPF_V3_LSTYPE_AS_EXTERNAL,          "AS-External-LSA"              }, 
169         {OSPF_V3_LSTYPE_GROUP_MEMBERSHIP,     "Group-Membership-LSA"         }, 
170         {OSPF_V3_LSTYPE_TYPE_7,               "Type-LSA"                     }, 
171         {OSPF_V3_LSTYPE_LINK,                 "Link-LSA"                     },
172         {OSPF_V3_LSTYPE_INTRA_AREA_PREFIX,    "Intra-Area-Prefix-LSA"        },
173         {0,                                   NULL                           }
174
175 };
176
177
178 #define OSPF_V3_ROUTER_LSA_FLAG_B 0x01
179 #define OSPF_V3_ROUTER_LSA_FLAG_E 0x02
180 #define OSPF_V3_ROUTER_LSA_FLAG_V 0x04
181 #define OSPF_V3_ROUTER_LSA_FLAG_W 0x08
182
183 #define OSPF_V3_PREFIX_OPTION_NU 0x01
184 #define OSPF_V3_PREFIX_OPTION_LA 0x02
185 #define OSPF_V3_PREFIX_OPTION_MC 0x04
186 #define OSPF_V3_PREFIX_OPTION_P  0x08
187
188 #define OSPF_V3_AS_EXTERNAL_FLAG_T 0x01
189 #define OSPF_V3_AS_EXTERNAL_FLAG_F 0x02
190 #define OSPF_V3_AS_EXTERNAL_FLAG_E 0x04
191
192
193 static int proto_ospf = -1;
194
195 static gint ett_ospf = -1;
196 static gint ett_ospf_hdr = -1;
197 static gint ett_ospf_hello = -1;
198 static gint ett_ospf_desc = -1;
199 static gint ett_ospf_lsr = -1;
200 static gint ett_ospf_lsa = -1;
201 static gint ett_ospf_lsa_upd = -1;
202
203 /* Trees for opaque LSAs */
204 static gint ett_ospf_lsa_mpls = -1;
205 static gint ett_ospf_lsa_mpls_router = -1;
206 static gint ett_ospf_lsa_mpls_link = -1;
207 static gint ett_ospf_lsa_mpls_link_stlv = -1;
208
209 static dissector_handle_t data_handle;
210
211 static void dissect_ospf_hello(tvbuff_t*, int, proto_tree*, guint8);
212 static void dissect_ospf_db_desc(tvbuff_t*, int, proto_tree*, guint8); 
213 static void dissect_ospf_ls_req(tvbuff_t*, int, proto_tree*, guint8); 
214 static void dissect_ospf_ls_upd(tvbuff_t*, int, proto_tree*, guint8); 
215 static void dissect_ospf_ls_ack(tvbuff_t*, int, proto_tree*, guint8); 
216
217 /* dissect_ospf_v[23]lsa returns the offset of the next LSA
218  * if disassemble_body is set to FALSE (e.g. in LSA ACK 
219  * packets), the offset is set to the offset of the next
220  * LSA header
221  */
222 static int dissect_ospf_v2_lsa(tvbuff_t*, int, proto_tree*, gboolean disassemble_body); 
223 static int dissect_ospf_v3_lsa(tvbuff_t*, int, proto_tree*, gboolean disassemble_body); 
224
225 static void dissect_ospf_options(tvbuff_t *, int, proto_tree *, guint8);
226
227 static void dissect_ospf_v3_prefix_options(tvbuff_t *, int, proto_tree *);
228
229 static void dissect_ospf_v3_address_prefix(tvbuff_t *, int, int, proto_tree *);
230
231 static void 
232 dissect_ospf(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
233 {
234     proto_tree *ospf_tree = NULL;
235     proto_item *ti; 
236     proto_tree *ospf_header_tree;
237     guint8  version;
238     guint8  packet_type;
239     guint16 ospflen;
240     vec_t cksum_vec[4];
241     int cksum_vec_len;
242     guint32 phdr[2];
243     guint16 cksum, computed_cksum;
244     guint length, reported_length;
245     guint16 auth_type;
246     char auth_data[8];
247     int crypto_len;
248     unsigned int ospf_header_length;
249     guint8 instance_ID;
250     guint8 reserved;
251     guint32 areaid;
252
253
254     if (check_col(pinfo->cinfo, COL_PROTOCOL))
255         col_set_str(pinfo->cinfo, COL_PROTOCOL, "OSPF");
256     if (check_col(pinfo->cinfo, COL_INFO))
257         col_clear(pinfo->cinfo, COL_INFO);
258
259     version = tvb_get_guint8(tvb, 0);
260     switch (version) {
261         case OSPF_VERSION_2:
262             ospf_header_length = OSPF_VERSION_2_HEADER_LENGTH;
263             break;
264         case OSPF_VERSION_3:
265             ospf_header_length = OSPF_VERSION_3_HEADER_LENGTH;
266             break;
267         default:
268             ospf_header_length = 0;
269             break;
270     }
271
272     packet_type = tvb_get_guint8(tvb, 1);
273     if (check_col(pinfo->cinfo, COL_INFO)) {
274         col_add_str(pinfo->cinfo, COL_INFO,
275                     val_to_str(packet_type, pt_vals, "Unknown (%u)"));
276     }  
277
278     if (tree) {
279         ospflen = tvb_get_ntohs(tvb, 2);
280
281         ti = proto_tree_add_item(tree, proto_ospf, tvb, 0, ospflen, FALSE);
282         ospf_tree = proto_item_add_subtree(ti, ett_ospf);
283
284         ti = proto_tree_add_text(ospf_tree, tvb, 0, ospf_header_length,
285                                  "OSPF Header"); 
286         ospf_header_tree = proto_item_add_subtree(ti, ett_ospf_hdr);
287
288         proto_tree_add_text(ospf_header_tree, tvb, 0, 1, "OSPF Version: %u",
289                             version);  
290         proto_tree_add_text(ospf_header_tree, tvb, 1, 1, "OSPF Packet Type: %u (%s)",
291                             packet_type,
292                             val_to_str(packet_type, pt_vals, "Unknown"));
293         proto_tree_add_text(ospf_header_tree, tvb, 2, 2, "Packet Length: %u",
294                             ospflen);
295         proto_tree_add_text(ospf_header_tree, tvb, 4, 4, "Source OSPF Router ID: %s",
296                             ip_to_str(tvb_get_ptr(tvb, 4, 4)));
297         areaid=tvb_get_ntohl(tvb,8);
298         proto_tree_add_text(ospf_header_tree, tvb, 8, 4, "Area ID: %s%s",
299                                ip_to_str(tvb_get_ptr(tvb, 8, 4)), areaid == 0 ? " (Backbone)" : "");
300         cksum = tvb_get_ntohs(tvb, 12);
301         length = tvb_length(tvb);
302         /* XXX - include only the length from the OSPF header? */
303         reported_length = tvb_reported_length(tvb);
304         if (cksum == 0) {
305                 /* No checksum supplied in the packet. */
306                 proto_tree_add_text(ospf_header_tree, tvb, 12, 2,
307                     "Packet Checksum: 0x%04x (none)", cksum);
308         } else if (!pinfo->fragmented && length >= reported_length
309                 && length >= ospf_header_length
310                 && (version == OSPF_VERSION_2 || version == OSPF_VERSION_3)) {
311             /* The packet isn't part of a fragmented datagram and isn't
312                truncated, and we know how to checksum this version of
313                OSPF, so we can checksum it. */
314
315             switch (version) {
316
317             case OSPF_VERSION_2:
318                 /* Header, not including the authentication data (the OSPFv2
319                    checksum excludes the 64-bit authentication field). */
320                 cksum_vec[0].ptr = tvb_get_ptr(tvb, 0, 16);
321                 cksum_vec[0].len = 16;
322                 if (length > ospf_header_length) {
323                     /* Rest of the packet, again not including the
324                        authentication data. */
325                     reported_length -= ospf_header_length;
326                     cksum_vec[1].ptr = tvb_get_ptr(tvb, ospf_header_length, reported_length);
327                     cksum_vec[1].len = reported_length;
328                     cksum_vec_len = 2;
329                 } else {
330                     /* There's nothing but a header. */
331                     cksum_vec_len = 1;
332                 }
333                 break;
334
335             case OSPF_VERSION_3:
336                 /* IPv6-style checksum, covering the entire OSPF packet
337                    and a prepended IPv6 pseudo-header. */
338
339                 /* Set up the fields of the pseudo-header. */
340                 cksum_vec[0].ptr = pinfo->src.data;
341                 cksum_vec[0].len = pinfo->src.len;
342                 cksum_vec[1].ptr = pinfo->dst.data;
343                 cksum_vec[1].len = pinfo->dst.len;
344                 cksum_vec[2].ptr = (const guint8 *)&phdr;
345                 phdr[0] = htonl(ospflen);
346                 phdr[1] = htonl(IP_PROTO_OSPF);
347                 cksum_vec[2].len = 8;
348
349                 cksum_vec[3].ptr = tvb_get_ptr(tvb, 0, reported_length);
350                 cksum_vec[3].len = reported_length;
351                 cksum_vec_len = 4;
352                 break;
353
354             default:
355                 g_assert_not_reached();
356                 cksum_vec_len = 0;
357             }
358             computed_cksum = in_cksum(cksum_vec, cksum_vec_len);
359             if (computed_cksum == 0) {
360                 proto_tree_add_text(ospf_header_tree, tvb, 12, 2,
361                         "Packet Checksum: 0x%04x (correct)", cksum);
362             } else {
363                 proto_tree_add_text(ospf_header_tree, tvb, 12, 2,
364                         "Packet Checksum: 0x%04x (incorrect, should be 0x%04x)",
365                         cksum, in_cksum_shouldbe(cksum, computed_cksum));
366             }
367         } else {
368             proto_tree_add_text(ospf_header_tree, tvb, 12, 2,
369                 "Packet Checksum: 0x%04x", cksum);
370         }
371
372
373         /* Authentication is only valid for OSPFv2 */
374         if ( version == OSPF_VERSION_2 ) {        
375             auth_type = tvb_get_ntohs(tvb, 14);
376             proto_tree_add_text(ospf_header_tree, tvb, 14, 2, "Auth Type: %s",
377                             val_to_str(auth_type, auth_vals, "Unknown (%u)"));
378             switch (auth_type) {
379
380             case OSPF_AUTH_NONE:
381                 proto_tree_add_text(ospf_header_tree, tvb, 16, 8, "Auth Data (none)");
382                 break;
383
384             case OSPF_AUTH_SIMPLE:
385                 tvb_get_nstringz0(tvb, 16, 8, auth_data);
386                 proto_tree_add_text(ospf_header_tree, tvb, 16, 8, "Auth Data: %s", auth_data);
387                 break;
388
389             case OSPF_AUTH_CRYPT:
390                 proto_tree_add_text(ospf_header_tree, tvb, 18, 1, "Auth Key ID: %u",
391                                 tvb_get_guint8(tvb, 18));
392                 crypto_len = tvb_get_guint8(tvb, 19);
393                 proto_tree_add_text(ospf_header_tree, tvb, 19, 1, "Auth Data Length: %u",
394                                 crypto_len);
395                 proto_tree_add_text(ospf_header_tree, tvb, 20, 4, "Auth Crypto Sequence Number: 0x%x",
396                                 tvb_get_ntohl(tvb, 20));
397   
398                 /* Show the message digest that was appended to the end of the
399                    OSPF message - but only if it's present (we don't want
400                    to get an exception before we've tried dissecting OSPF
401                    message). */
402                 if (tvb_bytes_exist(tvb, ospflen, crypto_len)) {
403                     proto_tree_add_text(ospf_header_tree, tvb, ospflen, crypto_len,
404                                     "Auth Data: %s",
405                                     tvb_bytes_to_str(tvb, ospflen, crypto_len));
406                 }
407                 break;
408
409             default:
410                 proto_tree_add_text(ospf_header_tree, tvb, 16, 8, "Auth Data (unknown)");
411                 break;
412             }
413
414         }
415
416         /* Instance ID and "reserved" is OSPFv3-only */
417         if ( version == OSPF_VERSION_3 ) {
418             instance_ID = tvb_get_guint8(tvb, 14);
419             proto_tree_add_text(ospf_header_tree, tvb, 14, 1, "Instance ID: %u",
420                             instance_ID);
421             reserved = tvb_get_guint8(tvb, 15);
422             proto_tree_add_text(ospf_header_tree, tvb, 15, 1, (reserved == 0 ? "Reserved: %u" : "Reserved: %u (incorrect, should be 0)"),
423                                 reserved);
424
425         }
426
427         /* Adjust the length of the tvbuff to match the size of the OSPF
428          * packet (since the dissect routines use it to work out where the
429          * end of the OSPF packet is).
430          */
431         tvb_set_reported_length(tvb, ospflen);
432
433         switch (packet_type){
434
435         case OSPF_HELLO:
436             dissect_ospf_hello(tvb, ospf_header_length, ospf_tree, version);
437             break;
438
439         case OSPF_DB_DESC:
440             dissect_ospf_db_desc(tvb, ospf_header_length, ospf_tree, version);
441             break;
442
443         case OSPF_LS_REQ:
444             dissect_ospf_ls_req(tvb, ospf_header_length, ospf_tree, version);
445             break;
446
447         case OSPF_LS_UPD:
448             dissect_ospf_ls_upd(tvb, ospf_header_length, ospf_tree, version);
449             break;
450
451         case OSPF_LS_ACK:
452             dissect_ospf_ls_ack(tvb, ospf_header_length, ospf_tree, version);
453             break;
454
455         default:
456             call_dissector(data_handle,tvb_new_subset(tvb, ospf_header_length,-1,tvb_reported_length_remaining(tvb,ospf_header_length)), pinfo, tree);
457             break;
458         }
459     }
460 }
461
462 static void
463 dissect_ospf_hello(tvbuff_t *tvb, int offset, proto_tree *tree, guint8 version)
464 {
465     proto_tree *ospf_hello_tree;
466     proto_item *ti; 
467
468     ti = proto_tree_add_text(tree, tvb, offset, -1, "OSPF Hello Packet");
469     ospf_hello_tree = proto_item_add_subtree(ti, ett_ospf_hello);
470     
471     switch (version ) {
472         case OSPF_VERSION_2:
473             proto_tree_add_text(ospf_hello_tree, tvb, offset, 4, "Network Mask: %s",
474                         ip_to_str(tvb_get_ptr(tvb, offset, 4)));
475             proto_tree_add_text(ospf_hello_tree, tvb, offset + 4, 2,
476                         "Hello Interval: %u seconds",
477                         tvb_get_ntohs(tvb, offset + 4));
478
479             dissect_ospf_options(tvb, offset + 6, ospf_hello_tree, version);
480             proto_tree_add_text(ospf_hello_tree, tvb, offset + 7, 1, "Router Priority: %u",
481                         tvb_get_guint8(tvb, offset + 7));
482             proto_tree_add_text(ospf_hello_tree, tvb, offset + 8, 4, "Router Dead Interval: %u seconds",
483                         tvb_get_ntohl(tvb, offset + 8));
484             proto_tree_add_text(ospf_hello_tree, tvb, offset + 12, 4, "Designated Router: %s",
485                         ip_to_str(tvb_get_ptr(tvb, offset + 12, 4)));
486             proto_tree_add_text(ospf_hello_tree, tvb, offset + 16, 4, "Backup Designated Router: %s",
487                         ip_to_str(tvb_get_ptr(tvb, offset + 16, 4)));
488
489             offset += 20;
490             while (tvb_reported_length_remaining(tvb, offset) != 0) {
491                 proto_tree_add_text(ospf_hello_tree, tvb, offset, 4,
492                             "Active Neighbor: %s",
493                             ip_to_str(tvb_get_ptr(tvb, offset, 4)));
494                 offset += 4;
495             }
496             break;
497         case OSPF_VERSION_3:
498             proto_tree_add_text(ospf_hello_tree, tvb, offset + 0, 4, "Interface ID: %u",
499                         tvb_get_ntohl(tvb, offset + 0));
500             proto_tree_add_text(ospf_hello_tree, tvb, offset + 4, 1, "Router Priority: %u",
501                         tvb_get_guint8(tvb, offset + 4));
502             dissect_ospf_options(tvb, offset + 5, ospf_hello_tree, version);
503             proto_tree_add_text(ospf_hello_tree, tvb, offset + 8, 2,
504                         "Hello Interval: %u seconds",
505                         tvb_get_ntohs(tvb, offset + 8));
506             proto_tree_add_text(ospf_hello_tree, tvb, offset + 10, 2, "Router Dead Interval: %u seconds",
507                         tvb_get_ntohs(tvb, offset + 10));
508             proto_tree_add_text(ospf_hello_tree, tvb, offset + 12, 4, "Designated Router: %s",
509                         ip_to_str(tvb_get_ptr(tvb, offset + 12, 4)));
510             proto_tree_add_text(ospf_hello_tree, tvb, offset + 16, 4, "Backup Designated Router: %s",
511                         ip_to_str(tvb_get_ptr(tvb, offset + 16, 4)));
512             offset += 20;
513             while (tvb_reported_length_remaining(tvb, offset) != 0) {
514                 proto_tree_add_text(ospf_hello_tree, tvb, offset, 4,
515                             "Active Neighbor: %s",
516                             ip_to_str(tvb_get_ptr(tvb, offset, 4)));
517                 offset += 4;
518             }
519
520             break;
521
522         default:    
523             break;
524     }
525 }
526
527 static void
528 dissect_ospf_db_desc(tvbuff_t *tvb, int offset, proto_tree *tree, guint8 version)
529 {
530     proto_tree *ospf_db_desc_tree=NULL;
531     proto_item *ti; 
532     guint8 flags;
533     guint8 reserved;
534     char flags_string[20] = "";
535
536     if (tree) {
537         ti = proto_tree_add_text(tree, tvb, offset, -1, "OSPF DB Description"); 
538         ospf_db_desc_tree = proto_item_add_subtree(ti, ett_ospf_desc);
539
540         switch (version ) {
541  
542             case OSPF_VERSION_2:
543
544                 proto_tree_add_text(ospf_db_desc_tree, tvb, offset, 2, "Interface MTU: %u",
545                             tvb_get_ntohs(tvb, offset));
546
547                 dissect_ospf_options(tvb, offset + 2, ospf_db_desc_tree, version);
548
549                 flags = tvb_get_guint8(tvb, offset + 3);
550                 if (flags & OSPF_DBD_FLAG_MS)
551                     strcat(flags_string, "MS");
552                 if (flags & OSPF_DBD_FLAG_M) {
553                     if (flags_string[0] != '\0')
554                         strcat(flags_string, "/");
555                     strcat(flags_string, "M");
556                 }
557                 if (flags & OSPF_DBD_FLAG_I) {
558                     if (flags_string[0] != '\0')
559                         strcat(flags_string, "/");
560                     strcat(flags_string, "I");
561                 }
562                 proto_tree_add_text(ospf_db_desc_tree, tvb, offset + 3, 1, "Flags: 0x%x (%s)",
563                             flags, flags_string);
564                 proto_tree_add_text(ospf_db_desc_tree, tvb, offset + 4, 4, "DD Sequence: %u",
565                             tvb_get_ntohl(tvb, offset + 4));
566
567                 offset += 8;
568                 break;
569
570             case OSPF_VERSION_3:
571
572                 reserved = tvb_get_guint8(tvb, offset);
573                 proto_tree_add_text(ospf_db_desc_tree, tvb, offset, 1, (reserved == 0 ? "Reserved: %u" : "Reserved: %u (incorrect, should be 0)"),
574                                 reserved);
575
576                 dissect_ospf_options(tvb, offset + 1, ospf_db_desc_tree, version);
577
578                 proto_tree_add_text(ospf_db_desc_tree, tvb, offset + 4, 2, "Interface MTU: %u",
579                             tvb_get_ntohs(tvb, offset+4));
580
581                 reserved = tvb_get_guint8(tvb, offset + 6);
582                 proto_tree_add_text(ospf_db_desc_tree, tvb, offset + 6, 1, (reserved == 0 ? "Reserved: %u" : "Reserved: %u (incorrect, should be 0)"),
583                                 reserved);
584
585                 flags = tvb_get_guint8(tvb, offset + 7);
586                 if (flags & OSPF_DBD_FLAG_MS)
587                     strcat(flags_string, "MS");
588                 if (flags & OSPF_DBD_FLAG_M) {
589                     if (flags_string[0] != '\0')
590                         strcat(flags_string, "/");
591                     strcat(flags_string, "M");
592                 }
593                 if (flags & OSPF_DBD_FLAG_I) {
594                     if (flags_string[0] != '\0')
595                         strcat(flags_string, "/");
596                     strcat(flags_string, "I");
597                 }
598                 proto_tree_add_text(ospf_db_desc_tree, tvb, offset + 7, 1, "Flags: 0x%x (%s)",
599                             flags, flags_string);
600
601                 proto_tree_add_text(ospf_db_desc_tree, tvb, offset + 8, 4, "DD Sequence: %u",
602                             tvb_get_ntohl(tvb, offset + 8));
603
604                 offset += 12;
605                 break;
606
607             default:
608                 break;
609         }
610     }
611
612     /* LS Headers will be processed here */
613     /* skip to the end of DB-Desc header */
614     while (tvb_reported_length_remaining(tvb, offset) != 0) {
615       if ( version == OSPF_VERSION_2)
616           offset = dissect_ospf_v2_lsa(tvb, offset, tree, FALSE);
617       else
618           if ( version == OSPF_VERSION_3)
619               offset = dissect_ospf_v3_lsa(tvb, offset, tree, FALSE);
620     }
621
622 }
623
624 static void
625 dissect_ospf_ls_req(tvbuff_t *tvb, int offset, proto_tree *tree, guint8 version)
626 {
627     proto_tree *ospf_lsr_tree;
628     proto_item *ti;
629     guint32 ls_type;
630     guint8 reserved;
631
632     /* zero or more LS requests may be within a LS Request */
633     /* we place every request for a LSA in a single subtree */
634     while (tvb_reported_length_remaining(tvb, offset) != 0) {
635         ti = proto_tree_add_text(tree, tvb, offset, OSPF_LS_REQ_LENGTH,
636                                  "Link State Request"); 
637         ospf_lsr_tree = proto_item_add_subtree(ti, ett_ospf_lsr);
638       
639         reserved = tvb_get_guint8(tvb, offset);
640         proto_tree_add_text(ospf_lsr_tree, tvb, offset, 1,
641            (reserved == 0 ? "Reserved: %u" : "Reserved: %u (incorrect, should be 0)"),
642                                 reserved);
643
644         switch ( version ) {
645
646             case OSPF_VERSION_2:
647                 ls_type = tvb_get_ntohl(tvb, offset);
648                 proto_tree_add_text(ospf_lsr_tree, tvb, offset, 4, "LS Type: %s (%u)",
649                             val_to_str(ls_type, ls_type_vals, "Unknown"),
650                             ls_type);
651                 break;
652             case OSPF_VERSION_3:
653                 ls_type = tvb_get_ntohs(tvb, offset+2);
654                 proto_tree_add_text(ospf_lsr_tree, tvb, offset+2, 2, "LS Type: %s (0x%04x)",
655                             val_to_str(ls_type, v3_ls_type_vals, "Unknown"),
656                             ls_type);
657                   break;
658             default:
659                  ls_type=0;
660                  break;
661         }
662
663
664         proto_tree_add_text(ospf_lsr_tree, tvb, offset + 4, 4, "Link State ID: %s", 
665                             ip_to_str(tvb_get_ptr(tvb, offset + 4, 4)));
666         proto_tree_add_text(ospf_lsr_tree, tvb, offset + 8, 4, "Advertising Router: %s", 
667                             ip_to_str(tvb_get_ptr(tvb, offset + 8, 4)));
668
669         offset += 12;
670     }
671 }
672
673 static void
674 dissect_ospf_ls_upd(tvbuff_t *tvb, int offset, proto_tree *tree, guint8 version)
675 {
676     proto_tree *ospf_lsa_upd_tree=NULL;
677     proto_item *ti;
678     guint32 lsa_nr;
679     guint32 lsa_counter; 
680
681     ti = proto_tree_add_text(tree, tvb, offset, -1, "LS Update Packet");
682     ospf_lsa_upd_tree = proto_item_add_subtree(ti, ett_ospf_lsa_upd);
683
684     lsa_nr = tvb_get_ntohl(tvb, offset);
685     proto_tree_add_text(ospf_lsa_upd_tree, tvb, offset, 4, "Number of LSAs: %u",
686                         lsa_nr);
687     /* skip to the beginning of the first LSA */
688     offset += 4; /* the LS Upd Packet contains only a 32 bit #LSAs field */
689     
690     lsa_counter = 0;
691     while (lsa_counter < lsa_nr) {
692         if ( version == OSPF_VERSION_2)
693             offset = dissect_ospf_v2_lsa(tvb, offset, ospf_lsa_upd_tree, TRUE);
694         else
695             if ( version == OSPF_VERSION_3)
696                 offset = dissect_ospf_v3_lsa(tvb, offset, ospf_lsa_upd_tree, TRUE);
697         lsa_counter += 1;
698     }
699 }
700
701 static void
702 dissect_ospf_ls_ack(tvbuff_t *tvb, int offset, proto_tree *tree, guint8 version)
703 {
704     /* the body of a LS Ack packet simply contains zero or more LSA Headers */
705     while (tvb_reported_length_remaining(tvb, offset) != 0) {
706         if ( version == OSPF_VERSION_2)
707             offset = dissect_ospf_v2_lsa(tvb, offset, tree, FALSE);
708         else
709             if ( version == OSPF_VERSION_3)
710               offset = dissect_ospf_v3_lsa(tvb, offset, tree, FALSE);
711     }
712 }
713
714 /*
715  * Returns if an LSA is opaque, i.e. requires special treatment 
716  */
717 static int
718 is_opaque(int lsa_type)
719 {
720     return (lsa_type >= OSPF_LSTYPE_OP_LINKLOCAL &&
721         lsa_type <= OSPF_LSTYPE_OP_ASWIDE);
722 }
723
724 /* MPLS/TE TLV types */
725 #define MPLS_TLV_ROUTER    1
726 #define MPLS_TLV_LINK      2
727
728 /* MPLS/TE Link STLV types */
729 enum {
730     MPLS_LINK_TYPE       = 1,
731     MPLS_LINK_ID,
732     MPLS_LINK_LOCAL_IF,
733     MPLS_LINK_REMOTE_IF,
734     MPLS_LINK_TE_METRIC,
735     MPLS_LINK_MAX_BW,
736     MPLS_LINK_MAX_RES_BW,
737     MPLS_LINK_UNRES_BW,
738     MPLS_LINK_COLOR,
739     MPLS_LINK_LOCAL_ID = 11,
740     MPLS_LINK_REMOTE_ID,
741     MPLS_LINK_PROTECTION = 14,
742     MPLS_LINK_IF_SWITCHING_DESC,
743     MPLS_LINK_SHARED_RISK_GROUP,
744 };
745
746 static const value_string mpls_link_stlv_str[] = {
747     {MPLS_LINK_TYPE, "Link Type"},
748     {MPLS_LINK_ID, "Link ID"},
749     {MPLS_LINK_LOCAL_IF, "Local Interface IP Address"},
750     {MPLS_LINK_REMOTE_IF, "Remote Interface IP Address"},
751     {MPLS_LINK_TE_METRIC, "Traffic Engineering Metric"},
752     {MPLS_LINK_MAX_BW, "Maximum Bandwidth"},
753     {MPLS_LINK_MAX_RES_BW, "Maximum Reservable Bandwidth"},
754     {MPLS_LINK_UNRES_BW, "Unreserved Bandwidth"},
755     {MPLS_LINK_COLOR, "Resource Class/Color"},
756     {MPLS_LINK_LOCAL_ID, "Link Local Identifier"},
757     {MPLS_LINK_REMOTE_ID, "Link Remote Identifier"},
758     {MPLS_LINK_PROTECTION, "Link Protection Type"},
759     {MPLS_LINK_IF_SWITCHING_DESC, "Interface Switching Capability Descriptor"},
760     {MPLS_LINK_SHARED_RISK_GROUP, "Shared Risk Link Group"},
761     {0, NULL},
762 };
763
764 /*
765  * From packet-rsvp.c
766  */
767 extern const value_string gmpls_lsp_enc_str[];
768 extern const value_string gmpls_switching_type_str[];
769
770 /* 
771  * Dissect MPLS/TE opaque LSA 
772  */
773 static void
774 dissect_ospf_lsa_mpls(tvbuff_t *tvb, int offset, proto_tree *tree,
775                       guint32 length)
776 {
777     proto_item *ti; 
778     proto_tree *mpls_tree;
779     proto_tree *tlv_tree;
780     proto_tree *stlv_tree;
781
782     int tlv_type;
783     int tlv_length;
784     int tlv_end_offset;
785
786     int stlv_type, stlv_len, stlv_offset;
787     char *stlv_name;
788     int i;
789
790     ti = proto_tree_add_text(tree, tvb, offset, length,
791                              "MPLS Traffic Engineering LSA");
792     mpls_tree = proto_item_add_subtree(ti, ett_ospf_lsa_mpls);
793
794     while (length != 0) {
795         tlv_type = tvb_get_ntohs(tvb, offset);
796         tlv_length = tvb_get_ntohs(tvb, offset + 2);
797         tlv_end_offset = offset + tlv_length + 4;
798
799         switch (tlv_type) {
800
801         case MPLS_TLV_ROUTER:
802             ti = proto_tree_add_text(mpls_tree, tvb, offset, tlv_length+4,
803                                      "Router Address: %s", 
804                                      ip_to_str(tvb_get_ptr(tvb, offset+4, 4)));
805             tlv_tree = proto_item_add_subtree(ti, ett_ospf_lsa_mpls_router);
806             proto_tree_add_text(tlv_tree, tvb, offset, 2, "TLV Type: 1 - Router Address");
807             proto_tree_add_text(tlv_tree, tvb, offset+2, 2, "TLV Length: %u",
808                                 tlv_length);
809             proto_tree_add_text(tlv_tree, tvb, offset+4, 4, "Router Address: %s",
810                                 ip_to_str(tvb_get_ptr(tvb, offset+4, 4)));
811             break;
812
813         case MPLS_TLV_LINK:
814             ti = proto_tree_add_text(mpls_tree, tvb, offset, tlv_length+4,
815                                      "Link Information");
816             tlv_tree = proto_item_add_subtree(ti, ett_ospf_lsa_mpls_link);
817             proto_tree_add_text(tlv_tree, tvb, offset, 2, "TLV Type: 2 - Link Information");
818             proto_tree_add_text(tlv_tree, tvb, offset+2, 2, "TLV Length: %u",
819                                 tlv_length);
820             stlv_offset = offset + 4;
821
822             /* Walk down the sub-TLVs for link information */
823             while (stlv_offset < tlv_end_offset) {
824                 stlv_type = tvb_get_ntohs(tvb, stlv_offset);
825                 stlv_len = tvb_get_ntohs(tvb, stlv_offset + 2);
826                 stlv_name = val_to_str(stlv_type, mpls_link_stlv_str, "Unknown sub-TLV");
827                 switch (stlv_type) {
828
829                 case MPLS_LINK_TYPE:
830                     ti = proto_tree_add_text(tlv_tree, tvb, stlv_offset, stlv_len+4,
831                                              "%s: %u", stlv_name,
832                                              tvb_get_guint8(tvb, stlv_offset + 4));
833                     stlv_tree = proto_item_add_subtree(ti, ett_ospf_lsa_mpls_link_stlv);
834                     proto_tree_add_text(stlv_tree, tvb, stlv_offset, 2,
835                                         "TLV Type: %u: %s", stlv_type, stlv_name);
836                     proto_tree_add_text(stlv_tree, tvb, stlv_offset+2, 2, "TLV Length: %u",
837                                         stlv_len);
838                     proto_tree_add_text(stlv_tree, tvb, stlv_offset+4, 1, "%s: %u", stlv_name,
839                                         tvb_get_guint8(tvb, stlv_offset + 4));
840                     break;
841
842                 case MPLS_LINK_ID:
843                     ti = proto_tree_add_text(tlv_tree, tvb, stlv_offset, stlv_len+4,
844                                              "%s: %s (%x)", stlv_name,
845                                              ip_to_str(tvb_get_ptr(tvb, stlv_offset + 4, 4)),
846                                              tvb_get_ntohl(tvb, stlv_offset + 4));
847                     stlv_tree = proto_item_add_subtree(ti, ett_ospf_lsa_mpls_link_stlv);
848                     proto_tree_add_text(stlv_tree, tvb, stlv_offset, 2,
849                                         "TLV Type: %u: %s", stlv_type, stlv_name);
850                     proto_tree_add_text(stlv_tree, tvb, stlv_offset+2, 2, "TLV Length: %u",
851                                         stlv_len);
852                     proto_tree_add_text(stlv_tree, tvb, stlv_offset+4, 4, "%s: %s (%x)", stlv_name,
853                                         ip_to_str(tvb_get_ptr(tvb, stlv_offset + 4, 4)),
854                                         tvb_get_ntohl(tvb, stlv_offset + 4));
855                     break;
856
857                 case MPLS_LINK_LOCAL_IF:
858                 case MPLS_LINK_REMOTE_IF:
859                     ti = proto_tree_add_text(tlv_tree, tvb, stlv_offset, stlv_len+4,
860                                              "%s", stlv_name);
861                     stlv_tree = proto_item_add_subtree(ti, ett_ospf_lsa_mpls_link_stlv);
862                     proto_tree_add_text(stlv_tree, tvb, stlv_offset, 2,
863                                         "TLV Type: %u: %s", stlv_type, stlv_name);
864                     proto_tree_add_text(stlv_tree, tvb, stlv_offset+2, 2, "TLV Length: %u",
865                                         stlv_len);
866                     /*   The Local/Remote Interface IP Address sub-TLV is TLV type 3/4, and is 4N
867                        octets in length, where N is the number of neighbor addresses. */
868                     for (i=0; i < stlv_len; i+=4)
869                       proto_tree_add_text(stlv_tree, tvb, stlv_offset+4+i, 4, "%s: %s", stlv_name,
870                                           ip_to_str(tvb_get_ptr(tvb, stlv_offset+4+i, 4)));
871                     break;
872
873                 case MPLS_LINK_TE_METRIC:
874                 case MPLS_LINK_COLOR:
875                     ti = proto_tree_add_text(tlv_tree, tvb, stlv_offset, stlv_len+4,
876                                              "%s: %u", stlv_name,
877                                              tvb_get_ntohl(tvb, stlv_offset + 4));
878                     stlv_tree = proto_item_add_subtree(ti, ett_ospf_lsa_mpls_link_stlv);
879                     proto_tree_add_text(stlv_tree, tvb, stlv_offset, 2,
880                                         "TLV Type: %u: %s", stlv_type, stlv_name);
881                     proto_tree_add_text(stlv_tree, tvb, stlv_offset+2, 2, "TLV Length: %u",
882                                         stlv_len);
883                     proto_tree_add_text(stlv_tree, tvb, stlv_offset+4, 4, "%s: %u", stlv_name,
884                                         tvb_get_ntohl(tvb, stlv_offset + 4));
885                     break;
886
887                 case MPLS_LINK_MAX_BW:
888                 case MPLS_LINK_MAX_RES_BW:
889                     ti = proto_tree_add_text(tlv_tree, tvb, stlv_offset, stlv_len+4,
890                                              "%s: %ld", stlv_name,
891                                              tvb_ieee_to_long(tvb, stlv_offset + 4));
892                     stlv_tree = proto_item_add_subtree(ti, ett_ospf_lsa_mpls_link_stlv);
893                     proto_tree_add_text(stlv_tree, tvb, stlv_offset, 2,
894                                         "TLV Type: %u: %s", stlv_type, stlv_name);
895                     proto_tree_add_text(stlv_tree, tvb, stlv_offset+2, 2, "TLV Length: %u",
896                                         stlv_len);
897                     proto_tree_add_text(stlv_tree, tvb, stlv_offset+4, 4, "%s: %ld", stlv_name,
898                                         tvb_ieee_to_long(tvb, stlv_offset + 4));
899                     break;
900
901                 case MPLS_LINK_UNRES_BW:
902                     ti = proto_tree_add_text(tlv_tree, tvb, stlv_offset, stlv_len+4,
903                                              "%s", stlv_name);
904                     stlv_tree = proto_item_add_subtree(ti, ett_ospf_lsa_mpls_link_stlv);
905                     proto_tree_add_text(stlv_tree, tvb, stlv_offset, 2,
906                                         "TLV Type: %u: %s", stlv_type, stlv_name);
907                     proto_tree_add_text(stlv_tree, tvb, stlv_offset+2, 2, "TLV Length: %u",
908                                         stlv_len);
909                     for (i = 0; i < 8; i++) {
910                         proto_tree_add_text(stlv_tree, tvb, stlv_offset+4+(i*4), 4,
911                                             "Pri %d: %ld bytes/s (%.0f bits/s)", i,
912                                             tvb_ieee_to_long(tvb, stlv_offset + 4 + i*4),
913                                             tvb_ieee_to_long(tvb, stlv_offset + 4 + i*4) * 8.0);
914                     }
915                     break;
916
917                 case MPLS_LINK_LOCAL_ID:
918                 case MPLS_LINK_REMOTE_ID:
919                     ti = proto_tree_add_text(tlv_tree, tvb, stlv_offset, stlv_len+4,
920                                              "%s: %d (0x%x)", stlv_name,
921                                              tvb_get_ntohl(tvb, stlv_offset + 4),
922                                              tvb_get_ntohl(tvb, stlv_offset + 4));
923                     stlv_tree = proto_item_add_subtree(ti, ett_ospf_lsa_mpls_link_stlv);
924                     proto_tree_add_text(stlv_tree, tvb, stlv_offset, 2,
925                                         "TLV Type: %u: %s", stlv_type, stlv_name);
926                     proto_tree_add_text(stlv_tree, tvb, stlv_offset+2, 2, "TLV Length: %u",
927                                         stlv_len);
928                     proto_tree_add_text(stlv_tree, tvb, stlv_offset+4, 4, "%s: %d (0x%x)", stlv_name,
929                                         tvb_get_ntohl(tvb, stlv_offset + 4),
930                                         tvb_get_ntohl(tvb, stlv_offset + 4));
931                     break;
932
933                 case MPLS_LINK_IF_SWITCHING_DESC:
934                     ti = proto_tree_add_text(tlv_tree, tvb, stlv_offset, stlv_len+4,
935                                              "%s", stlv_name);
936                     stlv_tree = proto_item_add_subtree(ti, ett_ospf_lsa_mpls_link_stlv);
937                     proto_tree_add_text(stlv_tree, tvb, stlv_offset, 2,
938                                         "TLV Type: %u: %s", stlv_type, stlv_name);
939                     proto_tree_add_text(stlv_tree, tvb, stlv_offset+2, 2, "TLV Length: %u",
940                                         stlv_len);
941                     proto_tree_add_text(stlv_tree, tvb, stlv_offset+4, 1, "Switching Type: %s", 
942                                         val_to_str(tvb_get_guint8(tvb,stlv_offset+4), 
943                                                    gmpls_switching_type_str, "Unknown (%d)"));
944                     proto_tree_add_text(stlv_tree, tvb, stlv_offset+5, 1, "Encoding: %s", 
945                                         val_to_str(tvb_get_guint8(tvb,stlv_offset+5), 
946                                                    gmpls_lsp_enc_str, "Unknown (%d)"));
947                     for (i = 0; i < 8; i++) {
948                         proto_tree_add_text(stlv_tree, tvb, stlv_offset+8+(i*4), 4,
949                                             "Pri %d: %ld bytes/s (%.0f bits/s)", i,
950                                             tvb_ieee_to_long(tvb, stlv_offset + 8 + i*4),
951                                             tvb_ieee_to_long(tvb, stlv_offset + 8 + i*4) * 8.0);
952                     }
953                     break;
954
955                 default:
956                     proto_tree_add_text(tlv_tree, tvb, stlv_offset, stlv_len+4,
957                                         "Unknown Link sub-TLV: %u", stlv_type);
958                     break;
959                 }
960                 stlv_offset += ((stlv_len+4+3)/4)*4;
961             }
962             break;
963
964         default:
965             ti = proto_tree_add_text(mpls_tree, tvb, offset, tlv_length+4, 
966                                      "Unknown LSA: %u", tlv_type);
967             tlv_tree = proto_item_add_subtree(ti, ett_ospf_lsa_mpls_link);
968             proto_tree_add_text(tlv_tree, tvb, offset, 2, "TLV Type: %u - Unknown",
969                                 tlv_type);
970             proto_tree_add_text(tlv_tree, tvb, offset+2, 2, "TLV Length: %u",
971                                 tlv_length);
972             proto_tree_add_text(tlv_tree, tvb, offset+4, tlv_length, "TLV Data");
973             break;
974         }
975
976         offset += tlv_length + 4;
977         length -= tlv_length + 4;
978     }
979 }
980
981 /*
982  * Dissect opaque LSAs
983  */
984 void
985 dissect_ospf_lsa_opaque(tvbuff_t *tvb, int offset, proto_tree *tree,
986                         guint8 ls_id_type, guint32 length)
987 {
988     switch (ls_id_type) {
989
990     case OSPF_LSA_MPLS_TE:
991         dissect_ospf_lsa_mpls(tvb, offset, tree, length);
992         break;
993
994     default:
995         proto_tree_add_text(tree, tvb, offset, length,
996                             "Unknown LSA Type %u", ls_id_type);
997         break;
998     } /* switch on opaque LSA id */
999 }
1000
1001 static int
1002 dissect_ospf_v2_lsa(tvbuff_t *tvb, int offset, proto_tree *tree,
1003                  gboolean disassemble_body)
1004 {
1005     proto_tree *ospf_lsa_tree;
1006     proto_item *ti; 
1007
1008     guint8               ls_type;
1009     guint16              ls_length;
1010     int                  end_offset;
1011     guint8               nr_links;
1012     guint16              nr_tos;
1013
1014     /* router LSA */
1015     guint8               link_type;
1016     guint16              link_counter;
1017     guint8               tos_counter;
1018     char                *link_type_str;
1019     char                *link_id;
1020
1021     /* AS-external LSA */
1022     guint8               options;
1023
1024     /* opaque LSA */
1025     guint8               ls_id_type;
1026
1027     ls_type = tvb_get_guint8(tvb, offset + 3);
1028     ls_length = tvb_get_ntohs(tvb, offset + 18);
1029     end_offset = offset + ls_length;
1030
1031     if (disassemble_body) {
1032         ti = proto_tree_add_text(tree, tvb, offset, ls_length,
1033                                  "%s (Type: %u)", val_to_str(ls_type, ls_type_vals,"Unkown"), ls_type); 
1034     } else {
1035         ti = proto_tree_add_text(tree, tvb, offset, OSPF_LSA_HEADER_LENGTH,
1036                                  "LSA Header"); 
1037     }
1038     ospf_lsa_tree = proto_item_add_subtree(ti, ett_ospf_lsa);
1039
1040     proto_tree_add_text(ospf_lsa_tree, tvb, offset, 2, "LS Age: %u seconds",
1041                         tvb_get_ntohs(tvb, offset));
1042     dissect_ospf_options(tvb, offset + 2, ospf_lsa_tree, OSPF_VERSION_2);
1043     proto_tree_add_text(ospf_lsa_tree, tvb, offset + 3, 1, "LSA Type: %u (%s)",
1044                         ls_type, val_to_str(ls_type,ls_type_vals,"Unknown"));
1045
1046     if (is_opaque(ls_type)) {
1047         ls_id_type = tvb_get_guint8(tvb, offset + 4);
1048         proto_tree_add_text(ospf_lsa_tree, tvb, offset + 4, 1, "Link State ID Opaque Type: %u",
1049                             ls_id_type);
1050         proto_tree_add_text(ospf_lsa_tree, tvb, offset + 5, 3, "Link State ID Opaque ID: %u",
1051                             tvb_get_ntoh24(tvb, offset + 5));
1052     } else {
1053         ls_id_type = 0;
1054         proto_tree_add_text(ospf_lsa_tree, tvb, offset + 4, 4, "Link State ID: %s",
1055                             ip_to_str(tvb_get_ptr(tvb, offset + 4, 4)));
1056     }
1057
1058     proto_tree_add_text(ospf_lsa_tree, tvb, offset + 8, 4, "Advertising Router: %s",
1059                         ip_to_str(tvb_get_ptr(tvb, offset + 8, 4)));
1060     proto_tree_add_text(ospf_lsa_tree, tvb, offset + 12, 4, "LS Sequence Number: 0x%04x",
1061                         tvb_get_ntohl(tvb, offset + 12));
1062     proto_tree_add_text(ospf_lsa_tree, tvb, offset + 16, 2, "LS Checksum: %04x",
1063                         tvb_get_ntohs(tvb, offset + 16));
1064
1065     proto_tree_add_text(ospf_lsa_tree, tvb, offset + 18, 2, "Length: %u",
1066                         ls_length);
1067
1068     /* skip past the LSA header to the body */
1069     offset += OSPF_LSA_HEADER_LENGTH;
1070     ls_length -= OSPF_LSA_HEADER_LENGTH;
1071
1072     if (!disassemble_body)
1073         return offset;
1074
1075     switch (ls_type){
1076
1077     case OSPF_LSTYPE_ROUTER:
1078         /* again: flags should be secified in detail */
1079         proto_tree_add_text(ospf_lsa_tree, tvb, offset, 1, "Flags: 0x%02x",
1080                             tvb_get_guint8(tvb, offset));
1081         nr_links = tvb_get_ntohs(tvb, offset + 2);
1082         proto_tree_add_text(ospf_lsa_tree, tvb, offset + 2, 2, "Number of Links: %u",
1083                             nr_links);
1084         offset += 4;
1085         /* nr_links links follow 
1086          * maybe we should put each of the links into its own subtree ???
1087          */
1088         for (link_counter = 1; link_counter <= nr_links; link_counter++) {
1089             /* check the Link Type and ID */
1090             link_type = tvb_get_guint8(tvb, offset + 8);
1091             switch (link_type) {
1092
1093             case OSPF_LINK_PTP:
1094                 link_type_str="Point-to-point connection to another router";
1095                 link_id="Neighboring router's Router ID";
1096                 break;
1097
1098             case OSPF_LINK_TRANSIT:
1099                 link_type_str="Connection to a transit network";
1100                 link_id="IP address of Designated Router";
1101                 break;
1102
1103             case OSPF_LINK_STUB:
1104                 link_type_str="Connection to a stub network";
1105                 link_id="IP network/subnet number";
1106                 break;
1107
1108             case OSPF_LINK_VIRTUAL:
1109                 link_type_str="Virtual link";
1110                 link_id="Neighboring router's Router ID";
1111                 break;
1112
1113             default:
1114                 link_type_str="Unknown link type";
1115                 link_id="Unknown link ID";
1116                 break;
1117             }
1118
1119             proto_tree_add_text(ospf_lsa_tree, tvb, offset, 4, "%s: %s", link_id,
1120                                 ip_to_str(tvb_get_ptr(tvb, offset, 4)));
1121
1122             /* link_data should be specified in detail (e.g. network mask) (depends on link type)*/
1123             proto_tree_add_text(ospf_lsa_tree, tvb, offset + 4, 4, "Link Data: %s",
1124                                 ip_to_str(tvb_get_ptr(tvb, offset + 4, 4)));
1125
1126             proto_tree_add_text(ospf_lsa_tree, tvb, offset + 8, 1, "Link Type: %u - %s",
1127                                 link_type, link_type_str);
1128             nr_tos = tvb_get_guint8(tvb, offset + 9);
1129             proto_tree_add_text(ospf_lsa_tree, tvb, offset + 9, 1, "Number of TOS metrics: %u",
1130                                 nr_tos);
1131             proto_tree_add_text(ospf_lsa_tree, tvb, offset + 10, 2, "TOS 0 metric: %u",
1132                                 tvb_get_ntohs(tvb, offset + 10));
1133
1134             offset += 12;
1135
1136             /* nr_tos metrics may follow each link 
1137              * ATTENTION: TOS metrics are not tested (I don't have TOS
1138              * based routing)
1139              * please send me a mail if it is/isn't working
1140              */
1141             for (tos_counter = 1; tos_counter <= nr_tos; tos_counter++) {
1142                 proto_tree_add_text(ospf_lsa_tree, tvb, offset, 4, "TOS: %u, Metric: %u",
1143                                     tvb_get_guint8(tvb, offset),
1144                                     tvb_get_ntohs(tvb, offset + 2));
1145                 offset += 4;
1146             }
1147         }
1148         break;
1149
1150     case OSPF_LSTYPE_NETWORK:
1151         proto_tree_add_text(ospf_lsa_tree, tvb, offset, 4, "Netmask: %s",
1152                                 ip_to_str(tvb_get_ptr(tvb, offset, 4)));
1153         offset += 4;
1154
1155         while (offset < end_offset) {
1156             proto_tree_add_text(ospf_lsa_tree, tvb, offset, 4, "Attached Router: %s",
1157                                 ip_to_str(tvb_get_ptr(tvb, offset, 4)));
1158             offset += 4;
1159         }
1160         break;
1161
1162     case OSPF_LSTYPE_SUMMERY:
1163     /* Type 3 and 4 LSAs have the same format */
1164     case OSPF_LSTYPE_ASBR:
1165         proto_tree_add_text(ospf_lsa_tree, tvb, offset, 4, "Netmask: %s",
1166                             ip_to_str(tvb_get_ptr(tvb, offset, 4)));
1167         offset += 4;
1168
1169         proto_tree_add_text(ospf_lsa_tree, tvb, offset, 4, "Metric: %u",
1170                             tvb_get_ntoh24(tvb, offset + 1));
1171         offset += 4;
1172
1173         /* TOS-specific information, if any */
1174         while (offset < end_offset) {
1175             proto_tree_add_text(ospf_lsa_tree, tvb, offset, 4, "TOS: %u, Metric: %u",
1176                                 tvb_get_guint8(tvb, offset),
1177                                 tvb_get_ntoh24(tvb, offset + 1));
1178             offset += 4;
1179         }
1180         break;
1181
1182     case OSPF_LSTYPE_ASEXT:
1183     case OSPF_LSTYPE_ASEXT7:
1184         proto_tree_add_text(ospf_lsa_tree, tvb, offset, 4, "Netmask: %s", 
1185                             ip_to_str(tvb_get_ptr(tvb, offset, 4)));
1186         offset += 4;
1187
1188         options = tvb_get_guint8(tvb, offset);
1189         if (options & 0x80) { /* check wether or not E bit is set */
1190             proto_tree_add_text(ospf_lsa_tree, tvb, offset, 1,
1191                     "External Type: Type 2 (metric is larger than any other link state path)");
1192         } else {
1193             proto_tree_add_text(ospf_lsa_tree, tvb, offset, 1,
1194                     "External Type: Type 1 (metric is specified in the same units as interface cost)");
1195         }
1196         /* the metric field of a AS-external LAS is specified in 3 bytes */
1197         proto_tree_add_text(ospf_lsa_tree, tvb, offset + 1, 3, "Metric: %u",
1198                             tvb_get_ntoh24(tvb, offset + 1));
1199         offset += 4;
1200
1201         proto_tree_add_text(ospf_lsa_tree, tvb, offset, 4, "Forwarding Address: %s", 
1202                             ip_to_str(tvb_get_ptr(tvb, offset, 4)));
1203         offset += 4;
1204
1205         proto_tree_add_text(ospf_lsa_tree, tvb, offset, 4, "External Route Tag: %u",
1206                             tvb_get_ntohl(tvb, offset));
1207         offset += 4;
1208
1209         /* TOS-specific information, if any */
1210         while (offset < end_offset) {
1211             options = tvb_get_guint8(tvb, offset);
1212             if (options & 0x80) { /* check wether or not E bit is set */
1213                 proto_tree_add_text(ospf_lsa_tree, tvb, offset, 1,
1214                         "External Type: Type 2 (metric is larger than any other link state path)");
1215             } else {
1216                 proto_tree_add_text(ospf_lsa_tree, tvb, offset, 1,
1217                         "External Type: Type 1 (metric is specified in the same units as interface cost)");
1218             }
1219             proto_tree_add_text(ospf_lsa_tree, tvb, offset, 4, "TOS: %u, Metric: %u",
1220                                 options & 0x7F,
1221                                 tvb_get_ntoh24(tvb, offset + 1));
1222             offset += 4;
1223
1224             proto_tree_add_text(ospf_lsa_tree, tvb, offset, 4, "Forwarding Address: %s", 
1225                                 ip_to_str(tvb_get_ptr(tvb, offset, 4)));
1226             offset += 4;
1227
1228             proto_tree_add_text(ospf_lsa_tree, tvb, offset, 4, "External Route Tag: %u",
1229                                 tvb_get_ntohl(tvb, offset));
1230             offset += 4;
1231         }
1232         break;
1233
1234     case OSPF_LSTYPE_OP_LINKLOCAL:
1235     case OSPF_LSTYPE_OP_AREALOCAL:
1236     case OSPF_LSTYPE_OP_ASWIDE:
1237         dissect_ospf_lsa_opaque(tvb, offset, ospf_lsa_tree, ls_id_type,
1238                                 ls_length);
1239         offset += ls_length;
1240         break;
1241
1242     default:
1243         /* unknown LSA type */
1244         proto_tree_add_text(ospf_lsa_tree, tvb, offset, ls_length,
1245                             "Unknown LSA Type");
1246         offset += ls_length;
1247         break;
1248     }
1249     /* return the offset of the next LSA */
1250     return offset;
1251 }
1252
1253 static int
1254 dissect_ospf_v3_lsa(tvbuff_t *tvb, int offset, proto_tree *tree,
1255                  gboolean disassemble_body)
1256 {
1257     proto_tree *ospf_lsa_tree;
1258     proto_item *ti; 
1259
1260     guint16              ls_type;
1261     guint16              ls_length;
1262     int                  end_offset;
1263     guint8               reserved;
1264
1265     /* router LSA */
1266     guint8               link_type;
1267     char                *link_type_str;
1268     guint32              metric;
1269
1270     guint8               router_lsa_flags;
1271     char                 router_lsa_flags_string[5];
1272
1273     guint8               router_priority;
1274     guint32              number_prefixes;
1275     guint8               prefix_length;
1276     guint16              reserved16;
1277
1278     guint16              referenced_ls_type;
1279
1280     guint8               flags;
1281     guint8               flags_string[4];
1282     guint32              external_route_tag;
1283
1284
1285     ls_type = tvb_get_ntohs(tvb, offset + 2);
1286     ls_length = tvb_get_ntohs(tvb, offset + 18);
1287     end_offset = offset + ls_length;
1288
1289     if (disassemble_body) {
1290         ti = proto_tree_add_text(tree, tvb, offset, ls_length,
1291                                  "%s (Type: 0x%04x)", val_to_str(ls_type, v3_ls_type_vals,"Unknown"), ls_type); 
1292     } else {
1293         ti = proto_tree_add_text(tree, tvb, offset, OSPF_LSA_HEADER_LENGTH,
1294                                  "LSA Header"); 
1295     }
1296     ospf_lsa_tree = proto_item_add_subtree(ti, ett_ospf_lsa);
1297
1298     proto_tree_add_text(ospf_lsa_tree, tvb, offset, 2, "LS Age: %u seconds",
1299                         tvb_get_ntohs(tvb, offset));
1300
1301     proto_tree_add_text(ospf_lsa_tree, tvb, offset + 2, 2, "LSA Type: 0x%04x (%s)",
1302                         ls_type, val_to_str(ls_type, v3_ls_type_vals,"Unkown"));
1303
1304     proto_tree_add_text(ospf_lsa_tree, tvb, offset + 4, 4, "Link State ID: %s",
1305                             ip_to_str(tvb_get_ptr(tvb, offset + 4, 4)));
1306
1307     proto_tree_add_text(ospf_lsa_tree, tvb, offset + 8, 4, "Advertising Router: %s",
1308                         ip_to_str(tvb_get_ptr(tvb, offset + 8, 4)));
1309     proto_tree_add_text(ospf_lsa_tree, tvb, offset + 12, 4, "LS Sequence Number: %d",
1310                         tvb_get_ntohl(tvb, offset + 12));
1311     proto_tree_add_text(ospf_lsa_tree, tvb, offset + 16, 2, "LS Checksum: %04x",
1312                         tvb_get_ntohs(tvb, offset + 16));
1313
1314     proto_tree_add_text(ospf_lsa_tree, tvb, offset + 18, 2, "Length: %u",
1315                         ls_length);
1316
1317     /* skip past the LSA header to the body */
1318     offset += OSPF_LSA_HEADER_LENGTH;
1319     ls_length -= OSPF_LSA_HEADER_LENGTH;
1320
1321     if (!disassemble_body)
1322         return offset;
1323
1324     switch (ls_type){
1325
1326
1327     case OSPF_V3_LSTYPE_ROUTER:
1328
1329       /* flags field in an router-lsa */
1330         router_lsa_flags=tvb_get_guint8(tvb,offset);
1331         if (router_lsa_flags & OSPF_V3_ROUTER_LSA_FLAG_B)
1332             router_lsa_flags_string[3] = 'B';
1333         else
1334             router_lsa_flags_string[3] = '.';
1335         if (router_lsa_flags & OSPF_V3_ROUTER_LSA_FLAG_E)
1336             router_lsa_flags_string[2] = 'E';
1337         else
1338             router_lsa_flags_string[2] = '.';
1339         if (router_lsa_flags & OSPF_V3_ROUTER_LSA_FLAG_V)
1340             router_lsa_flags_string[1] = 'V';
1341         else
1342             router_lsa_flags_string[1] = '.';
1343         if (router_lsa_flags & OSPF_V3_ROUTER_LSA_FLAG_W)
1344             router_lsa_flags_string[0] = 'W';
1345         else
1346             router_lsa_flags_string[0] = '.';
1347
1348         router_lsa_flags_string[4]=0;
1349
1350         proto_tree_add_text(ospf_lsa_tree, tvb, offset, 1, "Flags: 0x%02x (%s)",
1351                             router_lsa_flags, router_lsa_flags_string);
1352
1353         /* options field in an router-lsa */
1354         dissect_ospf_options(tvb, offset + 1, ospf_lsa_tree, OSPF_VERSION_3);
1355
1356         /* skip the router-lsa flags and options */
1357         offset+=4;
1358         ls_length-=4;
1359
1360         if (ls_length > 0)
1361              proto_tree_add_text(ospf_lsa_tree, tvb, offset, ls_length,
1362                    "Router Interfaces:"); 
1363
1364         /* scan all router-lsa router interfaces */
1365         /* maybe we should put each of the links into its own subtree ??? */
1366         while (ls_length > 0 ) {
1367
1368             /* check the type */
1369             link_type = tvb_get_guint8(tvb, offset);
1370             switch (link_type) {
1371
1372                 case OSPF_V3_LINK_PTP:
1373                     link_type_str="Point-to-point connection to another router";
1374                     break;
1375
1376                 case OSPF_V3_LINK_TRANSIT:
1377                     link_type_str="Connection to a transit network";
1378                     break;
1379
1380                 case OSPF_V3_LINK_RESERVED:
1381                     link_type_str="Connection to a stub network";
1382                     break;
1383
1384                 case OSPF_V3_LINK_VIRTUAL:
1385                     link_type_str="Virtual link";
1386                     break;
1387
1388                 default:
1389                     link_type_str="Unknown link type";
1390                     break;
1391             }
1392
1393             proto_tree_add_text(ospf_lsa_tree, tvb, offset, 1, "Type: %u (%s)", link_type,link_type_str);
1394
1395             /* reserved field */
1396             reserved = tvb_get_guint8(tvb, offset+1);
1397             proto_tree_add_text(ospf_lsa_tree, tvb, offset+1, 1,
1398                (reserved == 0 ? "Reserved: %u" : "Reserved: %u (incorrect, should be 0)"),reserved);
1399
1400             /* metric */
1401             metric=tvb_get_ntohs(tvb, offset+2);
1402             proto_tree_add_text(ospf_lsa_tree, tvb, offset + 2, 2,"Metric: %u",metric);
1403
1404             /* Interface ID */
1405             proto_tree_add_text(ospf_lsa_tree, tvb, offset + 4, 4, "Interface ID: %u",
1406                         tvb_get_ntohl(tvb, offset + 4));
1407
1408             /* Neighbor Interface ID */
1409             proto_tree_add_text(ospf_lsa_tree, tvb, offset + 8, 4, "Neighbor Interface ID: %u",
1410                         tvb_get_ntohl(tvb, offset + 8));
1411
1412             /* Neighbor Router ID */
1413             proto_tree_add_text(ospf_lsa_tree, tvb, offset + 12, 4, "Neighbor Router ID: %s",
1414                 ip_to_str(tvb_get_ptr(tvb, offset + 12, 4)));
1415
1416             /* skip to the (possible) next entry */
1417             offset+=16;
1418             ls_length-=16;
1419
1420         }
1421         break;
1422
1423     case OSPF_V3_LSTYPE_NETWORK:
1424
1425         /* reserved field */
1426         reserved = tvb_get_guint8(tvb, offset);
1427         proto_tree_add_text(ospf_lsa_tree, tvb, offset, 1,
1428                (reserved == 0 ? "Reserved: %u" : "Reserved: %u (incorrect, should be 0)"),reserved);
1429
1430         /* options field in an network-lsa */
1431         dissect_ospf_options(tvb, offset + 1, ospf_lsa_tree, OSPF_VERSION_3);
1432
1433         offset += 4;
1434         ls_length-=4;
1435
1436         while (ls_length > 0 ) {
1437             proto_tree_add_text(ospf_lsa_tree, tvb, offset, 4, "Attached Router: %s",
1438                                 ip_to_str(tvb_get_ptr(tvb, offset, 4)));
1439             ls_length-=4;
1440             offset += 4;
1441         }
1442         break;
1443
1444
1445     case OSPF_V3_LSTYPE_INTER_AREA_PREFIX:
1446
1447         /* reserved field */
1448         reserved = tvb_get_guint8(tvb, offset);
1449         proto_tree_add_text(ospf_lsa_tree, tvb, offset, 1,
1450                (reserved == 0 ? "Reserved: %u" : "Reserved: %u (incorrect, should be 0)"),reserved);
1451
1452         /* metric */
1453         metric=tvb_get_ntoh24(tvb, offset+11);
1454         proto_tree_add_text(ospf_lsa_tree, tvb, offset + 1, 3,"Metric: %u",metric);
1455
1456         /* prefix length */
1457         prefix_length=tvb_get_guint8(tvb, offset+4);
1458         proto_tree_add_text(ospf_lsa_tree, tvb, offset+4, 1, "PrefixLength: %u",prefix_length);
1459
1460         /* prefix options */
1461         dissect_ospf_v3_prefix_options(tvb, offset+5, ospf_lsa_tree);
1462
1463         /* 16 bits reserved */
1464         reserved16=tvb_get_ntohs(tvb, offset+6);
1465         proto_tree_add_text(ospf_lsa_tree, tvb, offset+6, 2,
1466                (reserved16 == 0 ? "Reserved: %u" : "Reserved: %u (incorrect, should be 0)"),reserved16);
1467
1468         offset+=8;
1469
1470         /* address_prefix */
1471         dissect_ospf_v3_address_prefix(tvb, offset, prefix_length, ospf_lsa_tree);
1472
1473         offset+=(prefix_length+31)/32*4;
1474
1475         break;
1476
1477
1478     case OSPF_V3_LSTYPE_INTER_AREA_ROUTER:
1479
1480         /* reserved field */
1481         reserved = tvb_get_guint8(tvb, offset);
1482         proto_tree_add_text(ospf_lsa_tree, tvb, offset, 1,
1483                (reserved == 0 ? "Reserved: %u" : "Reserved: %u (incorrect, should be 0)"),reserved);
1484
1485         /* options field in an inter-area-router-lsa */
1486         dissect_ospf_options(tvb, offset + 1, ospf_lsa_tree, OSPF_VERSION_3);
1487
1488         /* reserved field */
1489         reserved = tvb_get_guint8(tvb, offset+4);
1490         proto_tree_add_text(ospf_lsa_tree, tvb, offset+4, 1,
1491                (reserved == 0 ? "Reserved: %u" : "Reserved: %u (incorrect, should be 0)"),reserved);
1492
1493         /* metric */
1494         metric=tvb_get_ntoh24(tvb, offset+6);
1495         proto_tree_add_text(ospf_lsa_tree, tvb, offset + 6, 3,"Metric: %u",metric);
1496
1497         /* Destination Router ID */
1498         proto_tree_add_text(ospf_lsa_tree, tvb, offset + 8, 4, "Destination Router ID: %s",
1499                 ip_to_str(tvb_get_ptr(tvb, offset + 8, 4)));
1500
1501         offset+=12;
1502         break;
1503
1504
1505     case OSPF_V3_LSTYPE_AS_EXTERNAL:
1506
1507         /* flags */
1508         flags=tvb_get_guint8(tvb, offset);
1509         if (flags & OSPF_V3_AS_EXTERNAL_FLAG_E)
1510             flags_string[0] = 'E';
1511         else
1512             flags_string[0] = '.';
1513         if (flags & OSPF_V3_AS_EXTERNAL_FLAG_F)
1514             flags_string[1] = 'F';
1515         else
1516             flags_string[1] = '.';
1517         if (flags & OSPF_V3_AS_EXTERNAL_FLAG_T)
1518             flags_string[2] = 'T';
1519         else
1520             flags_string[2] = '.';
1521
1522         flags_string[3]=0;
1523
1524         proto_tree_add_text(ospf_lsa_tree, tvb, offset, 1, "Flags: 0x%02x (%s)",
1525                             flags, flags_string);
1526         
1527         /* 24 bits metric */
1528         metric=tvb_get_ntoh24(tvb, offset+1);
1529         proto_tree_add_text(ospf_lsa_tree, tvb, offset+1, 3,
1530                                 "Metric: %u", metric);
1531
1532         /* prefix length */
1533         prefix_length=tvb_get_guint8(tvb, offset+4);
1534         proto_tree_add_text(ospf_lsa_tree, tvb, offset+4, 1, "PrefixLength: %u",prefix_length);
1535
1536         /* prefix options */
1537         dissect_ospf_v3_prefix_options(tvb, offset+5, ospf_lsa_tree);
1538
1539         /* referenced LS type */
1540         referenced_ls_type=tvb_get_ntohs(tvb, offset+6);
1541         proto_tree_add_text(ospf_lsa_tree, tvb, offset+6, 2,"Referenced LS type 0x%04x (%s)",
1542                             referenced_ls_type, val_to_str(referenced_ls_type, v3_ls_type_vals, "Unknown"));
1543
1544         offset+=8;
1545
1546         /* address_prefix */
1547         dissect_ospf_v3_address_prefix(tvb, offset, prefix_length, ospf_lsa_tree);
1548        
1549         offset+=(prefix_length+31)/32*4;
1550
1551         /* Forwarding Address (optional - only if F-flag is on) */
1552         if ( (offset < end_offset) && (flags & OSPF_V3_AS_EXTERNAL_FLAG_F) ) {
1553             proto_tree_add_text(ospf_lsa_tree, tvb, offset, 16,"Forwarding Address: %s",
1554               ip6_to_str((struct e_in6_addr *)tvb_get_ptr(tvb, offset, 16)));
1555
1556             offset+=16;
1557         }
1558
1559         /* External Route Tag (optional - only if T-flag is on) */
1560         if ( (offset < end_offset) && (flags & OSPF_V3_AS_EXTERNAL_FLAG_T) ) {
1561             external_route_tag=tvb_get_ntohl(tvb, offset);
1562             proto_tree_add_text(ospf_lsa_tree, tvb, offset, 4,"External Route Tag: 0x%04x",
1563                                 external_route_tag);
1564
1565             offset+=4;
1566         }
1567
1568         /* Referenced Link State ID (optional - only if Referenced LS type is non-zero */
1569         if ( (offset < end_offset) && (referenced_ls_type != 0) ) {
1570             proto_tree_add_text(ospf_lsa_tree, tvb, offset, 4, "Referenced Link State ID: %s", 
1571                             ip_to_str(tvb_get_ptr(tvb, offset, 4)));
1572             offset+=4;
1573         }
1574
1575         break;
1576
1577     case OSPF_V3_LSTYPE_LINK:
1578
1579         /* router priority */
1580         router_priority=tvb_get_guint8(tvb, offset);
1581         proto_tree_add_text(ospf_lsa_tree, tvb, offset, 1, "Router Priority: %u", router_priority);
1582
1583         /* options field in an link-lsa */
1584         dissect_ospf_options(tvb, offset + 1, ospf_lsa_tree, OSPF_VERSION_3);
1585
1586         /* Link-local Interface Address */
1587         proto_tree_add_text(ospf_lsa_tree, tvb, offset + 4, 16, "Link-local Interface Address: %s",
1588            ip6_to_str((struct e_in6_addr *)tvb_get_ptr(tvb, offset + 4, 16)));
1589
1590         /* Number prefixes */
1591         number_prefixes=tvb_get_ntohl(tvb, offset + 20);
1592         proto_tree_add_text(ospf_lsa_tree, tvb, offset+20, 4, "# prefixes: %d",number_prefixes);
1593
1594         offset+=24;
1595
1596         while (number_prefixes > 0) {
1597
1598             /* prefix length */
1599             prefix_length=tvb_get_guint8(tvb, offset);
1600             proto_tree_add_text(ospf_lsa_tree, tvb, offset, 1, "PrefixLength: %u",prefix_length);
1601
1602             /* prefix options */
1603             dissect_ospf_v3_prefix_options(tvb, offset+1, ospf_lsa_tree);
1604
1605             /* 16 bits reserved */
1606             reserved16=tvb_get_ntohs(tvb, offset+2);
1607             proto_tree_add_text(ospf_lsa_tree, tvb, offset+2, 2,
1608                (reserved16 == 0 ? "Reserved: %u" : "Reserved: %u (incorrect, should be 0)"),reserved16);
1609
1610             offset+=4;
1611
1612             /* address_prefix */
1613             dissect_ospf_v3_address_prefix(tvb, offset, prefix_length, ospf_lsa_tree);
1614        
1615             offset+=(prefix_length+31)/32*4;
1616
1617             number_prefixes--;
1618
1619         }             
1620         break;
1621
1622     case OSPF_V3_LSTYPE_INTRA_AREA_PREFIX:
1623
1624         /* # prefixes */
1625         number_prefixes=tvb_get_ntohs(tvb, offset);
1626         proto_tree_add_text(ospf_lsa_tree, tvb, offset, 2,"# prefixes: %u",number_prefixes);
1627
1628         /* referenced LS type */
1629         referenced_ls_type=tvb_get_ntohs(tvb, offset+2);
1630         proto_tree_add_text(ospf_lsa_tree, tvb, offset+2, 2,"Referenced LS type 0x%04x (%s)",
1631                             referenced_ls_type, val_to_str(referenced_ls_type, v3_ls_type_vals, "Unknown"));
1632
1633         /* Referenced Link State ID */
1634         proto_tree_add_text(ospf_lsa_tree, tvb, offset + 4, 4, "Referenced Link State ID: %s", 
1635                             ip_to_str(tvb_get_ptr(tvb, offset + 4, 4)));
1636
1637         /* Referenced Advertising Router */
1638         proto_tree_add_text(ospf_lsa_tree, tvb, offset + 8, 4, "Referenced Advertising Router: %s", 
1639                             ip_to_str(tvb_get_ptr(tvb, offset + 8, 4)));
1640
1641         offset+=12;
1642
1643         while (number_prefixes > 0) {
1644
1645             /* prefix length */
1646             prefix_length=tvb_get_guint8(tvb, offset);
1647             proto_tree_add_text(ospf_lsa_tree, tvb, offset, 1, "PrefixLength: %u",prefix_length);
1648
1649             /* prefix options */
1650             dissect_ospf_v3_prefix_options(tvb, offset+1, ospf_lsa_tree);
1651
1652             /* 16 bits metric */
1653             metric=tvb_get_ntohs(tvb, offset+2);
1654             proto_tree_add_text(ospf_lsa_tree, tvb, offset+2, 2,
1655                                 "Metric: %u", metric);
1656
1657             offset+=4;
1658
1659             /* address_prefix */
1660             dissect_ospf_v3_address_prefix(tvb, offset, prefix_length, ospf_lsa_tree);
1661        
1662             offset+=(prefix_length+31)/32*4;
1663
1664             number_prefixes--;
1665         }
1666         break;
1667
1668     default:
1669         /* unknown LSA type */
1670         proto_tree_add_text(ospf_lsa_tree, tvb, offset, ls_length,
1671                             "Unknown LSA Type 0x%04x",ls_type);
1672         offset += ls_length;
1673         break;
1674     }
1675     /* return the offset of the next LSA */
1676     return offset;
1677 }
1678
1679
1680 static void
1681 dissect_ospf_options(tvbuff_t *tvb, int offset, proto_tree *tree, guint8 version)
1682 {
1683     guint8 options_ospfv2;
1684     guint32 options_ospfv3;
1685     char options_string[20] = "";
1686
1687     /* ATTENTION !!! no check for length of options string  - with OSPFv3 maximum length is 14 characters */
1688
1689     switch ( version ) {
1690
1691         case OSPF_VERSION_2:
1692
1693             options_ospfv2 = tvb_get_guint8(tvb, offset);
1694
1695             if (options_ospfv2 & OSPF_V2_OPTIONS_E)
1696                 strcat(options_string, "E");
1697
1698             if (options_ospfv2 & OSPF_V2_OPTIONS_MC) {
1699                 if (options_string[0] != '\0')
1700                     strcat(options_string, "/");
1701                 strcat(options_string, "MC");
1702             }
1703
1704             if (options_ospfv2 & OSPF_V2_OPTIONS_NP) {
1705                 if (options_string[0] != '\0')
1706                     strcat(options_string, "/");
1707                 strcat(options_string, "NP");
1708             }
1709
1710             if (options_ospfv2 & OSPF_V2_OPTIONS_EA) {
1711                 if (options_string[0] != '\0')
1712                     strcat(options_string, "/");
1713                 strcat(options_string, "EA");
1714             }
1715
1716             if (options_ospfv2 & OSPF_V2_OPTIONS_DC) {
1717                 if (options_string[0] != '\0')
1718                     strcat(options_string, "/");
1719                 strcat(options_string, "DC");
1720             }
1721
1722             if (options_ospfv2 & OSPF_V2_OPTIONS_O) {
1723                 if (options_string[0] != '\0')
1724                     strcat(options_string, "/");
1725                 strcat(options_string, "O");
1726             }
1727
1728             if (options_ospfv2 & OSPF_V2_OPTIONS_DN) {
1729                 if (options_string[0] != '\0')
1730                     strcat(options_string, "/");
1731                 strcat(options_string, "DN");  
1732             }
1733
1734             proto_tree_add_text(tree, tvb, offset, 1, "Options: 0x%x (%s)",
1735                         options_ospfv2, options_string);
1736             break;
1737
1738
1739         case OSPF_VERSION_3:
1740
1741             options_ospfv3 = tvb_get_ntoh24(tvb, offset);
1742
1743             if (options_ospfv3 & OSPF_V3_OPTIONS_V6)
1744                 strcat(options_string, "V6");
1745
1746             if (options_ospfv3 & OSPF_V3_OPTIONS_E)
1747                 if (options_string[0] != '\0')
1748                     strcat(options_string, "/");
1749                 strcat(options_string, "E");
1750
1751             if (options_ospfv3 & OSPF_V3_OPTIONS_MC) {
1752                 if (options_string[0] != '\0')
1753                     strcat(options_string, "/");
1754                 strcat(options_string, "MC");
1755             }
1756
1757             if (options_ospfv3 & OSPF_V3_OPTIONS_N) {
1758                 if (options_string[0] != '\0')
1759                     strcat(options_string, "/");
1760                 strcat(options_string, "N");
1761             }
1762
1763             if (options_ospfv3 & OSPF_V3_OPTIONS_R) {
1764                 if (options_string[0] != '\0')
1765                     strcat(options_string, "/");
1766                 strcat(options_string, "R");
1767             }
1768
1769             if (options_ospfv3 & OSPF_V3_OPTIONS_DC) {
1770                 if (options_string[0] != '\0')
1771                     strcat(options_string, "/");
1772                 strcat(options_string, "DC");
1773             }
1774
1775             proto_tree_add_text(tree, tvb, offset, 3, "Options: 0x%x (%s)",
1776                         options_ospfv3, options_string);
1777             break;
1778
1779         default:
1780             break;
1781     }
1782
1783 }
1784
1785
1786 static void dissect_ospf_v3_prefix_options(tvbuff_t *tvb, int offset, proto_tree *tree)
1787 {
1788
1789     guint8 prefix_options;
1790     char prefix_options_string[11];
1791     guint8 position;
1792
1793     position=0;
1794     
1795     prefix_options=tvb_get_guint8(tvb, offset);
1796
1797     strcpy(prefix_options_string,"");
1798
1799     if (prefix_options & OSPF_V3_PREFIX_OPTION_P) {
1800         strcat(prefix_options_string, "P");
1801         position++;
1802     }
1803
1804     if (prefix_options & OSPF_V3_PREFIX_OPTION_MC) {
1805         if ( (position > 0) && (prefix_options_string[position-1] != '/') ) {
1806             strcat(prefix_options_string, "/");
1807             position++;
1808         }
1809         strcat(prefix_options_string, "MC");
1810         position+=2;
1811     }
1812
1813     if (prefix_options & OSPF_V3_PREFIX_OPTION_LA) {
1814         if ( (position > 0) && (prefix_options_string[position-1] != '/') ) {
1815             strcat(prefix_options_string, "/");
1816             position++;
1817         }
1818         strcat(prefix_options_string, "LA");
1819         position+=2;
1820     }
1821
1822     if (prefix_options & OSPF_V3_PREFIX_OPTION_NU) {
1823         if ( (position > 0) && (prefix_options_string[position-1] != '/') ) {
1824             strcat(prefix_options_string, "/");
1825             position++;
1826         }
1827         strcat(prefix_options_string, "NU");
1828     }
1829
1830     prefix_options_string[10]=0;
1831
1832     proto_tree_add_text(tree, tvb, offset, 1, "PrefixOptions: 0x%02x (%s)",prefix_options, prefix_options_string);
1833
1834 }
1835
1836
1837 static void dissect_ospf_v3_address_prefix(tvbuff_t *tvb, int offset, int prefix_length, proto_tree *tree)
1838 {
1839
1840     guint8 value;
1841     guint8 position;
1842     guint8 bufpos;
1843     gchar  buffer[32+7];
1844     gchar  bytebuf[3];
1845     guint8 bytes_to_process;
1846     int start_offset;
1847
1848     start_offset=offset;
1849     position=0;
1850     bufpos=0;
1851     bytes_to_process=((prefix_length+31)/32)*4;
1852
1853     while (bytes_to_process > 0 ) {
1854
1855         value=tvb_get_guint8(tvb, offset);
1856
1857         if ( (position > 0) && ( (position%2) == 0 ) )
1858             buffer[bufpos++]=':';
1859
1860         sprintf(bytebuf,"%02x",value);
1861         buffer[bufpos++]=bytebuf[0];        
1862         buffer[bufpos++]=bytebuf[1];        
1863         
1864         position++;
1865         offset++;
1866         bytes_to_process--;
1867     }
1868
1869     buffer[bufpos]=0;  
1870     proto_tree_add_text(tree, tvb, start_offset, ((prefix_length+31)/32)*4, "Address Prefix: %s",buffer);
1871
1872 }
1873
1874
1875 void
1876 proto_register_ospf(void)
1877 {
1878 /*        static hf_register_info hf[] = {
1879                 { &variable,
1880                 { "Name",           "ospf.abbreviation", TYPE, VALS_POINTER }},
1881         };*/
1882     static gint *ett[] = {
1883         &ett_ospf,
1884         &ett_ospf_hdr,
1885         &ett_ospf_hello,
1886         &ett_ospf_desc,
1887         &ett_ospf_lsr,
1888         &ett_ospf_lsa,
1889         &ett_ospf_lsa_upd,
1890         &ett_ospf_lsa_mpls,
1891         &ett_ospf_lsa_mpls_router,
1892         &ett_ospf_lsa_mpls_link,
1893         &ett_ospf_lsa_mpls_link_stlv
1894     };
1895
1896     proto_ospf = proto_register_protocol("Open Shortest Path First",
1897                                          "OSPF", "ospf");
1898  /*       proto_register_field_array(proto_ospf, hf, array_length(hf));*/
1899     proto_register_subtree_array(ett, array_length(ett));
1900 }
1901
1902 void
1903 proto_reg_handoff_ospf(void)
1904 {
1905     dissector_handle_t ospf_handle;
1906
1907     ospf_handle = create_dissector_handle(dissect_ospf, proto_ospf);
1908     dissector_add("ip.proto", IP_PROTO_OSPF, ospf_handle);
1909     data_handle = find_dissector("data");
1910 }