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