Fix #ifndef line whose symbol had been omitted.
[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.17 1999/11/17 19:07:10 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
53 static int proto_ospf = -1;
54
55 static gint ett_ospf = -1;
56 static gint ett_ospf_hdr = -1;
57 static gint ett_ospf_hello = -1;
58 static gint ett_ospf_desc = -1;
59 static gint ett_ospf_lsr = -1;
60 static gint ett_ospf_lsa = -1;
61 static gint ett_ospf_lsa_upd = -1;
62
63 void 
64 dissect_ospf(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
65     e_ospfhdr ospfh;
66     e_ospf_crypto *crypto;
67     int i, saved_len, ospflen;
68
69     proto_tree *ospf_tree = NULL;
70         proto_item *ti; 
71     proto_tree *ospf_header_tree;
72     char auth_data[(2 * 16) + 1]="";
73     char *packet_type;
74     static value_string pt_vals[] = { {OSPF_HELLO,   "Hello Packet"   },
75                                       {OSPF_DB_DESC, "DB Descr."      },
76                                       {OSPF_LS_REQ,  "LS Request"     },
77                                       {OSPF_LS_UPD,  "LS Update"      },
78                                       {OSPF_LS_ACK,  "LS Acknowledge" },
79                                       {0,             NULL            } };
80
81     memcpy(&ospfh, &pd[offset], sizeof(e_ospfhdr));
82
83     packet_type = match_strval(ospfh.packet_type, pt_vals);
84     if (check_col(fd, COL_PROTOCOL))
85         col_add_str(fd, COL_PROTOCOL, "OSPF");
86     if (check_col(fd, COL_INFO)) {
87         if (packet_type != NULL)
88             col_add_str(fd, COL_INFO, packet_type); 
89         else
90             col_add_fstr(fd, COL_INFO, "Unknown (%d)", ospfh.packet_type); 
91     }  
92
93     if (tree) {
94         ti = proto_tree_add_item(tree, proto_ospf, offset, ntohs(ospfh.length), NULL);
95         ospf_tree = proto_item_add_subtree(ti, ett_ospf);
96
97         ti = proto_tree_add_text(ospf_tree, offset, OSPF_HEADER_LENGTH, "OSPF Header"); 
98         ospf_header_tree = proto_item_add_subtree(ti, ett_ospf_hdr);
99
100         proto_tree_add_text(ospf_header_tree, offset, 1, "OSPF Version: %d", ospfh.version);  
101         proto_tree_add_text(ospf_header_tree, offset + 1 , 1, "OSPF Packet Type: %d (%s)", 
102                                                            ospfh.packet_type,
103                                                            (packet_type != NULL ?
104                                                              packet_type :
105                                                              "Unknown"));
106         proto_tree_add_text(ospf_header_tree, offset + 2 , 2, "Packet Length: %d", 
107                                                            ntohs(ospfh.length));
108         proto_tree_add_text(ospf_header_tree, offset + 4 , 4, "Source OSPF Router ID: %s", 
109
110                                                            ip_to_str((guint8 *) &(ospfh.routerid)));
111         if (!(ospfh.area)) {
112            proto_tree_add_text(ospf_header_tree, offset + 8 , 4, "Area ID: Backbone");
113         } else {
114            proto_tree_add_text(ospf_header_tree, offset + 8 , 4, "Area ID: %s", ip_to_str((guint8 *) &(ospfh.area)));
115         }
116         proto_tree_add_text(ospf_header_tree, offset + 12 , 2, "Packet Checksum: 0x%x",
117               ntohs(ospfh.checksum));
118         switch( ntohs(ospfh.auth_type) ) {
119             case OSPF_AUTH_NONE:
120                  proto_tree_add_text(ospf_header_tree, offset + 14 , 2, "Auth Type: none");
121                  proto_tree_add_text(ospf_header_tree, offset + 16 , 8, "Auth Data (none)");
122                  break;
123             case OSPF_AUTH_SIMPLE:
124                  proto_tree_add_text(ospf_header_tree, offset + 14 , 2, "Auth Type: simple");
125                  strncpy(auth_data, (char *) &ospfh.auth_data, 8);
126                  proto_tree_add_text(ospf_header_tree, offset + 16 , 8, "Auth Data: %s", auth_data);
127                  break;
128             case OSPF_AUTH_CRYPT:
129                  crypto = (e_ospf_crypto *)ospfh.auth_data;
130                  proto_tree_add_text(ospf_header_tree, offset + 14 , 2, "Auth Type: crypt");
131                  proto_tree_add_text(ospf_header_tree, offset + 18 , 1, "Auth Key ID: %d",
132                                      crypto->key_id);
133                  proto_tree_add_text(ospf_header_tree, offset + 19 , 1, "Auth Data Length: %d",
134                                      crypto->length);
135                  proto_tree_add_text(ospf_header_tree, offset + 20 , 4, "Auth Crypto Sequence Number: 0x%x",
136                                      ntohl(crypto->sequence_num));
137   
138                  ospflen = ntohs(ospfh.length);
139                  for (i = 0; i < crypto->length && i < (sizeof(auth_data)/2); i++)
140                      sprintf(&auth_data[i*2],"%02x",pd[offset + ospflen + i]);
141                  proto_tree_add_text(ospf_header_tree, offset + ospflen , 16, "Auth Data: %s", auth_data);
142                  break;
143             default:
144                  proto_tree_add_text(ospf_header_tree, offset + 14 , 2, "Auth Type (unknown)");
145                  proto_tree_add_text(ospf_header_tree, offset + 16 , 8, "Auth Data (unknown)");
146         }
147
148     }
149
150     /* Temporarily adjust the captured length to match the size of the OSPF
151      * packet (since the dissect routines use it to work out where the end of
152      * the ospf packet is).
153      */
154     saved_len = pi.captured_len;
155     if (BYTES_ARE_IN_FRAME(offset, ntohs(ospfh.length))) {
156       pi.captured_len = offset + ntohs(ospfh.length);
157     }
158
159     /*  Skip over header */
160     offset += OSPF_HEADER_LENGTH;
161
162     switch(ospfh.packet_type){
163         case OSPF_HELLO:
164             dissect_ospf_hello(pd, offset, fd, ospf_tree); 
165             break;
166         case OSPF_DB_DESC:
167             dissect_ospf_db_desc(pd, offset, fd, ospf_tree);   
168             break;
169         case OSPF_LS_REQ:
170             dissect_ospf_ls_req(pd, offset, fd, ospf_tree);   
171             break;
172         case OSPF_LS_UPD:
173             dissect_ospf_ls_upd(pd, offset, fd, ospf_tree);
174             break;
175         case OSPF_LS_ACK:
176             dissect_ospf_ls_ack(pd, offset, fd, ospf_tree);
177             break;
178         default:
179             pi.captured_len = saved_len;
180             dissect_data(pd, offset, fd, tree); 
181     }
182     pi.captured_len = saved_len;
183 }
184
185 void
186 dissect_ospf_hello(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
187     e_ospf_hello ospfhello;
188     guint32 *ospfneighbor;
189     char options[20]="";
190     int options_offset;
191
192     proto_tree *ospf_hello_tree;
193         proto_item *ti; 
194
195     memcpy(&ospfhello, &pd[offset], sizeof(e_ospf_hello));
196
197     if (tree) {
198         ti = proto_tree_add_text(tree, offset, END_OF_FRAME, "OSPF Hello Packet"); 
199         ospf_hello_tree = proto_item_add_subtree(ti, ett_ospf_hello);
200
201
202         proto_tree_add_text(ospf_hello_tree, offset , 4, "Network Mask: %s",  ip_to_str((guint8 *) &ospfhello.network_mask));
203         proto_tree_add_text(ospf_hello_tree, offset + 4, 2, "Hello Interval: %d seconds",  ntohs(ospfhello.hellointervall));
204
205         /* ATTENTION !!! no check for length of options string */
206         options_offset=0;
207         if(( ospfhello.options & OSPF_OPTIONS_E ) == OSPF_OPTIONS_E){
208             strcpy( (char *)(options + options_offset), "E");
209             options_offset+=1;
210         }
211         if(( ospfhello.options & OSPF_OPTIONS_MC ) == OSPF_OPTIONS_MC){
212             strcpy((char *) (options + options_offset), "/MC");
213             options_offset+=3;
214         }
215         if(( ospfhello.options & OSPF_OPTIONS_NP ) == OSPF_OPTIONS_NP){
216             strcpy((char *) (options + options_offset), "/NP");
217             options_offset+=3;
218         }
219         if(( ospfhello.options & OSPF_OPTIONS_EA ) == OSPF_OPTIONS_EA){
220             strcpy((char *) (options + options_offset) , "/EA");
221             options_offset+=3;
222         }
223         if(( ospfhello.options & OSPF_OPTIONS_DC ) == OSPF_OPTIONS_DC){
224             strcpy((char *) (options + options_offset) , "/DC");
225             options_offset+=3;
226         }
227
228         proto_tree_add_text(ospf_hello_tree, offset + 6, 1, "Options: %d (%s)",  ospfhello.options, options);
229         proto_tree_add_text(ospf_hello_tree, offset + 7, 1, "Router Priority: %d",  ospfhello.priority);
230         proto_tree_add_text(ospf_hello_tree, offset + 8, 4, "Router Dead Interval: %ld seconds",  (long)ntohl(ospfhello.dead_interval));
231         proto_tree_add_text(ospf_hello_tree, offset + 12, 4, "Designated Router: %s",  ip_to_str((guint8 *) &ospfhello.drouter));
232         proto_tree_add_text(ospf_hello_tree, offset + 16, 4, "Backup Designated Router: %s",  ip_to_str((guint8 *) &ospfhello.bdrouter));
233
234
235         offset+=20;
236         while(((int)(pi.captured_len - offset)) >= 4){
237             ospfneighbor=(guint32 *) &pd[offset];
238             proto_tree_add_text(ospf_hello_tree, offset, 4, "Active Neighbor: %s",  ip_to_str((guint8 *) ospfneighbor));
239             offset+=4;
240         }
241     }
242 }
243
244 void
245 dissect_ospf_db_desc(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
246     e_ospf_dbd ospf_dbd;
247     char options[20]="";
248     int options_offset;
249     char flags[20]="";
250     int flags_offset;
251
252     proto_tree *ospf_db_desc_tree=NULL;
253         proto_item *ti; 
254
255     memcpy(&ospf_dbd, &pd[offset], sizeof(e_ospf_dbd));
256
257     if (tree) {
258         ti = proto_tree_add_text(tree, offset, END_OF_FRAME, "OSPF DB Description"); 
259         ospf_db_desc_tree = proto_item_add_subtree(ti, ett_ospf_desc);
260
261         proto_tree_add_text(ospf_db_desc_tree, offset, 2, "Interface MTU: %d", ntohs(ospf_dbd.interface_mtu) );
262
263
264         options_offset=0;
265         if(( ospf_dbd.options & OSPF_OPTIONS_E ) == OSPF_OPTIONS_E){
266             strcpy( (char *)(options + options_offset), "_E_");
267             options_offset+=1;
268         }
269         if(( ospf_dbd.options & OSPF_OPTIONS_MC ) == OSPF_OPTIONS_MC){
270             strcpy((char *) (options + options_offset), "_MC_");
271             options_offset+=3;
272         }
273         if(( ospf_dbd.options & OSPF_OPTIONS_NP ) == OSPF_OPTIONS_NP){
274             strcpy((char *) (options + options_offset), "_NP_");
275             options_offset+=3;
276         }
277         if(( ospf_dbd.options & OSPF_OPTIONS_EA ) == OSPF_OPTIONS_EA){
278             strcpy((char *) (options + options_offset) , "_EA_");
279             options_offset+=3;
280         }
281         if(( ospf_dbd.options & OSPF_OPTIONS_DC ) == OSPF_OPTIONS_DC){
282             strcpy((char *) (options + options_offset) , "_DC_");
283             options_offset+=3;
284         }
285
286         proto_tree_add_text(ospf_db_desc_tree, offset + 2 , 1, "Options: %d (%s)", ospf_dbd.options, options );
287
288
289         flags_offset=0;
290         if(( ospf_dbd.flags & OSPF_DBD_FLAG_MS ) == OSPF_DBD_FLAG_MS){
291             strcpy( (char *)(flags + flags_offset), "_I_");
292             flags_offset+=1;
293         }
294         if(( ospf_dbd.flags & OSPF_DBD_FLAG_M ) == OSPF_DBD_FLAG_M){
295             strcpy((char *) (flags + flags_offset), "_M_");
296             flags_offset+=3;
297         }
298         if(( ospf_dbd.flags & OSPF_DBD_FLAG_I ) == OSPF_DBD_FLAG_I){
299             strcpy((char *) (flags + flags_offset), "_I_");
300             flags_offset+=3;
301         }
302
303         proto_tree_add_text(ospf_db_desc_tree, offset + 3 , 1, "Flags: %d (%s)", ospf_dbd.flags, flags );
304         proto_tree_add_text(ospf_db_desc_tree, offset + 4 , 4, "DD Sequence: %ld", (long)ntohl(ospf_dbd.dd_sequence) );
305     }
306     /* LS Headers will be processed here */
307     /* skip to the end of DB-Desc header */
308     offset+=8;
309     while( ((int) (pi.captured_len - offset)) >= OSPF_LSA_HEADER_LENGTH ) {
310        dissect_ospf_lsa(pd, offset, fd, tree, FALSE);
311        offset+=OSPF_LSA_HEADER_LENGTH;
312     }
313 }
314
315 void
316 dissect_ospf_ls_req(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
317     e_ospf_ls_req ospf_lsr;
318
319     proto_tree *ospf_lsr_tree;
320         proto_item *ti; 
321
322
323     /* zero or more LS requests may be within a LS Request */
324     /* we place every request for a LSA in a single subtree */
325     if (tree) {
326         while( ((int) (pi.captured_len - offset)) >= OSPF_LS_REQ_LENGTH ){
327              memcpy(&ospf_lsr, &pd[offset], sizeof(e_ospf_ls_req));
328              ti = proto_tree_add_text(tree, offset, OSPF_LS_REQ_LENGTH, "Link State Request"); 
329              ospf_lsr_tree = proto_item_add_subtree(ti, ett_ospf_lsr);
330
331              switch( ntohl( ospf_lsr.ls_type ) ){
332                  case OSPF_LSTYPE_ROUTER:
333                      proto_tree_add_text(ospf_lsr_tree, offset, 4, "LS Type: Router-LSA (%ld)", 
334                                        (long)ntohl( ospf_lsr.ls_type ) );
335                      break;
336                  case OSPF_LSTYPE_NETWORK:
337                      proto_tree_add_text(ospf_lsr_tree, offset, 4, "LS Type: Network-LSA (%ld)", 
338                                        (long)ntohl( ospf_lsr.ls_type ) );
339                      break;
340                  case OSPF_LSTYPE_SUMMERY:
341                      proto_tree_add_text(ospf_lsr_tree, offset, 4, "LS Type: Summary-LSA (IP network) (%ld)", 
342                                        (long)ntohl( ospf_lsr.ls_type ) );
343                      break;
344                  case OSPF_LSTYPE_ASBR:
345                      proto_tree_add_text(ospf_lsr_tree, offset, 4, "LS Type: Summary-LSA (ASBR) (%ld)", 
346                                        (long)ntohl( ospf_lsr.ls_type ) );
347                      break;
348                  case OSPF_LSTYPE_ASEXT:
349                      proto_tree_add_text(ospf_lsr_tree, offset, 4, "LS Type: AS-External-LSA (ASBR) (%ld)", 
350                                        (long)ntohl( ospf_lsr.ls_type ) );
351                      break;
352                  default:
353                      proto_tree_add_text(ospf_lsr_tree, offset, 4, "LS Type: %ld (unknown)", 
354                                        (long)ntohl( ospf_lsr.ls_type ) );
355              }
356
357              proto_tree_add_text(ospf_lsr_tree, offset + 4, 4, "Link State ID : %s", 
358                                          ip_to_str((guint8 *) &(ospf_lsr.ls_id)));
359              proto_tree_add_text(ospf_lsr_tree, offset + 8, 4, "Advertising Router : %s", 
360                                          ip_to_str((guint8 *) &(ospf_lsr.adv_router)));
361
362              offset+=12;
363         }
364     }
365 }
366 void
367 dissect_ospf_ls_upd(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
368     e_ospf_lsa_upd_hdr upd_hdr;
369     guint32 lsa_counter; 
370
371     proto_tree *ospf_lsa_upd_tree=NULL;
372         proto_item *ti; 
373
374     memcpy(&upd_hdr, &pd[offset], sizeof(e_ospf_lsa_upd_hdr));
375
376     if (tree) {
377         ti = proto_tree_add_text(tree, offset, END_OF_FRAME, "LS Update Packet"); 
378         ospf_lsa_upd_tree = proto_item_add_subtree(ti, ett_ospf_lsa_upd);
379
380         proto_tree_add_text(ospf_lsa_upd_tree, offset, 4, "Nr oF LSAs: %ld", (long)ntohl(upd_hdr.lsa_nr) );
381     }
382     /* skip to the beginning of the first LSA */
383     offset+=4; /* the LS Upd PAcket contains only a 32 bit #LSAs field */
384     
385     lsa_counter = 0;
386     while(lsa_counter < ntohl(upd_hdr.lsa_nr)){
387         offset+=dissect_ospf_lsa(pd, offset, fd, ospf_lsa_upd_tree, TRUE);
388         lsa_counter += 1;
389     }
390 }
391
392 void
393 dissect_ospf_ls_ack(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
394
395     /* the body of a LS Ack packet simply contains zero or more LSA Headers */
396     while( ((int)(pi.captured_len - offset)) >= OSPF_LSA_HEADER_LENGTH ) {
397        dissect_ospf_lsa(pd, offset, fd, tree, FALSE);
398        offset+=OSPF_LSA_HEADER_LENGTH;
399     }
400
401 }
402
403 int
404 dissect_ospf_lsa(const u_char *pd, int offset, frame_data *fd, proto_tree *tree, int disassemble_body) {
405     e_ospf_lsa_hdr       lsa_hdr;
406     char                *lsa_type;
407     int                  lsa_end;
408
409     /* data strutures for the router LSA */
410     e_ospf_router_lsa           router_lsa;
411     e_ospf_router_data          router_data;
412     e_ospf_router_metric        tos_data;
413     guint16                     link_counter;
414     guint8                      tos_counter;
415     char                        *link_type;
416     char                        *link_id;
417
418     /* data structures for the network lsa */
419     e_ospf_network_lsa  network_lsa;
420     guint32             *attached_router;
421
422     /* data structures for the summary and ASBR LSAs */
423     e_ospf_summary_lsa  summary_lsa;
424
425     /* data structures for the AS-External LSA */
426     e_ospf_asexternal_lsa       asext_lsa;
427     guint32                   asext_metric;
428
429     proto_tree *ospf_lsa_tree;
430         proto_item *ti; 
431
432     memcpy(&lsa_hdr, &pd[offset], sizeof(e_ospf_lsa_hdr));
433
434              
435
436     switch(lsa_hdr.ls_type) {
437         case OSPF_LSTYPE_ROUTER:
438             lsa_type="Router LSA";
439             break;
440         case OSPF_LSTYPE_NETWORK:
441             lsa_type="Network LSA";
442             break;
443         case OSPF_LSTYPE_SUMMERY:
444             lsa_type="Summary LSA";
445             break;
446         case OSPF_LSTYPE_ASBR:
447             lsa_type="ASBR LSA";
448             break;
449         case OSPF_LSTYPE_ASEXT:
450             lsa_type="AS-external-LSA";
451             break;
452         default:
453             lsa_type="unknown";
454     }
455
456     if (tree) {
457         if(disassemble_body){
458              ti = proto_tree_add_text(tree, offset, ntohs(lsa_hdr.length), 
459                                                       "%s (Type: %d)", lsa_type, lsa_hdr.ls_type); 
460         } else {
461              ti = proto_tree_add_text(tree, offset, OSPF_LSA_HEADER_LENGTH, "LSA Header"); 
462         }
463         ospf_lsa_tree = proto_item_add_subtree(ti, ett_ospf_lsa);
464
465         
466         proto_tree_add_text(ospf_lsa_tree, offset, 2, "LS Age: %d seconds", ntohs(lsa_hdr.ls_age));
467         proto_tree_add_text(ospf_lsa_tree, offset + 2, 1, "Options: %d ", lsa_hdr.options);
468         proto_tree_add_text(ospf_lsa_tree, offset + 3, 1, "LSA Type: %d (%s)", lsa_hdr.ls_type, lsa_type);
469
470         proto_tree_add_text(ospf_lsa_tree, offset + 4, 4, "Linke State ID: %s ", 
471                                                      ip_to_str((guint8 *) &(lsa_hdr.ls_id)));
472
473         proto_tree_add_text(ospf_lsa_tree, offset + 8, 4, "Advertising Router: %s ", 
474                                                      ip_to_str((guint8 *) &(lsa_hdr.adv_router)));
475         proto_tree_add_text(ospf_lsa_tree, offset + 12, 4, "LS Sequence Number: 0x%04lx ", 
476                                                      (unsigned long)ntohl(lsa_hdr.ls_seq));
477         proto_tree_add_text(ospf_lsa_tree, offset + 16, 2, "LS Checksum: %d ", ntohs(lsa_hdr.ls_checksum));
478
479         proto_tree_add_text(ospf_lsa_tree, offset + 18, 2, "Length: %d ", ntohs(lsa_hdr.length));
480
481         if(!disassemble_body){
482            return OSPF_LSA_HEADER_LENGTH;
483         }
484
485         lsa_end = offset + ntohs(lsa_hdr.length);
486         /* the LSA body starts after 20 bytes of LSA Header */
487         offset+=20;
488
489         switch(lsa_hdr.ls_type){
490             case(OSPF_LSTYPE_ROUTER):
491                 memcpy(&router_lsa, &pd[offset], sizeof(e_ospf_router_lsa));
492
493                 /* again: flags should be secified in detail */
494                 proto_tree_add_text(ospf_lsa_tree, offset, 1, "Flags: 0x%02x ", router_lsa.flags);
495                 proto_tree_add_text(ospf_lsa_tree, offset + 2, 2, "Nr. of Links: %d ", 
496                                                                    ntohs(router_lsa.nr_links));
497                 offset += 4;
498                 /* router_lsa.nr_links links follow 
499                  * maybe we should put each of the links into its own subtree ???
500                  */
501                 for(link_counter = 1 ; link_counter <= ntohs(router_lsa.nr_links); link_counter++){
502
503                     memcpy(&router_data, &pd[offset], sizeof(e_ospf_router_data));
504                     /* check the Link Type and ID */
505                     switch(router_data.link_type) {
506                         case OSPF_LINK_PTP:
507                             link_type="Point-to-point connection to another router";
508                             link_id="Neighboring router's Router ID";
509                             break;
510                         case OSPF_LINK_TRANSIT:
511                             link_type="Connection to a transit network";
512                             link_id="IP address of Designated Router";
513                             break;
514                         case OSPF_LINK_STUB:
515                             link_type="Connection to a stub network";
516                             link_id="IP network/subnet number";
517                             break;
518                         case OSPF_LINK_VIRTUAL:
519                             link_type="Virtual link";
520                             link_id="Neighboring router's Router ID";
521                             break;
522                         default:
523                             link_type="unknown link type";
524                             link_id="unknown link id";
525                     }
526
527                     proto_tree_add_text(ospf_lsa_tree, offset, 4, "%s: %s", link_id,
528                                                    ip_to_str((guint8 *) &(router_data.link_id)));
529
530                     /* link_data should be specified in detail (e.g. network mask) (depends on link type)*/
531                     proto_tree_add_text(ospf_lsa_tree, offset + 4, 4, "Link Data: %s", 
532                                                    ip_to_str((guint8 *) &(router_data.link_data)));
533
534                     proto_tree_add_text(ospf_lsa_tree, offset + 8, 1, "Link Type: %d - %s", 
535                                                               router_data.link_type, link_type);
536                     proto_tree_add_text(ospf_lsa_tree, offset + 9, 1, "Nr. of TOS metrics: %d", router_data.nr_tos);
537                     proto_tree_add_text(ospf_lsa_tree, offset + 10, 2, "TOS 0 metric: %d", ntohs( router_data.tos0_metric ));
538
539                     offset += 12;
540
541                     /* router_data.nr_tos metrics may follow each link 
542                      * ATTENTION: TOS metrics are not tested (I don't have TOS based routing)
543                      * please send me a mail if it is/isn't working
544                      */
545
546                     for(tos_counter = 1 ; link_counter <= ntohs(router_data.nr_tos); tos_counter++){
547                         memcpy(&tos_data, &pd[offset], sizeof(e_ospf_router_metric));
548                         proto_tree_add_text(ospf_lsa_tree, offset, 1, "TOS: %d, Metric: %d", 
549                                                 tos_data.tos, ntohs(tos_data.metric));
550                         offset += 4;
551                     }
552                 }
553                 break;
554             case(OSPF_LSTYPE_NETWORK):
555                 memcpy(&network_lsa, &pd[offset], sizeof(e_ospf_network_lsa));
556                 proto_tree_add_text(ospf_lsa_tree, offset, 4, "Netmask: %s", 
557                                                  ip_to_str((guint8 *) &(network_lsa.network_mask)));
558                 offset += 4;
559
560                 while( (lsa_end - offset) >= 4){
561                     attached_router = (guint32 *) &pd[offset];
562                     proto_tree_add_text(ospf_lsa_tree, offset, 4, "Attached Router: %s", 
563                                                  ip_to_str((guint8 *) attached_router));
564                     offset += 4;
565                 }
566                 break;
567             case(OSPF_LSTYPE_SUMMERY):
568                 /* Type 3 and 4 LSAs have the same format */
569             case(OSPF_LSTYPE_ASBR):
570                 memcpy(&summary_lsa, &pd[offset], sizeof(e_ospf_summary_lsa));
571                 proto_tree_add_text(ospf_lsa_tree, offset, 4, "Netmask: %s", 
572                                                  ip_to_str((guint8 *) &(summary_lsa.network_mask)));
573                 /* returns only the TOS 0 metric (even if there are more TOS metrics) */
574                 break;
575             case(OSPF_LSTYPE_ASEXT):
576                 memcpy(&summary_lsa, &pd[offset], sizeof(e_ospf_summary_lsa));
577                 proto_tree_add_text(ospf_lsa_tree, offset, 4, "Netmask: %s", 
578                                                   ip_to_str((guint8 *) &(summary_lsa.network_mask)));
579
580                 /* asext_lsa = (e_ospf_asexternal_lsa *) &pd[offset + 4]; */
581                 memcpy(&asext_lsa, &pd[offset + 4], sizeof(asext_lsa));
582                 if( (asext_lsa.options & 128) == 128 ) { /* check wether or not E bit is set */
583                    proto_tree_add_text(ospf_lsa_tree, offset, 1, 
584                             "External Type: Type 2 (metric is larger than any other link state path)");
585                 } else {
586                    proto_tree_add_text(ospf_lsa_tree, offset + 4, 1, 
587                             "External Type: Type 1 (metric is specified in the same units as interface cost)");
588                 }
589                 /* the metric field of a AS-external LAS is specified in 3 bytes -> not well aligned */
590                 /* this routine returns only the TOS 0 metric (even if there are more TOS metrics) */
591                 memcpy(&asext_metric, &pd[offset+4], 4); 
592                 
593                 /* erase the leading 8 bits (the dont belong to the metric */
594                 asext_metric = ntohl(asext_metric) & 0x00ffffff ;
595
596                 proto_tree_add_text(ospf_lsa_tree, offset + 5,  3,"Metric: %d", asext_metric);
597                 proto_tree_add_text(ospf_lsa_tree, offset + 8,  4,"Forwarding Address: %s", 
598                                                  ip_to_str((guint8 *) &(asext_lsa.gateway)));
599                 proto_tree_add_text(ospf_lsa_tree, offset + 12, 4,"External Route Tag: %ld", (long)ntohl(asext_lsa.external_tag)); 
600                     
601                 break;
602             default:
603                /* unknown LSA type */
604                 proto_tree_add_text(ospf_lsa_tree, offset, END_OF_FRAME, "Unknown LSA Type");
605         }
606     }
607     /* return the length of this LSA */
608     return ntohs(lsa_hdr.length);
609 }
610
611 void
612 proto_register_ospf(void)
613 {
614 /*        static hf_register_info hf[] = {
615                 { &variable,
616                 { "Name",           "ospf.abbreviation", TYPE, VALS_POINTER }},
617         };*/
618         static gint *ett[] = {
619                 &ett_ospf,
620                 &ett_ospf_hdr,
621                 &ett_ospf_hello,
622                 &ett_ospf_desc,
623                 &ett_ospf_lsr,
624                 &ett_ospf_lsa,
625                 &ett_ospf_lsa_upd,
626         };
627
628         proto_ospf = proto_register_protocol("Open Shortest Path First", "ospf");
629  /*       proto_register_field_array(proto_ospf, hf, array_length(hf));*/
630         proto_register_subtree_array(ett, array_length(ett));
631 }