dbd8c3a248482528ff5b41d10db75a223451b933
[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.22 2000/04/16 22:46:20 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  *
12  * TOS - support is not fully implemented
13  * 
14  * Ethereal - Network traffic analyzer
15  * By Gerald Combs <gerald@zing.org>
16  * Copyright 1998 Gerald Combs
17  * 
18  * 
19  * This program is free software; you can redistribute it and/or
20  * modify it under the terms of the GNU General Public License
21  * as published by the Free Software Foundation; either version 2
22  * of the License, or (at your option) any later version.
23  * 
24  * This program is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27  * GNU General Public License for more details.
28  * 
29  * You should have received a copy of the GNU General Public License
30  * along with this program; if not, write to the Free Software
31  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
32  */
33  
34 #ifdef HAVE_CONFIG_H
35 # include "config.h"
36 #endif
37
38 #ifdef HAVE_SYS_TYPES_H
39 # include <sys/types.h>
40 #endif
41
42 #ifdef HAVE_NETINET_IN_H
43 # include <netinet/in.h>
44 #endif
45
46 #include <stdio.h>
47 #include <string.h>
48
49 #include <glib.h>
50 #include "packet.h"
51 #include "packet-ospf.h"
52 #include "packet-ip.h"
53 #include "ieee-float.h"
54
55 static int proto_ospf = -1;
56
57 static gint ett_ospf = -1;
58 static gint ett_ospf_hdr = -1;
59 static gint ett_ospf_hello = -1;
60 static gint ett_ospf_desc = -1;
61 static gint ett_ospf_lsr = -1;
62 static gint ett_ospf_lsa = -1;
63 static gint ett_ospf_lsa_upd = -1;
64
65 /* Trees for opaque LSAs */
66 static gint ett_ospf_lsa_mpls = -1;
67 static gint ett_ospf_lsa_mpls_router = -1;
68 static gint ett_ospf_lsa_mpls_link = -1;
69 static gint ett_ospf_lsa_mpls_link_stlv = -1;
70
71 static void dissect_ospf_hello(const u_char*, int, frame_data*, proto_tree*);
72 static void dissect_ospf_db_desc(const u_char*, int, frame_data*, proto_tree*); 
73 static void dissect_ospf_ls_req(const u_char*, int, frame_data*, proto_tree*); 
74 static void dissect_ospf_ls_upd(const u_char*, int, frame_data*, proto_tree*); 
75 static void dissect_ospf_ls_ack(const u_char*, int, frame_data*, proto_tree*); 
76
77 /* dissect_ospf_lsa returns the length of the LSA 
78  * if disassemble_body is set to FALSE (e.g. in LSA ACK 
79  * packets), the LSA-header length is returned (20)
80  */
81 static int dissect_ospf_lsa(const u_char*, int, frame_data*, proto_tree*, int disassemble_body); 
82
83 static void 
84 dissect_ospf(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
85     e_ospfhdr ospfh;
86     e_ospf_crypto *crypto;
87     int i, saved_len, ospflen;
88
89     proto_tree *ospf_tree = NULL;
90         proto_item *ti; 
91     proto_tree *ospf_header_tree;
92     char auth_data[(2 * 16) + 1]="";
93     char *packet_type;
94     static value_string pt_vals[] = { {OSPF_HELLO,   "Hello Packet"   },
95                                       {OSPF_DB_DESC, "DB Descr."      },
96                                       {OSPF_LS_REQ,  "LS Request"     },
97                                       {OSPF_LS_UPD,  "LS Update"      },
98                                       {OSPF_LS_ACK,  "LS Acknowledge" },
99                                       {0,             NULL            } };
100
101     memcpy(&ospfh, &pd[offset], sizeof(e_ospfhdr));
102
103     packet_type = match_strval(ospfh.packet_type, pt_vals);
104     if (check_col(fd, COL_PROTOCOL))
105         col_add_str(fd, COL_PROTOCOL, "OSPF");
106     if (check_col(fd, COL_INFO)) {
107         if (packet_type != NULL)
108             col_add_str(fd, COL_INFO, packet_type); 
109         else
110             col_add_fstr(fd, COL_INFO, "Unknown (%d)", ospfh.packet_type); 
111     }  
112
113     if (tree) {
114         ti = proto_tree_add_item(tree, proto_ospf, offset, ntohs(ospfh.length), NULL);
115         ospf_tree = proto_item_add_subtree(ti, ett_ospf);
116
117         ti = proto_tree_add_text(ospf_tree, offset, OSPF_HEADER_LENGTH, "OSPF Header"); 
118         ospf_header_tree = proto_item_add_subtree(ti, ett_ospf_hdr);
119
120         proto_tree_add_text(ospf_header_tree, offset, 1, "OSPF Version: %d", ospfh.version);  
121         proto_tree_add_text(ospf_header_tree, offset + 1 , 1, "OSPF Packet Type: %d (%s)", 
122                                                            ospfh.packet_type,
123                                                            (packet_type != NULL ?
124                                                              packet_type :
125                                                              "Unknown"));
126         proto_tree_add_text(ospf_header_tree, offset + 2 , 2, "Packet Length: %d", 
127                                                            ntohs(ospfh.length));
128         proto_tree_add_text(ospf_header_tree, offset + 4 , 4, "Source OSPF Router ID: %s", 
129
130                                                            ip_to_str((guint8 *) &(ospfh.routerid)));
131         if (!(ospfh.area)) {
132            proto_tree_add_text(ospf_header_tree, offset + 8 , 4, "Area ID: Backbone");
133         } else {
134            proto_tree_add_text(ospf_header_tree, offset + 8 , 4, "Area ID: %s", ip_to_str((guint8 *) &(ospfh.area)));
135         }
136         proto_tree_add_text(ospf_header_tree, offset + 12 , 2, "Packet Checksum: 0x%x",
137               ntohs(ospfh.checksum));
138         switch( ntohs(ospfh.auth_type) ) {
139             case OSPF_AUTH_NONE:
140                  proto_tree_add_text(ospf_header_tree, offset + 14 , 2, "Auth Type: none");
141                  proto_tree_add_text(ospf_header_tree, offset + 16 , 8, "Auth Data (none)");
142                  break;
143             case OSPF_AUTH_SIMPLE:
144                  proto_tree_add_text(ospf_header_tree, offset + 14 , 2, "Auth Type: simple");
145                  strncpy(auth_data, (char *) &ospfh.auth_data, 8);
146                  proto_tree_add_text(ospf_header_tree, offset + 16 , 8, "Auth Data: %s", auth_data);
147                  break;
148             case OSPF_AUTH_CRYPT:
149                  crypto = (e_ospf_crypto *)ospfh.auth_data;
150                  proto_tree_add_text(ospf_header_tree, offset + 14 , 2, "Auth Type: crypt");
151                  proto_tree_add_text(ospf_header_tree, offset + 18 , 1, "Auth Key ID: %d",
152                                      crypto->key_id);
153                  proto_tree_add_text(ospf_header_tree, offset + 19 , 1, "Auth Data Length: %d",
154                                      crypto->length);
155                  proto_tree_add_text(ospf_header_tree, offset + 20 , 4, "Auth Crypto Sequence Number: 0x%lx",
156                                  (unsigned long)ntohl(crypto->sequence_num));
157   
158                  ospflen = ntohs(ospfh.length);
159                  for (i = 0; i < crypto->length && i < (sizeof(auth_data)/2); i++)
160                      sprintf(&auth_data[i*2],"%02x",pd[offset + ospflen + i]);
161                  proto_tree_add_text(ospf_header_tree, offset + ospflen , 16, "Auth Data: %s", auth_data);
162                  break;
163             default:
164                  proto_tree_add_text(ospf_header_tree, offset + 14 , 2, "Auth Type (unknown)");
165                  proto_tree_add_text(ospf_header_tree, offset + 16 , 8, "Auth Data (unknown)");
166         }
167
168     }
169
170     /* Temporarily adjust the captured length to match the size of the OSPF
171      * packet (since the dissect routines use it to work out where the end of
172      * the ospf packet is).
173      */
174     saved_len = pi.captured_len;
175     if (BYTES_ARE_IN_FRAME(offset, ntohs(ospfh.length))) {
176       pi.captured_len = offset + ntohs(ospfh.length);
177     }
178
179     /*  Skip over header */
180     offset += OSPF_HEADER_LENGTH;
181
182     switch(ospfh.packet_type){
183         case OSPF_HELLO:
184             dissect_ospf_hello(pd, offset, fd, ospf_tree); 
185             break;
186         case OSPF_DB_DESC:
187             dissect_ospf_db_desc(pd, offset, fd, ospf_tree);   
188             break;
189         case OSPF_LS_REQ:
190             dissect_ospf_ls_req(pd, offset, fd, ospf_tree);   
191             break;
192         case OSPF_LS_UPD:
193             dissect_ospf_ls_upd(pd, offset, fd, ospf_tree);
194             break;
195         case OSPF_LS_ACK:
196             dissect_ospf_ls_ack(pd, offset, fd, ospf_tree);
197             break;
198         default:
199             pi.captured_len = saved_len;
200             dissect_data(pd, offset, fd, tree); 
201     }
202     pi.captured_len = saved_len;
203 }
204
205 static void
206 dissect_ospf_hello(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
207     e_ospf_hello ospfhello;
208     guint32 *ospfneighbor;
209     char options[20]="";
210     int options_offset;
211
212     proto_tree *ospf_hello_tree;
213         proto_item *ti; 
214
215     memcpy(&ospfhello, &pd[offset], sizeof(e_ospf_hello));
216
217     if (tree) {
218         ti = proto_tree_add_text(tree, offset, END_OF_FRAME, "OSPF Hello Packet"); 
219         ospf_hello_tree = proto_item_add_subtree(ti, ett_ospf_hello);
220
221
222         proto_tree_add_text(ospf_hello_tree, offset , 4, "Network Mask: %s",  ip_to_str((guint8 *) &ospfhello.network_mask));
223         proto_tree_add_text(ospf_hello_tree, offset + 4, 2, "Hello Interval: %d seconds",  ntohs(ospfhello.hellointervall));
224
225         /* ATTENTION !!! no check for length of options string */
226         options_offset=0;
227         if(( ospfhello.options & OSPF_OPTIONS_E ) == OSPF_OPTIONS_E){
228             strcpy( (char *)(options + options_offset), "E");
229             options_offset+=1;
230         }
231         if(( ospfhello.options & OSPF_OPTIONS_MC ) == OSPF_OPTIONS_MC){
232             strcpy((char *) (options + options_offset), "/MC");
233             options_offset+=3;
234         }
235         if(( ospfhello.options & OSPF_OPTIONS_NP ) == OSPF_OPTIONS_NP){
236             strcpy((char *) (options + options_offset), "/NP");
237             options_offset+=3;
238         }
239         if(( ospfhello.options & OSPF_OPTIONS_EA ) == OSPF_OPTIONS_EA){
240             strcpy((char *) (options + options_offset) , "/EA");
241             options_offset+=3;
242         }
243         if(( ospfhello.options & OSPF_OPTIONS_DC ) == OSPF_OPTIONS_DC){
244             strcpy((char *) (options + options_offset) , "/DC");
245             options_offset+=3;
246         }
247
248         proto_tree_add_text(ospf_hello_tree, offset + 6, 1, "Options: %d (%s)",  ospfhello.options, options);
249         proto_tree_add_text(ospf_hello_tree, offset + 7, 1, "Router Priority: %d",  ospfhello.priority);
250         proto_tree_add_text(ospf_hello_tree, offset + 8, 4, "Router Dead Interval: %ld seconds",  (long)ntohl(ospfhello.dead_interval));
251         proto_tree_add_text(ospf_hello_tree, offset + 12, 4, "Designated Router: %s",  ip_to_str((guint8 *) &ospfhello.drouter));
252         proto_tree_add_text(ospf_hello_tree, offset + 16, 4, "Backup Designated Router: %s",  ip_to_str((guint8 *) &ospfhello.bdrouter));
253
254
255         offset+=20;
256         while(((int)(pi.captured_len - offset)) >= 4){
257             ospfneighbor=(guint32 *) &pd[offset];
258             proto_tree_add_text(ospf_hello_tree, offset, 4, "Active Neighbor: %s",  ip_to_str((guint8 *) ospfneighbor));
259             offset+=4;
260         }
261     }
262 }
263
264 static void
265 dissect_ospf_db_desc(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
266     e_ospf_dbd ospf_dbd;
267     char options[20]="";
268     int options_offset;
269     char flags[20]="";
270     int flags_offset;
271
272     proto_tree *ospf_db_desc_tree=NULL;
273         proto_item *ti; 
274
275     memcpy(&ospf_dbd, &pd[offset], sizeof(e_ospf_dbd));
276
277     if (tree) {
278         ti = proto_tree_add_text(tree, offset, END_OF_FRAME, "OSPF DB Description"); 
279         ospf_db_desc_tree = proto_item_add_subtree(ti, ett_ospf_desc);
280
281         proto_tree_add_text(ospf_db_desc_tree, offset, 2, "Interface MTU: %d", ntohs(ospf_dbd.interface_mtu) );
282
283
284         options_offset=0;
285         if(( ospf_dbd.options & OSPF_OPTIONS_E ) == OSPF_OPTIONS_E){
286             strcpy( (char *)(options + options_offset), "_E_");
287             options_offset+=1;
288         }
289         if(( ospf_dbd.options & OSPF_OPTIONS_MC ) == OSPF_OPTIONS_MC){
290             strcpy((char *) (options + options_offset), "_MC_");
291             options_offset+=3;
292         }
293         if(( ospf_dbd.options & OSPF_OPTIONS_NP ) == OSPF_OPTIONS_NP){
294             strcpy((char *) (options + options_offset), "_NP_");
295             options_offset+=3;
296         }
297         if(( ospf_dbd.options & OSPF_OPTIONS_EA ) == OSPF_OPTIONS_EA){
298             strcpy((char *) (options + options_offset) , "_EA_");
299             options_offset+=3;
300         }
301         if(( ospf_dbd.options & OSPF_OPTIONS_DC ) == OSPF_OPTIONS_DC){
302             strcpy((char *) (options + options_offset) , "_DC_");
303             options_offset+=3;
304         }
305
306         proto_tree_add_text(ospf_db_desc_tree, offset + 2 , 1, "Options: %d (%s)", ospf_dbd.options, options );
307
308
309         flags_offset=0;
310         if(( ospf_dbd.flags & OSPF_DBD_FLAG_MS ) == OSPF_DBD_FLAG_MS){
311             strcpy( (char *)(flags + flags_offset), "_I_");
312             flags_offset+=1;
313         }
314         if(( ospf_dbd.flags & OSPF_DBD_FLAG_M ) == OSPF_DBD_FLAG_M){
315             strcpy((char *) (flags + flags_offset), "_M_");
316             flags_offset+=3;
317         }
318         if(( ospf_dbd.flags & OSPF_DBD_FLAG_I ) == OSPF_DBD_FLAG_I){
319             strcpy((char *) (flags + flags_offset), "_I_");
320             flags_offset+=3;
321         }
322
323         proto_tree_add_text(ospf_db_desc_tree, offset + 3 , 1, "Flags: %d (%s)", ospf_dbd.flags, flags );
324         proto_tree_add_text(ospf_db_desc_tree, offset + 4 , 4, "DD Sequence: %ld", (long)ntohl(ospf_dbd.dd_sequence) );
325     }
326     /* LS Headers will be processed here */
327     /* skip to the end of DB-Desc header */
328     offset+=8;
329     while( ((int) (pi.captured_len - offset)) >= OSPF_LSA_HEADER_LENGTH ) {
330        dissect_ospf_lsa(pd, offset, fd, tree, FALSE);
331        offset+=OSPF_LSA_HEADER_LENGTH;
332     }
333 }
334
335 static void
336 dissect_ospf_ls_req(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
337     e_ospf_ls_req ospf_lsr;
338
339     proto_tree *ospf_lsr_tree;
340         proto_item *ti; 
341
342
343     /* zero or more LS requests may be within a LS Request */
344     /* we place every request for a LSA in a single subtree */
345     if (tree) {
346         while( ((int) (pi.captured_len - offset)) >= OSPF_LS_REQ_LENGTH ){
347              memcpy(&ospf_lsr, &pd[offset], sizeof(e_ospf_ls_req));
348              ti = proto_tree_add_text(tree, offset, OSPF_LS_REQ_LENGTH, "Link State Request"); 
349              ospf_lsr_tree = proto_item_add_subtree(ti, ett_ospf_lsr);
350
351              switch( ntohl( ospf_lsr.ls_type ) ){
352                  case OSPF_LSTYPE_ROUTER:
353                      proto_tree_add_text(ospf_lsr_tree, offset, 4, "LS Type: Router-LSA (%ld)", 
354                                        (long)ntohl( ospf_lsr.ls_type ) );
355                      break;
356                  case OSPF_LSTYPE_NETWORK:
357                      proto_tree_add_text(ospf_lsr_tree, offset, 4, "LS Type: Network-LSA (%ld)", 
358                                        (long)ntohl( ospf_lsr.ls_type ) );
359                      break;
360                  case OSPF_LSTYPE_SUMMERY:
361                      proto_tree_add_text(ospf_lsr_tree, offset, 4, "LS Type: Summary-LSA (IP network) (%ld)", 
362                                        (long)ntohl( ospf_lsr.ls_type ) );
363                      break;
364                  case OSPF_LSTYPE_ASBR:
365                      proto_tree_add_text(ospf_lsr_tree, offset, 4, "LS Type: Summary-LSA (ASBR) (%ld)", 
366                                        (long)ntohl( ospf_lsr.ls_type ) );
367                      break;
368                  case OSPF_LSTYPE_ASEXT:
369                      proto_tree_add_text(ospf_lsr_tree, offset, 4, "LS Type: AS-External-LSA (ASBR) (%ld)", 
370                                        (long)ntohl( ospf_lsr.ls_type ) );
371                      break;
372                  default:
373                      proto_tree_add_text(ospf_lsr_tree, offset, 4, "LS Type: %ld (unknown)", 
374                                        (long)ntohl( ospf_lsr.ls_type ) );
375              }
376
377              proto_tree_add_text(ospf_lsr_tree, offset + 4, 4, "Link State ID : %s", 
378                                          ip_to_str((guint8 *) &(ospf_lsr.ls_id)));
379              proto_tree_add_text(ospf_lsr_tree, offset + 8, 4, "Advertising Router : %s", 
380                                          ip_to_str((guint8 *) &(ospf_lsr.adv_router)));
381
382              offset+=12;
383         }
384     }
385 }
386
387 static void
388 dissect_ospf_ls_upd(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
389     e_ospf_lsa_upd_hdr upd_hdr;
390     guint32 lsa_counter; 
391
392     proto_tree *ospf_lsa_upd_tree=NULL;
393         proto_item *ti; 
394
395     memcpy(&upd_hdr, &pd[offset], sizeof(e_ospf_lsa_upd_hdr));
396
397     if (tree) {
398         ti = proto_tree_add_text(tree, offset, END_OF_FRAME, "LS Update Packet"); 
399         ospf_lsa_upd_tree = proto_item_add_subtree(ti, ett_ospf_lsa_upd);
400
401         proto_tree_add_text(ospf_lsa_upd_tree, offset, 4, "Nr oF LSAs: %ld", (long)ntohl(upd_hdr.lsa_nr) );
402     }
403     /* skip to the beginning of the first LSA */
404     offset+=4; /* the LS Upd PAcket contains only a 32 bit #LSAs field */
405     
406     lsa_counter = 0;
407     while(lsa_counter < ntohl(upd_hdr.lsa_nr)){
408         offset+=dissect_ospf_lsa(pd, offset, fd, ospf_lsa_upd_tree, TRUE);
409         lsa_counter += 1;
410     }
411 }
412
413 static void
414 dissect_ospf_ls_ack(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
415
416     /* the body of a LS Ack packet simply contains zero or more LSA Headers */
417     while( ((int)(pi.captured_len - offset)) >= OSPF_LSA_HEADER_LENGTH ) {
418        dissect_ospf_lsa(pd, offset, fd, tree, FALSE);
419        offset+=OSPF_LSA_HEADER_LENGTH;
420     }
421
422 }
423
424 /*
425  * Returns if an LSA is opaque, i.e. requires special treatment 
426  */
427 int is_opaque(int lsa_type)
428 {
429     return (lsa_type >= 9 && lsa_type <= 11);
430 }
431
432 /* MPLS/TE TLV types */
433 #define MPLS_TLV_ROUTER    1
434 #define MPLS_TLV_LINK      2
435
436 /* MPLS/TE Link STLV types */
437 enum {
438     MPLS_LINK_TYPE       = 1,
439     MPLS_LINK_ID,
440     MPLS_LINK_LOCAL_IF,
441     MPLS_LINK_REMOTE_IF,
442     MPLS_LINK_TE_METRIC,
443     MPLS_LINK_MAX_BW,
444     MPLS_LINK_MAX_RES_BW,
445     MPLS_LINK_UNRES_BW,
446     MPLS_LINK_COLOR,
447 };
448
449 static value_string mpls_link_stlv_str[] = {
450     {MPLS_LINK_TYPE, "Link Type"},
451     {MPLS_LINK_ID, "Link ID"},
452     {MPLS_LINK_LOCAL_IF, "Local Interface IP Address"},
453     {MPLS_LINK_REMOTE_IF, "Remote Interface IP Address"},
454     {MPLS_LINK_TE_METRIC, "Traffic Engineering Metric"},
455     {MPLS_LINK_MAX_BW, "Maximum Bandwidth"},
456     {MPLS_LINK_MAX_RES_BW, "Maximum Reservable Bandwidth"},
457     {MPLS_LINK_UNRES_BW, "Unreserved Bandwidth"},
458     {MPLS_LINK_COLOR, "Resource Class/Color"},
459 };
460
461 /* 
462  * Dissect MPLS/TE opaque LSA 
463  */
464
465 void dissect_ospf_lsa_mpls(const u_char *pd, 
466                           int offset, 
467                           frame_data *fd, 
468                           proto_tree *tree,
469                           e_ospf_lsa_hdr *lsa_hdr) {
470     proto_item *ti; 
471     proto_tree *mpls_tree;
472     proto_tree *tlv_tree;
473     proto_tree *stlv_tree;
474
475     int length;
476     int tlv_type;
477     int tlv_length;
478
479     int link_len;
480     int stlv_type, stlv_len;
481     char *stlv_name;
482     int i;
483
484     ti = proto_tree_add_text(tree, offset, ntohs(lsa_hdr->length) - 20,
485                              "MPLS Traffic Engineering LSA");
486     mpls_tree = proto_item_add_subtree(ti, ett_ospf_lsa_mpls);
487
488     for (length = 0; length < ntohs(lsa_hdr->length) - 20; length += 4) {
489         tlv_type = pntohs(pd+offset+length);
490         tlv_length = pntohs(pd+offset+length + 2);
491
492         switch(tlv_type) {
493         case MPLS_TLV_ROUTER:
494             ti = proto_tree_add_text(mpls_tree, offset+length, tlv_length+4, 
495                                      "Router Address: %s", 
496                                      ip_to_str((pd+offset+length+4)));
497             tlv_tree = proto_item_add_subtree(ti, ett_ospf_lsa_mpls_router);
498             proto_tree_add_text(tlv_tree, offset+length, 2, "TLV Type: 1 - Router Address");
499             proto_tree_add_text(tlv_tree, offset+length+2, 2, "TLV Length: %d", tlv_length);
500             proto_tree_add_text(tlv_tree, offset+length+4, 4, "Router Address: %s", 
501                                 ip_to_str((pd+offset+length+4)));
502             break;
503         case MPLS_TLV_LINK:
504             ti = proto_tree_add_text(mpls_tree, offset+length, tlv_length+4, 
505                                      "Link Information");
506             tlv_tree = proto_item_add_subtree(ti, ett_ospf_lsa_mpls_link);
507             proto_tree_add_text(tlv_tree, offset+length, 2, "TLV Type: 2 - Link Information");
508             proto_tree_add_text(tlv_tree, offset+length+2, 2, "TLV Length: %d", tlv_length);
509
510             /* Walk down the sub-TLVs for link information */
511             for (link_len = length + 4; link_len < length + 4 + tlv_length; link_len += 4) {
512                 stlv_type = pntohs(pd+offset+link_len);
513                 stlv_len = pntohs(pd+offset+link_len + 2);
514                 stlv_name = val_to_str(stlv_type, mpls_link_stlv_str, "Unknown sub-TLV");
515                 switch(stlv_type) {
516
517                 case MPLS_LINK_TYPE:
518                     ti = proto_tree_add_text(tlv_tree, offset+link_len, stlv_len+4, 
519                                              "%s: %d", stlv_name,
520                                              *((guint8 *)pd + offset + link_len + 4));
521                     stlv_tree = proto_item_add_subtree(ti, ett_ospf_lsa_mpls_link_stlv);
522                     proto_tree_add_text(stlv_tree, offset+link_len, 2, 
523                                         "TLV Type: %d: %s", stlv_type, stlv_name);
524                     proto_tree_add_text(stlv_tree, offset+link_len+2, 2, "TLV Length: %d", stlv_len);
525                     proto_tree_add_text(stlv_tree, offset+link_len+4, 1, "%s: %d", stlv_name,
526                                         *((guint8 *)pd + offset + link_len + 4));
527                     break;
528                     
529                 case MPLS_LINK_ID:
530                     ti = proto_tree_add_text(tlv_tree, offset+link_len, stlv_len+4, 
531                                              "%s: %s (%x)", stlv_name, 
532                                              ip_to_str(pd + offset + link_len + 4),
533                                              pntohl(pd + offset + link_len + 4));
534                     stlv_tree = proto_item_add_subtree(ti, ett_ospf_lsa_mpls_link_stlv);
535                     proto_tree_add_text(stlv_tree, offset+link_len, 2, 
536                                         "TLV Type: %d: %s", stlv_type, stlv_name);
537                     proto_tree_add_text(stlv_tree, offset+link_len+2, 2, "TLV Length: %d", stlv_len);
538                     proto_tree_add_text(stlv_tree, offset+link_len+4, 4, "%s: %s (%x)", stlv_name, 
539                                         ip_to_str(pd + offset + link_len + 4),
540                                         pntohl(pd + offset + link_len + 4));
541                     break;
542                     
543                 case MPLS_LINK_LOCAL_IF:
544                 case MPLS_LINK_REMOTE_IF:
545                     ti = proto_tree_add_text(tlv_tree, offset+link_len, stlv_len+4, 
546                                              "%s: %s", stlv_name, 
547                                              ip_to_str(pd+offset+link_len+4));
548                     stlv_tree = proto_item_add_subtree(ti, ett_ospf_lsa_mpls_link_stlv);
549                     proto_tree_add_text(stlv_tree, offset+link_len, 2, 
550                                         "TLV Type: %d: %s", stlv_type, stlv_name);
551                     proto_tree_add_text(stlv_tree, offset+link_len+2, 2, "TLV Length: %d", stlv_len);
552                     proto_tree_add_text(stlv_tree, offset+link_len+4, 4, "%s: %s", stlv_name, 
553                                         ip_to_str(pd+offset+link_len+4));
554
555                     break;
556
557                 case MPLS_LINK_TE_METRIC:
558                 case MPLS_LINK_COLOR:
559                     ti = proto_tree_add_text(tlv_tree, offset+link_len, stlv_len+4, 
560                                              "%s: %d", stlv_name, 
561                                              pntohl(pd + offset + link_len + 4));
562                     stlv_tree = proto_item_add_subtree(ti, ett_ospf_lsa_mpls_link_stlv);
563                     proto_tree_add_text(stlv_tree, offset+link_len, 2, 
564                                         "TLV Type: %d: %s", stlv_type, stlv_name);
565                     proto_tree_add_text(stlv_tree, offset+link_len+2, 2, "TLV Length: %d", stlv_len);
566                     proto_tree_add_text(stlv_tree, offset+link_len+4, 4, "%s: %d", stlv_name, 
567                                         pntohl(pd + offset + link_len + 4));
568                     break;
569                     
570                 case MPLS_LINK_MAX_BW:
571                 case MPLS_LINK_MAX_RES_BW:
572                     ti = proto_tree_add_text(tlv_tree, offset+link_len, stlv_len+4, 
573                                              "%s: %ld", stlv_name, 
574                                              pieee_to_long(pd + offset + link_len + 4));
575                     stlv_tree = proto_item_add_subtree(ti, ett_ospf_lsa_mpls_link_stlv);
576                     proto_tree_add_text(stlv_tree, offset+link_len, 2, 
577                                         "TLV Type: %d: %s", stlv_type, stlv_name);
578                     proto_tree_add_text(stlv_tree, offset+link_len+2, 2, "TLV Length: %d", stlv_len);
579                     proto_tree_add_text(stlv_tree, offset+link_len+4, 4, "%s: %ld", stlv_name, 
580                                         pieee_to_long(pd + offset + link_len + 4));
581                     break;
582                     
583                 case MPLS_LINK_UNRES_BW:
584                     ti = proto_tree_add_text(tlv_tree, offset+link_len, stlv_len+4, 
585                                              "%s", stlv_name);
586                     stlv_tree = proto_item_add_subtree(ti, ett_ospf_lsa_mpls_link_stlv);
587                     proto_tree_add_text(stlv_tree, offset+link_len, 2, 
588                                         "TLV Type: %d: %s", stlv_type, stlv_name);
589                     proto_tree_add_text(stlv_tree, offset+link_len+2, 2, "TLV Length: %d", stlv_len);
590                     for (i=0; i<8; i++) 
591                         proto_tree_add_text(stlv_tree, offset+link_len+4+(i*4), 4, 
592                                             "Pri %d: %ld", i,
593                                             pieee_to_long(pd + offset + link_len + 4 + i*4));
594                     break;
595                     
596                 default:
597                     proto_tree_add_text(tlv_tree, offset+link_len, stlv_len+4, 
598                                         "Unknown Link sub-TLV: %d", stlv_type);
599                 }
600                 link_len += ((stlv_len+3)/4)*4;
601             }
602
603             break;
604
605         default:
606             ti = proto_tree_add_text(mpls_tree, offset+length, tlv_length+4, 
607                                      "Unknown LSA: %d", tlv_type);
608             tlv_tree = proto_item_add_subtree(ti, ett_ospf_lsa_mpls_link);
609             proto_tree_add_text(tlv_tree, offset+length, 2, "TLV Type: %d - Unknown", tlv_type);
610             proto_tree_add_text(tlv_tree, offset+length+2, 2, "TLV Length: %d", tlv_length);
611             proto_tree_add_text(tlv_tree, offset+length+4, tlv_length, "TLV Data");
612             break;
613         }
614
615         length += tlv_length;
616
617     }
618 }
619
620 /*
621  * Dissect opaque LSAs
622  */
623 void dissect_ospf_lsa_opaque(const u_char *pd, 
624                             int offset, 
625                             frame_data *fd, 
626                             proto_tree *tree,
627                             e_ospf_lsa_hdr *lsa_hdr) {
628     guint8 opaque_id = *((guint8 *) &(lsa_hdr->ls_id));
629
630     switch(opaque_id) {
631
632     case OSPF_LSA_MPLS_TE:
633         dissect_ospf_lsa_mpls(pd, offset, fd, tree, lsa_hdr);
634         break;
635
636     default:
637         proto_tree_add_text(tree, offset, END_OF_FRAME, "Unknown LSA Type");
638         break;
639     } /* switch on opaque LSA id */
640 }
641
642 static int
643 dissect_ospf_lsa(const u_char *pd, int offset, frame_data *fd, proto_tree *tree, int disassemble_body) {
644     e_ospf_lsa_hdr       lsa_hdr;
645     char                *lsa_type;
646     int                  lsa_end;
647
648     /* data strutures for the router LSA */
649     e_ospf_router_lsa           router_lsa;
650     e_ospf_router_data          router_data;
651     e_ospf_router_metric        tos_data;
652     guint16                     link_counter;
653     guint8                      tos_counter;
654     char                        *link_type;
655     char                        *link_id;
656
657     /* data structures for the network lsa */
658     e_ospf_network_lsa  network_lsa;
659     guint32             *attached_router;
660
661     /* data structures for the summary and ASBR LSAs */
662     e_ospf_summary_lsa  summary_lsa;
663
664     /* data structures for the AS-External LSA */
665     e_ospf_asexternal_lsa       asext_lsa;
666     guint32                   asext_metric;
667
668     /* data structures for opaque LSA */
669     guint32                     ls_id;
670
671     proto_tree *ospf_lsa_tree;
672         proto_item *ti; 
673
674     memcpy(&lsa_hdr, &pd[offset], sizeof(e_ospf_lsa_hdr));
675
676              
677
678     switch(lsa_hdr.ls_type) {
679         case OSPF_LSTYPE_ROUTER:
680             lsa_type="Router LSA";
681             break;
682         case OSPF_LSTYPE_NETWORK:
683             lsa_type="Network LSA";
684             break;
685         case OSPF_LSTYPE_SUMMERY:
686             lsa_type="Summary LSA";
687             break;
688         case OSPF_LSTYPE_ASBR:
689             lsa_type="ASBR LSA";
690             break;
691         case OSPF_LSTYPE_ASEXT:
692             lsa_type="AS-external-LSA";
693             break;
694         case OSPF_LSTYPE_OP_LINKLOCAL:
695             lsa_type="Opaque LSA, Link-local scope";
696             break;
697         case OSPF_LSTYPE_OP_AREALOCAL:
698             lsa_type="Opaque LSA, Area-local scope";
699             break;
700         case OSPF_LSTYPE_OP_ASWIDE:
701             lsa_type="Opaque LSA, AS-wide scope";
702             break;
703         default:
704             lsa_type="unknown";
705     }
706
707     if (tree) {
708         if(disassemble_body){
709              ti = proto_tree_add_text(tree, offset, ntohs(lsa_hdr.length), 
710                                                       "%s (Type: %d)", lsa_type, lsa_hdr.ls_type); 
711         } else {
712              ti = proto_tree_add_text(tree, offset, OSPF_LSA_HEADER_LENGTH, "LSA Header"); 
713         }
714         ospf_lsa_tree = proto_item_add_subtree(ti, ett_ospf_lsa);
715
716         
717         proto_tree_add_text(ospf_lsa_tree, offset, 2, "LS Age: %d seconds", ntohs(lsa_hdr.ls_age));
718         proto_tree_add_text(ospf_lsa_tree, offset + 2, 1, "Options: %d ", lsa_hdr.options);
719         proto_tree_add_text(ospf_lsa_tree, offset + 3, 1, "LSA Type: %d (%s)", lsa_hdr.ls_type, lsa_type);
720
721         if (is_opaque(lsa_hdr.ls_type)) {
722             ls_id = ntohl(lsa_hdr.ls_id);
723             proto_tree_add_text(ospf_lsa_tree, offset + 4, 1, "Link State ID Opaque Type: %u ", 
724                                 (ls_id >> 24) & 0xff);
725             proto_tree_add_text(ospf_lsa_tree, offset + 5, 3, "Link State ID Opaque ID: %u ", 
726                                 ls_id & 0xffffff);
727         } else
728             proto_tree_add_text(ospf_lsa_tree, offset + 4, 4, "Link State ID: %s ", 
729                                 ip_to_str((guint8 *) &(lsa_hdr.ls_id)));
730
731         proto_tree_add_text(ospf_lsa_tree, offset + 8, 4, "Advertising Router: %s ", 
732                                                      ip_to_str((guint8 *) &(lsa_hdr.adv_router)));
733         proto_tree_add_text(ospf_lsa_tree, offset + 12, 4, "LS Sequence Number: 0x%04lx ", 
734                                                      (unsigned long)ntohl(lsa_hdr.ls_seq));
735         proto_tree_add_text(ospf_lsa_tree, offset + 16, 2, "LS Checksum: %d ", ntohs(lsa_hdr.ls_checksum));
736
737         proto_tree_add_text(ospf_lsa_tree, offset + 18, 2, "Length: %d ", ntohs(lsa_hdr.length));
738
739         if(!disassemble_body){
740            return OSPF_LSA_HEADER_LENGTH;
741         }
742
743         lsa_end = offset + ntohs(lsa_hdr.length);
744         /* the LSA body starts after 20 bytes of LSA Header */
745         offset+=20;
746
747         switch(lsa_hdr.ls_type){
748             case(OSPF_LSTYPE_ROUTER):
749                 memcpy(&router_lsa, &pd[offset], sizeof(e_ospf_router_lsa));
750
751                 /* again: flags should be secified in detail */
752                 proto_tree_add_text(ospf_lsa_tree, offset, 1, "Flags: 0x%02x ", router_lsa.flags);
753                 proto_tree_add_text(ospf_lsa_tree, offset + 2, 2, "Nr. of Links: %d ", 
754                                                                    ntohs(router_lsa.nr_links));
755                 offset += 4;
756                 /* router_lsa.nr_links links follow 
757                  * maybe we should put each of the links into its own subtree ???
758                  */
759                 for(link_counter = 1 ; link_counter <= ntohs(router_lsa.nr_links); link_counter++){
760
761                     memcpy(&router_data, &pd[offset], sizeof(e_ospf_router_data));
762                     /* check the Link Type and ID */
763                     switch(router_data.link_type) {
764                         case OSPF_LINK_PTP:
765                             link_type="Point-to-point connection to another router";
766                             link_id="Neighboring router's Router ID";
767                             break;
768                         case OSPF_LINK_TRANSIT:
769                             link_type="Connection to a transit network";
770                             link_id="IP address of Designated Router";
771                             break;
772                         case OSPF_LINK_STUB:
773                             link_type="Connection to a stub network";
774                             link_id="IP network/subnet number";
775                             break;
776                         case OSPF_LINK_VIRTUAL:
777                             link_type="Virtual link";
778                             link_id="Neighboring router's Router ID";
779                             break;
780                         default:
781                             link_type="unknown link type";
782                             link_id="unknown link id";
783                     }
784
785                     proto_tree_add_text(ospf_lsa_tree, offset, 4, "%s: %s", link_id,
786                                                    ip_to_str((guint8 *) &(router_data.link_id)));
787
788                     /* link_data should be specified in detail (e.g. network mask) (depends on link type)*/
789                     proto_tree_add_text(ospf_lsa_tree, offset + 4, 4, "Link Data: %s", 
790                                                    ip_to_str((guint8 *) &(router_data.link_data)));
791
792                     proto_tree_add_text(ospf_lsa_tree, offset + 8, 1, "Link Type: %d - %s", 
793                                                               router_data.link_type, link_type);
794                     proto_tree_add_text(ospf_lsa_tree, offset + 9, 1, "Nr. of TOS metrics: %d", router_data.nr_tos);
795                     proto_tree_add_text(ospf_lsa_tree, offset + 10, 2, "TOS 0 metric: %d", ntohs( router_data.tos0_metric ));
796
797                     offset += 12;
798
799                     /* router_data.nr_tos metrics may follow each link 
800                      * ATTENTION: TOS metrics are not tested (I don't have TOS based routing)
801                      * please send me a mail if it is/isn't working
802                      */
803
804                     for(tos_counter = 1 ; link_counter <= ntohs(router_data.nr_tos); tos_counter++){
805                         memcpy(&tos_data, &pd[offset], sizeof(e_ospf_router_metric));
806                         proto_tree_add_text(ospf_lsa_tree, offset, 1, "TOS: %d, Metric: %d", 
807                                                 tos_data.tos, ntohs(tos_data.metric));
808                         offset += 4;
809                     }
810                 }
811                 break;
812             case(OSPF_LSTYPE_NETWORK):
813                 memcpy(&network_lsa, &pd[offset], sizeof(e_ospf_network_lsa));
814                 proto_tree_add_text(ospf_lsa_tree, offset, 4, "Netmask: %s", 
815                                                  ip_to_str((guint8 *) &(network_lsa.network_mask)));
816                 offset += 4;
817
818                 while( (lsa_end - offset) >= 4){
819                     attached_router = (guint32 *) &pd[offset];
820                     proto_tree_add_text(ospf_lsa_tree, offset, 4, "Attached Router: %s", 
821                                                  ip_to_str((guint8 *) attached_router));
822                     offset += 4;
823                 }
824                 break;
825             case(OSPF_LSTYPE_SUMMERY):
826                 /* Type 3 and 4 LSAs have the same format */
827             case(OSPF_LSTYPE_ASBR):
828                 memcpy(&summary_lsa, &pd[offset], sizeof(e_ospf_summary_lsa));
829                 proto_tree_add_text(ospf_lsa_tree, offset, 4, "Netmask: %s", 
830                                                  ip_to_str((guint8 *) &(summary_lsa.network_mask)));
831                 /* returns only the TOS 0 metric (even if there are more TOS metrics) */
832                 break;
833             case(OSPF_LSTYPE_ASEXT):
834                 memcpy(&summary_lsa, &pd[offset], sizeof(e_ospf_summary_lsa));
835                 proto_tree_add_text(ospf_lsa_tree, offset, 4, "Netmask: %s", 
836                                                   ip_to_str((guint8 *) &(summary_lsa.network_mask)));
837
838                 /* asext_lsa = (e_ospf_asexternal_lsa *) &pd[offset + 4]; */
839                 memcpy(&asext_lsa, &pd[offset + 4], sizeof(asext_lsa));
840                 if( (asext_lsa.options & 128) == 128 ) { /* check wether or not E bit is set */
841                    proto_tree_add_text(ospf_lsa_tree, offset, 1, 
842                             "External Type: Type 2 (metric is larger than any other link state path)");
843                 } else {
844                    proto_tree_add_text(ospf_lsa_tree, offset + 4, 1, 
845                             "External Type: Type 1 (metric is specified in the same units as interface cost)");
846                 }
847                 /* the metric field of a AS-external LAS is specified in 3 bytes -> not well aligned */
848                 /* this routine returns only the TOS 0 metric (even if there are more TOS metrics) */
849                 memcpy(&asext_metric, &pd[offset+4], 4); 
850                 
851                 /* erase the leading 8 bits (the dont belong to the metric */
852                 asext_metric = ntohl(asext_metric) & 0x00ffffff ;
853
854                 proto_tree_add_text(ospf_lsa_tree, offset + 5,  3,"Metric: %d", asext_metric);
855                 proto_tree_add_text(ospf_lsa_tree, offset + 8,  4,"Forwarding Address: %s", 
856                                                  ip_to_str((guint8 *) &(asext_lsa.gateway)));
857                 proto_tree_add_text(ospf_lsa_tree, offset + 12, 4,"External Route Tag: %ld", (long)ntohl(asext_lsa.external_tag)); 
858                     
859                 break;
860
861         case OSPF_LSTYPE_OP_LINKLOCAL:
862         case OSPF_LSTYPE_OP_AREALOCAL:
863         case OSPF_LSTYPE_OP_ASWIDE:
864             dissect_ospf_lsa_opaque(pd, offset, fd, ospf_lsa_tree, &lsa_hdr);
865             break;
866
867         default:
868                /* unknown LSA type */
869                 proto_tree_add_text(ospf_lsa_tree, offset, END_OF_FRAME, "Unknown LSA Type");
870         }
871     }
872     /* return the length of this LSA */
873     return ntohs(lsa_hdr.length);
874 }
875
876 void
877 proto_register_ospf(void)
878 {
879 /*        static hf_register_info hf[] = {
880                 { &variable,
881                 { "Name",           "ospf.abbreviation", TYPE, VALS_POINTER }},
882         };*/
883         static gint *ett[] = {
884                 &ett_ospf,
885                 &ett_ospf_hdr,
886                 &ett_ospf_hello,
887                 &ett_ospf_desc,
888                 &ett_ospf_lsr,
889                 &ett_ospf_lsa,
890                 &ett_ospf_lsa_upd,
891                 &ett_ospf_lsa_mpls,
892                 &ett_ospf_lsa_mpls_router,
893                 &ett_ospf_lsa_mpls_link,
894                 &ett_ospf_lsa_mpls_link_stlv
895         };
896
897         proto_ospf = proto_register_protocol("Open Shortest Path First", "ospf");
898  /*       proto_register_field_array(proto_ospf, hf, array_length(hf));*/
899         proto_register_subtree_array(ett, array_length(ett));
900 }
901
902 void
903 proto_reg_handoff_ospf(void)
904 {
905         dissector_add("ip.proto", IP_PROTO_OSPF, dissect_ospf);
906 }