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