Statistics
| Branch: | Revision:

iof-bird-daemon / proto / ospf / ospf.c @ c27b2449

History | View | Annotate | Download (39.5 KB)

1
/*
2
 *        BIRD -- OSPF
3
 *
4
 *        (c) 1999--2004 Ondrej Filip <feela@network.cz>
5
 *
6
 *        Can be freely distributed and used under the terms of the GNU GPL.
7
 */
8

    
9
/**
10
 * DOC: Open Shortest Path First (OSPF)
11
 * 
12
 * The OSPF protocol is quite complicated and its complex implemenation is
13
 * split to many files. In |ospf.c|, you will find mainly the interface
14
 * for communication with the core (e.g., reconfiguration hooks, shutdown
15
 * and initialisation and so on). In |packet.c|, you will find various
16
 * functions for sending and receiving generic OSPF packets. There are
17
 * also routines for authentication and checksumming. File |iface.c| contains
18
 * the interface state machine and functions for allocation and deallocation of OSPF's
19
 * interface data structures. Source |neighbor.c| includes the neighbor state
20
 * machine and functions for election of Designated Router and Backup
21
 * Designated router. In |hello.c|, there are routines for sending
22
 * and receiving of hello packets as well as functions for maintaining
23
 * wait times and the inactivity timer. Files |lsreq.c|, |lsack.c|, |dbdes.c|
24
 * contain functions for sending and receiving of link-state requests,
25
 * link-state acknowledgements and database descriptions respectively.
26
 * In |lsupd.c|, there are functions for sending and receiving
27
 * of link-state updates and also the flooding algorithm. Source |topology.c| is
28
 * a place where routines for searching LSAs in the link-state database,
29
 * adding and deleting them reside, there also are functions for originating
30
 * of various types of LSAs (router LSA, net LSA, external LSA). File |rt.c|
31
 * contains routines for calculating the routing table. |lsalib.c| is a set
32
 * of various functions for working with the LSAs (endianity conversions,
33
 * calculation of checksum etc.).
34
 *
35
 * One instance of the protocol is able to hold LSA databases for
36
 * multiple OSPF areas, to exchange routing information between
37
 * multiple neighbors and to calculate the routing tables. The core
38
 * structure is &proto_ospf to which multiple &ospf_area and
39
 * &ospf_iface structures are connected. &ospf_area is also connected to
40
 * &top_hash_graph which is a dynamic hashing structure that
41
 * describes the link-state database. It allows fast search, addition
42
 * and deletion. Each LSA is kept in two pieces: header and body. Both of them are
43
 * kept in the endianity of the CPU.
44
 *
45
 * The heart beat of ospf is ospf_disp(). It is called at regular intervals
46
 * (&proto_ospf->tick). It is responsible for aging and flushing of LSAs in
47
 * the database, for routing table calculaction and it call area_disp() of every
48
 * ospf_area.
49
 * 
50
 * The function area_disp() is
51
 * responsible for late originating of router LSA and network LSA
52
 * and for cleanup after routing table calculation process in
53
 * the area.
54
 * To every &ospf_iface, we connect one or more
55
 * &ospf_neighbor's -- a structure containing many timers and queues
56
 * for building adjacency and for exchange of routing messages.
57
 *
58
 * BIRD's OSPF implementation respects RFC2328 in every detail, but
59
 * some of internal algorithms do differ. The RFC recommends making a snapshot
60
 * of the link-state database when a new adjacency is forming and sending
61
 * the database description packets based on the information in this 
62
 * snapshot. The database can be quite large in some networks, so
63
 * rather we walk through a &slist structure which allows us to
64
 * continue even if the actual LSA we were working with is deleted. New
65
 * LSAs are added at the tail of this &slist.
66
 *
67
 * We also don't keep a separate OSPF routing table, because the core
68
 * helps us by being able to recognize when a route is updated
69
 * to an identical one and it suppresses the update automatically.
70
 * Due to this, we can flush all the routes we've recalculated and
71
 * also those we've deleted to the core's routing table and the
72
 * core will take care of the rest. This simplifies the process
73
 * and conserves memory.
74
 */
75

    
76
#include <stdlib.h>
77
#include "ospf.h"
78

    
79

    
80
static int ospf_reload_routes(struct proto *p);
81
static void ospf_rt_notify(struct proto *p, net * n, rte * new, rte * old UNUSED, ea_list * attrs);
82
static void ospf_ifa_notify(struct proto *p, unsigned flags, struct ifa *a);
83
static int ospf_rte_better(struct rte *new, struct rte *old);
84
static int ospf_rte_same(struct rte *new, struct rte *old);
85
static void ospf_disp(timer *timer);
86

    
87
static void
88
ospf_area_initfib(struct fib_node *fn)
89
{
90
  struct area_net *an = (struct area_net *) fn;
91
  an->hidden = 0;
92
  an->active = -1;        /* Force to regenerate summary lsa */
93
        /* ac->oldactive will be rewritten by ospf_rt_spf() */
94
}
95

    
96
static void
97
add_area_nets(struct ospf_area *oa, struct ospf_area_config *ac)
98
{
99
    struct proto_ospf *po = oa->po;
100
    struct proto *p = &po->proto;
101
    struct area_net_config *anet;
102
    struct area_net *antmp;
103

    
104
    fib_init(&oa->net_fib, p->pool, sizeof(struct area_net), 16, ospf_area_initfib);
105

    
106
    WALK_LIST(anet, ac->net_list)
107
    {
108
      antmp = (struct area_net *) fib_get(&oa->net_fib, &anet->px.addr, anet->px.len);
109
      antmp->hidden = anet->hidden;
110
    }
111
}
112

    
113
static int
114
ospf_start(struct proto *p)
115
{
116
  struct proto_ospf *po = (struct proto_ospf *) p;
117
  struct ospf_config *c = (struct ospf_config *) (p->cf);
118
  struct ospf_area_config *ac;
119
  struct ospf_area *oa;
120

    
121
  po->router_id = proto_get_router_id(p->cf);
122
  po->rfc1583 = c->rfc1583;
123
  po->ebit = 0;
124
  po->tick = c->tick;
125
  po->disp_timer = tm_new(p->pool);
126
  po->disp_timer->data = po;
127
  po->disp_timer->randomize = 0;
128
  po->disp_timer->hook = ospf_disp;
129
  po->disp_timer->recurrent = po->tick;
130
  tm_start(po->disp_timer, 1);
131
  po->lsab_size = 256;
132
  po->lsab_used = 0;
133
  po->lsab = mb_alloc(p->pool, po->lsab_size);
134
  init_list(&(po->iface_list));
135
  init_list(&(po->area_list));
136
  fib_init(&po->rtf, p->pool, sizeof(ort), 16, ospf_rt_initort);
137
  po->areano = 0;
138
  po->gr = ospf_top_new(p->pool);
139
  po->cleanup = 1;
140
  s_init_list(&(po->lsal));
141
  if (EMPTY_LIST(c->area_list))
142
  {
143
    log(L_ERR "Cannot start, no OSPF areas configured!");
144
    return PS_DOWN;
145
  }
146

    
147
  WALK_LIST(ac, c->area_list)
148
  {
149
    oa = mb_allocz(p->pool, sizeof(struct ospf_area));
150
    add_tail(&po->area_list, NODE oa);
151
    po->areano++;
152
    oa->ac = ac;
153
    oa->stub = ac->stub;
154
    oa->areaid = ac->areaid;
155
    oa->rt = NULL;
156
    oa->po = po;
157
    add_area_nets(oa, ac);
158
    fib_init(&oa->rtr, p->pool, sizeof(ort), 16, ospf_rt_initort);
159

    
160
    if (oa->areaid == 0)
161
    {
162
      po->backbone = oa;
163
      if (oa->stub) log(L_ERR "Backbone cannot be stub. Ignoring!");
164
      oa->stub = 0;
165
    }
166

    
167
#ifdef OSPFv2
168
    oa->options = (oa->stub ? 0 : OPT_E);
169
#else /* OSPFv3 */
170
    oa->options = OPT_R | (oa->stub ? 0 : OPT_E) | OPT_V6;
171
#endif
172
  }
173

    
174
  /* Add all virtual links as interfaces */
175
  {
176
    struct ospf_iface_patt *ipatt;
177
    WALK_LIST(ac, c->area_list)
178
    {
179
      WALK_LIST(ipatt, ac->vlink_list)
180
      {
181
        if(!po->backbone)
182
        {
183
          oa = mb_allocz(p->pool, sizeof(struct ospf_area));
184
          add_tail(&po->area_list, NODE oa);
185
          po->areano++;
186
          oa->stub = 0;
187
          oa->areaid = 0;
188
          oa->rt = NULL;
189
          oa->po = po;
190
          fib_init(&oa->net_fib, p->pool, sizeof(struct area_net), 16, ospf_area_initfib);
191
          fib_init(&oa->rtr, p->pool, sizeof(ort), 16, ospf_rt_initort);
192
          po->backbone = oa;
193
#ifdef OSPFv2
194
          oa->options = OPT_E;
195
#else /* OSPFv3 */
196
          oa->options = OPT_R | OPT_E | OPT_V6;
197
#endif
198
        }
199
        ospf_iface_new(po, NULL, ac, ipatt);
200
      }
201
    }
202
  }
203
  return PS_UP;
204
}
205

    
206
static void
207
ospf_dump(struct proto *p)
208
{
209
  struct ospf_iface *ifa;
210
  struct ospf_neighbor *n;
211
  struct proto_ospf *po = (struct proto_ospf *) p;
212

    
213
  OSPF_TRACE(D_EVENTS, "Area number: %d", po->areano);
214

    
215
  WALK_LIST(ifa, po->iface_list)
216
  {
217
    OSPF_TRACE(D_EVENTS, "Interface: %s", (ifa->iface ? ifa->iface->name : "(null)"));
218
    OSPF_TRACE(D_EVENTS, "state: %u", ifa->state);
219
    OSPF_TRACE(D_EVENTS, "DR:  %R", ifa->drid);
220
    OSPF_TRACE(D_EVENTS, "BDR: %R", ifa->bdrid);
221
    WALK_LIST(n, ifa->neigh_list)
222
    {
223
      OSPF_TRACE(D_EVENTS, "  neighbor %R in state %u", n->rid, n->state);
224
    }
225
  }
226

    
227
  OSPF_TRACE(D_EVENTS, "LSA graph dump start:");
228
  ospf_top_dump(po->gr, p);
229
  OSPF_TRACE(D_EVENTS, "LSA graph dump finished");
230
  neigh_dump_all();
231
}
232

    
233
static struct proto *
234
ospf_init(struct proto_config *c)
235
{
236
  struct proto *p = proto_new(c, sizeof(struct proto_ospf));
237

    
238
  p->make_tmp_attrs = ospf_make_tmp_attrs;
239
  p->store_tmp_attrs = ospf_store_tmp_attrs;
240
  p->import_control = ospf_import_control;
241
  p->reload_routes = ospf_reload_routes;
242
  p->accept_ra_types = RA_OPTIMAL;
243
  p->rt_notify = ospf_rt_notify;
244
  p->if_notify = ospf_iface_notify;
245
  p->ifa_notify = ospf_ifa_notify;
246
  p->rte_better = ospf_rte_better;
247
  p->rte_same = ospf_rte_same;
248

    
249
  return p;
250
}
251

    
252
/* If new is better return 1 */
253
static int
254
ospf_rte_better(struct rte *new, struct rte *old)
255
{
256
  if (new->u.ospf.metric1 == LSINFINITY)
257
    return 0;
258

    
259
  if(new->attrs->source < old->attrs->source) return 1;
260
  if(new->attrs->source > old->attrs->source) return 0;
261

    
262
  if(new->attrs->source == RTS_OSPF_EXT2)
263
  {
264
    if(new->u.ospf.metric2 < old->u.ospf.metric2) return 1;
265
    if(new->u.ospf.metric2 > old->u.ospf.metric2) return 0;
266
  }
267

    
268
  if (new->u.ospf.metric1 < old->u.ospf.metric1)
269
    return 1;
270

    
271
  return 0;                        /* Old is shorter or same */
272
}
273

    
274
static int
275
ospf_rte_same(struct rte *new, struct rte *old)
276
{
277
  /* new->attrs == old->attrs always */
278
  return
279
    new->u.ospf.metric1 == old->u.ospf.metric1 &&
280
    new->u.ospf.metric2 == old->u.ospf.metric2 &&
281
    new->u.ospf.tag == old->u.ospf.tag &&
282
    new->u.ospf.router_id == old->u.ospf.router_id;
283
}
284

    
285
static ea_list *
286
ospf_build_attrs(ea_list * next, struct linpool *pool, u32 m1, u32 m2,
287
                 u32 tag, u32 rid)
288
{
289
  struct ea_list *l =
290
    lp_alloc(pool, sizeof(struct ea_list) + 4 * sizeof(eattr));
291

    
292
  l->next = next;
293
  l->flags = EALF_SORTED;
294
  l->count = 4;
295
  l->attrs[0].id = EA_OSPF_METRIC1;
296
  l->attrs[0].flags = 0;
297
  l->attrs[0].type = EAF_TYPE_INT | EAF_TEMP;
298
  l->attrs[0].u.data = m1;
299
  l->attrs[1].id = EA_OSPF_METRIC2;
300
  l->attrs[1].flags = 0;
301
  l->attrs[1].type = EAF_TYPE_INT | EAF_TEMP;
302
  l->attrs[1].u.data = m2;
303
  l->attrs[2].id = EA_OSPF_TAG;
304
  l->attrs[2].flags = 0;
305
  l->attrs[2].type = EAF_TYPE_INT | EAF_TEMP;
306
  l->attrs[2].u.data = tag;
307
  l->attrs[3].id = EA_OSPF_ROUTER_ID;
308
  l->attrs[3].flags = 0;
309
  l->attrs[3].type = EAF_TYPE_INT | EAF_TEMP;
310
  l->attrs[3].u.data = rid;
311
  return l;
312
}
313

    
314
void
315
schedule_net_lsa(struct ospf_iface *ifa)
316
{
317
  struct proto *p = &ifa->oa->po->proto;
318

    
319
  OSPF_TRACE(D_EVENTS, "Scheduling network-LSA origination for iface %s", ifa->iface->name);
320
  ifa->orignet = 1;
321
}
322

    
323
#ifdef OSPFv3
324
void
325
schedule_link_lsa(struct ospf_iface *ifa)
326
{
327
  struct proto *p = &ifa->oa->po->proto;
328

    
329
  OSPF_TRACE(D_EVENTS, "Scheduling link-LSA origination for iface %s", ifa->iface->name);
330
  ifa->origlink = 1;
331
}
332
#endif
333

    
334
void
335
schedule_rt_lsa(struct ospf_area *oa)
336
{
337
  struct proto *p = &oa->po->proto;
338

    
339
  OSPF_TRACE(D_EVENTS, "Scheduling router-LSA origination for area %R", oa->areaid);
340
  oa->origrt = 1;
341
}
342

    
343
void
344
schedule_rtcalc(struct proto_ospf *po)
345
{
346
  struct proto *p = &po->proto;
347

    
348
  if (po->calcrt)
349
    return;
350

    
351
  OSPF_TRACE(D_EVENTS, "Scheduling routing table calculation");
352
  po->calcrt = 1;
353
}
354

    
355
static int
356
ospf_reload_routes(struct proto *p)
357
{
358
  struct proto_ospf *po = (struct proto_ospf *) p;
359

    
360
  if (po->calcrt != 2)
361
    OSPF_TRACE(D_EVENTS, "Scheduling routing table calculation with route reload");
362

    
363
  po->calcrt = 2;
364

    
365
  return 1;
366
}
367

    
368
/**
369
 * area_disp - invokes origination of
370
 * router LSA and routing table cleanup
371
 * @oa: ospf area
372
 *
373
 * It invokes aging and when @ospf_area->origrt is set to 1, start
374
 * function for origination of router, network LSAs.
375
 */
376
void
377
area_disp(struct ospf_area *oa)
378
{
379
  struct proto_ospf *po = oa->po;
380
  struct ospf_iface *ifa;
381

    
382
  /* Now try to originage rt_lsa */
383
  if (oa->origrt)
384
    update_rt_lsa(oa);
385

    
386
  /* Now try to originate network LSA's */
387
  WALK_LIST(ifa, po->iface_list)
388
  {
389
#ifdef OSPFv3
390
    /* Link LSA should be originated before Network LSA */
391
    if (ifa->origlink && (ifa->oa == oa))
392
      update_link_lsa(ifa);
393
#endif
394

    
395
    if (ifa->orignet && (ifa->oa == oa))
396
      update_net_lsa(ifa);
397
  }
398
}
399

    
400
/**
401
 * ospf_disp - invokes routing table calctulation, aging and also area_disp()
402
 * @timer: timer usually called every @proto_ospf->tick second, @timer->data
403
 * point to @proto_ospf
404
 */
405
void
406
ospf_disp(timer * timer)
407
{
408
  struct proto_ospf *po = timer->data;
409
  struct ospf_area *oa;
410

    
411
  WALK_LIST(oa, po->area_list)
412
    area_disp(oa);
413

    
414
  /* Age LSA DB */
415
  ospf_age(po);
416

    
417
  /* Calculate routing table */
418
  if (po->calcrt)
419
    ospf_rt_spf(po);
420
}
421

    
422

    
423

    
424
/**
425
 * ospf_import_control - accept or reject new route from nest's routing table
426
 * @p: current instance of protocol
427
 * @new: the new route
428
 * @attrs: list of attributes
429
 * @pool: pool for allocation of attributes
430
 *
431
 * Its quite simple. It does not accept our own routes and leaves the decision on
432
 * import to the filters.
433
 */
434

    
435
int
436
ospf_import_control(struct proto *p, rte ** new, ea_list ** attrs,
437
                    struct linpool *pool)
438
{
439
  rte *e = *new;
440

    
441
  if (p == e->attrs->proto)
442
    return -1;                        /* Reject our own routes */
443
  *attrs = ospf_build_attrs(*attrs, pool, LSINFINITY, 10000, 0, 0);
444
  return 0;                        /* Leave decision to the filters */
445
}
446

    
447
struct ea_list *
448
ospf_make_tmp_attrs(struct rte *rt, struct linpool *pool)
449
{
450
  return ospf_build_attrs(NULL, pool, rt->u.ospf.metric1, rt->u.ospf.metric2,
451
                          rt->u.ospf.tag, rt->u.ospf.router_id);
452
}
453

    
454
void
455
ospf_store_tmp_attrs(struct rte *rt, struct ea_list *attrs)
456
{
457
  rt->u.ospf.metric1 = ea_get_int(attrs, EA_OSPF_METRIC1, LSINFINITY);
458
  rt->u.ospf.metric2 = ea_get_int(attrs, EA_OSPF_METRIC2, 10000);
459
  rt->u.ospf.tag = ea_get_int(attrs, EA_OSPF_TAG, 0);
460
  rt->u.ospf.router_id = ea_get_int(attrs, EA_OSPF_ROUTER_ID, 0);
461
}
462

    
463
/**
464
 * ospf_shutdown - Finish of OSPF instance
465
 * @p: current instance of protocol
466
 *
467
 * RFC does not define any action that should be taken before router
468
 * shutdown. To make my neighbors react as fast as possible, I send
469
 * them hello packet with empty neighbor list. They should start
470
 * their neighbor state machine with event %NEIGHBOR_1WAY.
471
 */
472

    
473
static int
474
ospf_shutdown(struct proto *p)
475
{
476
  struct proto_ospf *po = (struct proto_ospf *) p;
477
  struct ospf_iface *ifa;
478
  OSPF_TRACE(D_EVENTS, "Shutdown requested");
479

    
480
  /* And send to all my neighbors 1WAY */
481
  WALK_LIST(ifa, po->iface_list) ospf_iface_shutdown(ifa);
482

    
483
  return PS_DOWN;
484
}
485

    
486
static void
487
ospf_rt_notify(struct proto *p, net * n, rte * new, rte * old UNUSED,
488
               ea_list * attrs)
489
{
490
  struct proto_ospf *po = (struct proto_ospf *) p;
491

    
492
/* Temporarily down write anything
493
  OSPF_TRACE(D_EVENTS, "Got route %I/%d %s", p->name, n->n.prefix,
494
    n->n.pxlen, new ? "up" : "down");
495
*/
496

    
497
  if (new)                        /* Got some new route */
498
    originate_ext_lsa(n, new, po, attrs);
499
  else
500
    flush_ext_lsa(n, po);
501
}
502

    
503
static void
504
ospf_ifa_notify(struct proto *p, unsigned flags, struct ifa *a)
505
{
506
  struct proto_ospf *po = (struct proto_ospf *) p;
507
  struct ospf_iface *ifa;
508
  
509
  if ((a->flags & IA_SECONDARY) || (a->flags & IA_UNNUMBERED))
510
    return;
511

    
512
  WALK_LIST(ifa, po->iface_list)
513
    {
514
      if (ifa->iface == a->iface)
515
        {
516
          schedule_rt_lsa(ifa->oa);
517
          /* Event 5 from RFC5340 4.4.3. */
518
          schedule_link_lsa(ifa);
519
          return;
520
        }
521
    }
522
}
523

    
524
static void
525
ospf_get_status(struct proto *p, byte * buf)
526
{
527
  struct proto_ospf *po = (struct proto_ospf *) p;
528

    
529
  if (p->proto_state == PS_DOWN)
530
    buf[0] = 0;
531
  else
532
  {
533
    struct ospf_iface *ifa;
534
    struct ospf_neighbor *n;
535
    int adj = 0;
536

    
537
    WALK_LIST(ifa, po->iface_list)
538
      WALK_LIST(n, ifa->neigh_list) if (n->state == NEIGHBOR_FULL)
539
      adj = 1;
540

    
541
    if (adj == 0)
542
      strcpy(buf, "Alone");
543
    else
544
      strcpy(buf, "Running");
545
  }
546
}
547

    
548
static void
549
ospf_get_route_info(rte * rte, byte * buf, ea_list * attrs UNUSED)
550
{
551
  char *type = "<bug>";
552

    
553
  switch(rte->attrs->source)
554
  {
555
    case RTS_OSPF:
556
      type = "I";
557
      break;
558
    case RTS_OSPF_IA:
559
      type = "IA";
560
      break;
561
    case RTS_OSPF_EXT1:
562
      type = "E1";
563
      break;
564
    case RTS_OSPF_EXT2:
565
      type = "E2";
566
      break;
567
  }
568

    
569
  buf += bsprintf(buf, " %s", type);
570
  buf += bsprintf(buf, " (%d/%d", rte->pref, rte->u.ospf.metric1);
571
  if (rte->attrs->source == RTS_OSPF_EXT2)
572
    buf += bsprintf(buf, "/%d", rte->u.ospf.metric2);
573
  buf += bsprintf(buf, ")");
574
  if ((rte->attrs->source == RTS_OSPF_EXT2 || rte->attrs->source == RTS_OSPF_EXT1) && rte->u.ospf.tag)
575
  {
576
    buf += bsprintf(buf, " [%x]", rte->u.ospf.tag);
577
  }
578
  if (rte->u.ospf.router_id)
579
    buf += bsprintf(buf, " [%R]", rte->u.ospf.router_id);
580
}
581

    
582
static int
583
ospf_get_attr(eattr * a, byte * buf, int buflen UNUSED)
584
{
585
  switch (a->id)
586
  {
587
  case EA_OSPF_METRIC1:
588
    bsprintf(buf, "metric1");
589
    return GA_NAME;
590
  case EA_OSPF_METRIC2:
591
    bsprintf(buf, "metric2");
592
    return GA_NAME;
593
  case EA_OSPF_TAG:
594
    bsprintf(buf, "tag: %08x (%u)", a->u.data, a->u.data);
595
    return GA_FULL;
596
 case EA_OSPF_ROUTER_ID:
597
   bsprintf(buf, "router_id: %R (%u)", a->u.data, a->u.data);
598
    return GA_FULL;
599
  default:
600
    return GA_UNKNOWN;
601
  }
602
}
603

    
604
static int
605
ospf_patt_compare(struct ospf_iface_patt *a, struct ospf_iface_patt *b)
606
{
607
  return (a->type == b->type);
608
}
609

    
610
/**
611
 * ospf_reconfigure - reconfiguration hook
612
 * @p: current instance of protocol (with old configuration)
613
 * @c: new configuration requested by user
614
 *
615
 * This hook tries to be a little bit intelligent. Instance of OSPF
616
 * will survive change of many constants like hello interval,
617
 * password change, addition or deletion of some neighbor on
618
 * nonbroadcast network, cost of interface, etc.
619
 */
620
static int
621
ospf_reconfigure(struct proto *p, struct proto_config *c)
622
{
623
  struct ospf_config *old = (struct ospf_config *) (p->cf);
624
  struct ospf_config *new = (struct ospf_config *) c;
625
  struct ospf_area_config *oldac, *newac;
626
  struct proto_ospf *po = (struct proto_ospf *) p;
627
  struct ospf_iface_patt *oldip, *newip;
628
  struct ospf_iface *ifa;
629
  struct nbma_node *nb1, *nb2, *nbnx;
630
  struct ospf_area *oa = NULL;
631
  int found, olddead, newdead;
632
  struct area_net_config *anc;
633
  struct area_net *an;
634

    
635
  if (po->rfc1583 != new->rfc1583)
636
    return 0;
637

    
638
  schedule_rtcalc(po);
639

    
640
  po->tick = new->tick;
641
  po->disp_timer->recurrent = po->tick;
642
  tm_start(po->disp_timer, 1);
643

    
644
  oldac = HEAD(old->area_list);
645
  newac = HEAD(new->area_list);
646

    
647
  /* I should get it in the same order */
648

    
649
  while (((NODE(oldac))->next != NULL) && ((NODE(newac))->next != NULL))
650
  {
651
    if (oldac->areaid != newac->areaid)
652
      return 0;
653

    
654
    WALK_LIST(oa, po->area_list)
655
      if (oa->areaid == newac->areaid)
656
        break;
657

    
658
    if (!oa)
659
      return 0;
660

    
661
    oa->ac = newac;
662
    oa->stub = newac->stub;
663
    if (newac->stub && (oa->areaid == 0)) oa->stub = 0;
664

    
665
    /* Check stubnet_list */
666
    struct ospf_stubnet_config *oldsn = HEAD(oldac->stubnet_list);
667
    struct ospf_stubnet_config *newsn = HEAD(newac->stubnet_list);
668

    
669
    while (((NODE(oldsn))->next != NULL) && ((NODE(newsn))->next != NULL))
670
      {
671
        if (!ipa_equal(oldsn->px.addr, newsn->px.addr) ||
672
            (oldsn->px.len != newsn->px.len) ||
673
            (oldsn->hidden != newsn->hidden) ||
674
            (oldsn->summary != newsn->summary) ||
675
            (oldsn->cost != newsn->cost))
676
          break;
677

    
678
        oldsn = (struct ospf_stubnet_config *)(NODE(oldsn))->next;
679
        newsn = (struct ospf_stubnet_config *)(NODE(newsn))->next;
680
      }
681

    
682
    /* If there is no change, both pointers should be NULL */
683
    if (((NODE(oldsn))->next) != ((NODE(newsn))->next))
684
      schedule_rt_lsa(oa);
685

    
686
    /* Change net_list */
687
    FIB_WALK(&oa->net_fib, nf)        /* First check if some networks are deleted */
688
    {
689
      found = 0;
690
      WALK_LIST(anc, newac->net_list)
691
      {
692
        if (ipa_equal(anc->px.addr, nf->prefix) && (anc->px.len == nf->pxlen))
693
        {
694
          found = 1;
695
          break;
696
        }
697
        if (!found) flush_sum_lsa(oa, nf, ORT_NET);        /* And flush them */
698
      }
699
    }
700
    FIB_WALK_END;
701

    
702
    WALK_LIST(anc, newac->net_list)        /* Second add new networks */
703
    {
704
      an = fib_get(&oa->net_fib, &anc->px.addr, anc->px.len);
705
      an->hidden = anc->hidden;
706
    }
707

    
708
    if (!iface_patts_equal(&oldac->patt_list, &newac->patt_list,
709
                           (void *) ospf_patt_compare))
710
      return 0;
711

    
712
    WALK_LIST(ifa, po->iface_list)
713
    {
714
      if (oldip = (struct ospf_iface_patt *)
715
          iface_patt_find(&oldac->patt_list, ifa->iface))
716
      {
717
        /* Now reconfigure interface */
718
        if (!(newip = (struct ospf_iface_patt *)
719
              iface_patt_find(&newac->patt_list, ifa->iface)))
720
          return 0;
721

    
722
        /* HELLO TIMER */
723
        if (oldip->helloint != newip->helloint)
724
        {
725
          ifa->helloint = newip->helloint;
726
          ifa->hello_timer->recurrent = ifa->helloint;
727
          tm_start(ifa->hello_timer, ifa->helloint);
728
          OSPF_TRACE(D_EVENTS,
729
                     "Changing hello interval on interface %s from %d to %d",
730
                     ifa->iface->name, oldip->helloint, newip->helloint);
731
        }
732

    
733
        /* POLL TIMER */
734
        if (oldip->pollint != newip->pollint)
735
        {
736
          ifa->pollint = newip->helloint;
737
          ifa->poll_timer->recurrent = ifa->pollint;
738
          tm_start(ifa->poll_timer, ifa->pollint);
739
          OSPF_TRACE(D_EVENTS,
740
                     "Changing poll interval on interface %s from %d to %d",
741
                     ifa->iface->name, oldip->pollint, newip->pollint);
742
        }
743

    
744
        /* COST */
745
        if (oldip->cost != newip->cost)
746
        {
747
          ifa->cost = newip->cost;
748
          OSPF_TRACE(D_EVENTS,
749
                     "Changing cost interface %s from %d to %d",
750
                     ifa->iface->name, oldip->cost, newip->cost);
751
          schedule_rt_lsa(ifa->oa);
752
        }
753

    
754
        /* RX BUFF */
755
        if (oldip->rxbuf != newip->rxbuf)
756
        {
757
          ifa->rxbuf = newip->rxbuf;
758
          OSPF_TRACE(D_EVENTS,
759
                     "Changing rxbuf interface %s from %d to %d",
760
                     ifa->iface->name, oldip->rxbuf, newip->rxbuf);
761
          ospf_iface_change_mtu(po, ifa);
762
        }
763

    
764
        /* strict nbma */
765
        if ((oldip->strictnbma == 0) && (newip->strictnbma != 0))
766
        {
767
          ifa->strictnbma = newip->strictnbma;
768
          OSPF_TRACE(D_EVENTS,
769
                     "Interface %s is now strict NBMA.", ifa->iface->name);
770
        }
771
        if ((oldip->strictnbma != 0) && (newip->strictnbma == 0))
772
        {
773
          ifa->strictnbma = newip->strictnbma;
774
          OSPF_TRACE(D_EVENTS,
775
                     "Interface %s is no longer strict NBMA.",
776
                     ifa->iface->name);
777
        }
778

    
779
        /* stub */
780
        if ((oldip->stub == 0) && (newip->stub != 0))
781
        {
782
          ifa->stub = newip->stub;
783
          OSPF_TRACE(D_EVENTS, "Interface %s is now stub.", ifa->iface->name);
784
        }
785
        if ((oldip->stub != 0) && (newip->stub == 0) &&
786
            ((ifa->ioprob & OSPF_I_IP) == 0) &&
787
            (((ifa->ioprob & OSPF_I_MC) == 0) || (ifa->type == OSPF_IT_NBMA)))
788
        {
789
          ifa->stub = newip->stub;
790
          OSPF_TRACE(D_EVENTS,
791
                     "Interface %s is no longer stub.", ifa->iface->name);
792
        }
793

    
794
#ifdef OSPFv2        
795
        /* AUTHENTICATION */
796
        if (oldip->autype != newip->autype)
797
        {
798
          ifa->autype = newip->autype;
799
          OSPF_TRACE(D_EVENTS,
800
                     "Changing authentication type on interface %s",
801
                     ifa->iface->name);
802
        }
803
        /* Add *passwords */
804
        ifa->passwords = newip->passwords;
805
#endif
806

    
807
        /* priority */
808
        if (oldip->priority != newip->priority)
809
        {
810
          ifa->priority = newip->priority;
811
          OSPF_TRACE(D_EVENTS,
812
                     "Changing priority on interface %s from %d to %d",
813
                     ifa->iface->name, oldip->priority, newip->priority);
814
        }
815

    
816
        /* RXMT */
817
        if (oldip->rxmtint != newip->rxmtint)
818
        {
819
          ifa->rxmtint = newip->rxmtint;
820
          OSPF_TRACE(D_EVENTS,
821
                     "Changing retransmit interval on interface %s from %d to %d",
822
                     ifa->iface->name, oldip->rxmtint, newip->rxmtint);
823
        }
824

    
825
        /* WAIT */
826
        if (oldip->waitint != newip->waitint)
827
        {
828
          ifa->waitint = newip->waitint;
829
          if (ifa->wait_timer->expires != 0)
830
            tm_start(ifa->wait_timer, ifa->waitint);
831
          OSPF_TRACE(D_EVENTS,
832
                     "Changing wait interval on interface %s from %d to %d",
833
                     ifa->iface->name, oldip->waitint, newip->waitint);
834
        }
835

    
836
        /* INFTRANS */
837
        if (oldip->inftransdelay != newip->inftransdelay)
838
        {
839
          ifa->inftransdelay = newip->inftransdelay;
840
          OSPF_TRACE(D_EVENTS,
841
                     "Changing transmit delay on interface %s from %d to %d",
842
                     ifa->iface->name, oldip->inftransdelay,
843
                     newip->inftransdelay);
844
        }
845

    
846
        /* DEAD */
847
        olddead = (oldip->dead == 0) ? oldip->deadc * oldip->helloint : oldip->dead;
848
        newdead = (newip->dead == 0) ? newip->deadc * newip->helloint : newip->dead;
849
        if (olddead != newdead)
850
        {
851
          ifa->dead = newdead;
852
          OSPF_TRACE(D_EVENTS,
853
                     "Changing dead interval on interface %s from %d to %d",
854
                     ifa->iface->name, olddead, newdead);
855
        }
856

    
857
        /* NBMA LIST */
858
        /* First remove old */
859
        WALK_LIST_DELSAFE(nb1, nbnx, ifa->nbma_list)
860
        {
861
          found = 0;
862
          WALK_LIST(nb2, newip->nbma_list)
863
            if (ipa_compare(nb1->ip, nb2->ip) == 0)
864
          {
865
            found = 1;
866
            if (nb1->eligible != nb2->eligible)
867
              OSPF_TRACE(D_EVENTS,
868
                         "Changing neighbor eligibility %I on interface %s",
869
                         nb1->ip, ifa->iface->name);
870
            break;
871
          }
872

    
873
          if (!found)
874
          {
875
            OSPF_TRACE(D_EVENTS,
876
                       "Removing NBMA neighbor %I on interface %s",
877
                       nb1->ip, ifa->iface->name);
878
            rem_node(NODE nb1);
879
            mb_free(nb1);
880
          }
881
        }
882
        /* And then add new */
883
        WALK_LIST(nb2, newip->nbma_list)
884
        {
885
          found = 0;
886
          WALK_LIST(nb1, ifa->nbma_list)
887
            if (ipa_compare(nb1->ip, nb2->ip) == 0)
888
          {
889
            found = 1;
890
            break;
891
          }
892
          if (!found)
893
          {
894
            nb1 = mb_alloc(p->pool, sizeof(struct nbma_node));
895
            nb1->ip = nb2->ip;
896
            nb1->eligible = nb2->eligible;
897
            add_tail(&ifa->nbma_list, NODE nb1);
898
            OSPF_TRACE(D_EVENTS,
899
                       "Adding NBMA neighbor %I on interface %s",
900
                       nb1->ip, ifa->iface->name);
901
          }
902
        }
903
      }
904
    }
905

    
906
    oldac = (struct ospf_area_config *)(NODE(oldac))->next;
907
    newac = (struct ospf_area_config *)(NODE(newac))->next;
908
  }
909

    
910
  if (((NODE(oldac))->next) != ((NODE(newac))->next))
911
    return 0;                        /* One is not null */
912

    
913
  return 1;                        /* Everything OK :-) */
914
}
915

    
916
void
917
ospf_sh_neigh(struct proto *p, char *iff)
918
{
919
  struct ospf_iface *ifa = NULL, *f;
920
  struct ospf_neighbor *n;
921
  struct proto_ospf *po = (struct proto_ospf *) p;
922

    
923
  if (p->proto_state != PS_UP)
924
  {
925
    cli_msg(-1013, "%s: is not up", p->name);
926
    cli_msg(0, "");
927
    return;
928
  }
929

    
930
  cli_msg(-1013, "%s:", p->name);
931
  cli_msg(-1013, "%-12s\t%3s\t%-15s\t%-5s\t%-10s %-12s", "Router ID", "Pri",
932
          "     State", "DTime", "Interface", "Router IP");
933
  WALK_LIST(ifa, po->iface_list)
934
    if ((iff == NULL) || patmatch(iff, ifa->iface->name))
935
      WALK_LIST(n, ifa->neigh_list)
936
        ospf_sh_neigh_info(n);
937
  cli_msg(0, "");
938
}
939

    
940
void
941
ospf_sh(struct proto *p)
942
{
943
  struct ospf_area *oa;
944
  struct proto_ospf *po = (struct proto_ospf *) p;
945
  struct ospf_iface *ifa;
946
  struct ospf_neighbor *n;
947
  int ifano, nno, adjno, firstfib;
948
  struct area_net *anet;
949

    
950
  if (p->proto_state != PS_UP)
951
  {
952
    cli_msg(-1014, "%s: is not up", p->name);
953
    cli_msg(0, "");
954
    return;
955
  }
956

    
957
  cli_msg(-1014, "%s:", p->name);
958
  cli_msg(-1014, "RFC1583 compatibility: %s", (po->rfc1583 ? "enable" : "disabled"));
959
  cli_msg(-1014, "RT scheduler tick: %d", po->tick);
960
  cli_msg(-1014, "Number of areas: %u", po->areano);
961
  cli_msg(-1014, "Number of LSAs in DB:\t%u", po->gr->hash_entries);
962

    
963
  WALK_LIST(oa, po->area_list)
964
  {
965
    cli_msg(-1014, "\tArea: %R (%u) %s", oa->areaid, oa->areaid,
966
            oa->areaid == 0 ? "[BACKBONE]" : "");
967
    ifano = 0;
968
    nno = 0;
969
    adjno = 0;
970
    WALK_LIST(ifa, po->iface_list)
971
    {
972
      if (oa == ifa->oa)
973
      {
974
        ifano++;
975
        WALK_LIST(n, ifa->neigh_list)
976
        {
977
          nno++;
978
          if (n->state == NEIGHBOR_FULL)
979
            adjno++;
980
        }
981
      }
982
    }
983
    cli_msg(-1014, "\t\tStub:\t%s", oa->stub ? "Yes" : "No");
984
    cli_msg(-1014, "\t\tTransit:\t%s", oa->trcap ? "Yes" : "No");
985
    cli_msg(-1014, "\t\tNumber of interfaces:\t%u", ifano);
986
    cli_msg(-1014, "\t\tNumber of neighbors:\t%u", nno);
987
    cli_msg(-1014, "\t\tNumber of adjacent neighbors:\t%u", adjno);
988

    
989
    firstfib = 1;
990
    FIB_WALK(&oa->net_fib, nftmp)
991
    {
992
      anet = (struct area_net *) nftmp;
993
      if(firstfib)
994
      {
995
        cli_msg(-1014, "\t\tArea networks:");
996
        firstfib = 0;
997
      }
998
      cli_msg(-1014, "\t\t\t%1I/%u\t%s\t%s", anet->fn.prefix, anet->fn.pxlen,
999
                anet->hidden ? "Hidden" : "Advertise", anet->active ? "Active" : "");
1000
    }
1001
    FIB_WALK_END;
1002
  }
1003
  cli_msg(0, "");
1004
}
1005

    
1006
void
1007
ospf_sh_iface(struct proto *p, char *iff)
1008
{
1009
  struct proto_ospf *po = (struct proto_ospf *) p;
1010
  struct ospf_iface *ifa = NULL, *f;
1011

    
1012
  if (p->proto_state != PS_UP)
1013
  {
1014
    cli_msg(-1015, "%s: is not up", p->name);
1015
    cli_msg(0, "");
1016
    return;
1017
  }
1018

    
1019
  cli_msg(-1015, "%s:", p->name);
1020
  WALK_LIST(ifa, po->iface_list)
1021
    if ((iff == NULL) || patmatch(iff, ifa->iface->name))
1022
      ospf_iface_info(ifa);
1023
  cli_msg(0, "");
1024
}
1025

    
1026
/* lsa_compare_for_state() - Compare function for 'show ospf state'
1027
 *
1028
 * First we want to separate network-LSAs and other LSAs (because network-LSAs
1029
 * will be presented as network nodes and other LSAs together as router nodes)
1030
 * Network-LSAs are sorted according to network prefix, other LSAs are sorted
1031
 * according to originating router id (to get all LSA needed to represent one
1032
 * router node together). Then, according to LSA type, ID and age.
1033
 *
1034
 * For OSPFv3, we have to handle also Prefix-LSAs. We would like to put each
1035
 * immediately after the referenced LSA. We will make faked LSA based on ref_
1036
 * values
1037
 */
1038

    
1039
#ifdef OSPFv3
1040

    
1041
static struct ospf_lsa_header *
1042
fake_lsa_from_prefix_lsa(struct ospf_lsa_header *dst, struct ospf_lsa_header *src,
1043
                         struct ospf_lsa_prefix *px)
1044
{
1045
  dst->age = src->age;
1046
  dst->type = px->ref_type;
1047
  dst->id = px->ref_id;
1048
  dst->rt = px->ref_rt;
1049
  dst->sn = src->sn;
1050

    
1051
  return dst;
1052
}
1053

    
1054
#endif
1055

    
1056
static int
1057
lsa_compare_for_state(const void *p1, const void *p2)
1058
{
1059
  struct top_hash_entry * he1 = * (struct top_hash_entry **) p1;
1060
  struct top_hash_entry * he2 = * (struct top_hash_entry **) p2;
1061
  struct ospf_lsa_header *lsa1 = &(he1->lsa);
1062
  struct ospf_lsa_header *lsa2 = &(he2->lsa);
1063

    
1064
  if (he1->domain != he2->domain)
1065
    return he1->domain - he2->domain;
1066

    
1067
#ifdef OSPFv3
1068
  struct ospf_lsa_header lsatmp1, lsatmp2;
1069

    
1070
  int px1 = (lsa1->type == LSA_T_PREFIX);
1071
  int px2 = (lsa2->type == LSA_T_PREFIX);
1072

    
1073
  if (px1)
1074
    lsa1 = fake_lsa_from_prefix_lsa(&lsatmp1, lsa1, he1->lsa_body);
1075

    
1076
  if (px2)
1077
    lsa2 = fake_lsa_from_prefix_lsa(&lsatmp2, lsa2, he2->lsa_body);
1078
#endif
1079

    
1080
  int nt1 = (lsa1->type == LSA_T_NET);
1081
  int nt2 = (lsa2->type == LSA_T_NET);
1082

    
1083
  if (nt1 != nt2)
1084
    return nt1 - nt2;
1085

    
1086
  if (nt1)
1087
  {
1088
#ifdef OSPFv3
1089
    /* In OSPFv3, neworks are named base on ID of DR */
1090
    if (lsa1->rt != lsa2->rt)
1091
      return lsa1->rt - lsa2->rt;
1092
#endif
1093

    
1094
    /* For OSPFv2, this is IP of the network,
1095
       for OSPFv3, this is interface ID */
1096
    if (lsa1->id != lsa2->id)
1097
      return lsa1->id - lsa2->id;
1098

    
1099
#ifdef OSPFv3
1100
    if (px1 != px2)
1101
      return px1 - px2;
1102
#endif
1103

    
1104
    return lsa1->sn - lsa2->sn;
1105
  }
1106
  else 
1107
  {
1108
    if (lsa1->rt != lsa2->rt)
1109
      return lsa1->rt - lsa2->rt;
1110

    
1111
    if (lsa1->type != lsa2->type)
1112
      return lsa1->type - lsa2->type;
1113
  
1114
    if (lsa1->id != lsa2->id)
1115
      return lsa1->id - lsa2->id;
1116

    
1117
#ifdef OSPFv3
1118
    if (px1 != px2)
1119
      return px1 - px2;
1120
#endif
1121
  
1122
    return lsa1->sn - lsa2->sn;
1123
  }
1124
}
1125

    
1126
static inline void
1127
show_lsa_router(struct proto_ospf *po, struct top_hash_entry *he)
1128
{
1129
  struct ospf_lsa_header *lsa = &(he->lsa);
1130
  struct ospf_lsa_rt *rt = he->lsa_body;
1131
  struct ospf_lsa_rt_link *rr = (struct ospf_lsa_rt_link *) (rt + 1);
1132
  int max = lsa_rt_count(lsa);
1133
  int i;
1134

    
1135
  for (i = 0; i < max; i++)
1136
    if (rr[i].type == LSART_VLNK)
1137
      cli_msg(-1016, "\t\tvlink %R metric %u", rr[i].id, rr[i].metric);
1138

    
1139
  for (i = 0; i < max; i++)
1140
    if (rr[i].type == LSART_PTP)
1141
      cli_msg(-1016, "\t\trouter %R metric %u", rr[i].id, rr[i].metric);
1142

    
1143
  for (i = 0; i < max; i++)
1144
    if (rr[i].type == LSART_NET)
1145
    {
1146
#ifdef OSPFv2
1147
      struct top_hash_entry *net_he = ospf_hash_find_net(po->gr, he->domain, rr[i].id);
1148

    
1149
      if (net_he)
1150
      {
1151
        struct ospf_lsa_header *net_lsa = &(net_he->lsa);
1152
        struct ospf_lsa_net *net_ln = net_he->lsa_body;
1153

    
1154
        cli_msg(-1016, "\t\tnetwork %I/%d metric %u", 
1155
                ipa_and(ipa_from_u32(net_lsa->id), net_ln->netmask),
1156
                ipa_mklen(net_ln->netmask), rr[i].metric);
1157
      }
1158
      else
1159
        cli_msg(-1016, "\t\tnetwork [%R] metric %u", rr[i].id, rr[i].metric);
1160

    
1161
#else /* OSPFv3 */
1162
      cli_msg(-1016, "\t\tnetwork [%R-%u] metric %u", rr[i].id, rr[i].nif, rr[i].metric);
1163
#endif
1164
    }
1165

    
1166
#ifdef OSPFv2
1167
  for (i = 0; i < max; i++)
1168
    if (rr[i].type == LSART_STUB)
1169
      cli_msg(-1016, "\t\tstubnet %I/%d metric %u", ipa_from_u32(rr[i].id),
1170
              ipa_mklen(ipa_from_u32(rr[i].data)), rr[i].metric);
1171
#endif
1172
}
1173

    
1174
static inline void
1175
show_lsa_network(struct top_hash_entry *he)
1176
{
1177
  struct ospf_lsa_header *lsa = &(he->lsa);
1178
  struct ospf_lsa_net *ln = he->lsa_body;
1179
  u32 i;
1180

    
1181
#ifdef OSPFv2
1182
  cli_msg(-1016, "");
1183
  cli_msg(-1016, "\tnetwork %I/%d", ipa_and(ipa_from_u32(lsa->id), ln->netmask), ipa_mklen(ln->netmask));
1184
  cli_msg(-1016, "\t\tdr %R", lsa->rt);
1185
#else /* OSPFv3 */
1186
  cli_msg(-1016, "");
1187
  cli_msg(-1016, "\tnetwork [%R-%u]", lsa->rt, lsa->id);
1188
#endif
1189

    
1190
  for (i = 0; i < lsa_net_count(lsa); i++)
1191
    cli_msg(-1016, "\t\trouter %R", ln->routers[i]);
1192
}
1193

    
1194
static inline void
1195
show_lsa_sum_net(struct top_hash_entry *he)
1196
{
1197
  struct ospf_lsa_header *lsa = &(he->lsa);
1198
  ip_addr ip;
1199
  int pxlen;
1200

    
1201
#ifdef OSPFv2
1202
  struct ospf_lsa_sum *ls = he->lsa_body;
1203
  pxlen = ipa_mklen(ls->netmask);
1204
  ip = ipa_and(ipa_from_u32(he->lsa.id), ls->netmask);
1205
#else /* OSPFv3 */
1206
  u8 pxopts;
1207
  u16 rest;
1208
  struct ospf_lsa_sum_net *ls = he->lsa_body;
1209
  lsa_get_ipv6_prefix(ls->prefix, &ip, &pxlen, &pxopts, &rest);
1210
#endif
1211

    
1212
  cli_msg(-1016, "\t\txnetwork %I/%d", ip, pxlen);
1213
}
1214

    
1215
static inline void
1216
show_lsa_sum_rt(struct top_hash_entry *he)
1217
{
1218
  u32 dst_rid, options;
1219

    
1220
#ifdef OSPFv2
1221
  struct ospf_lsa_sum *ls = he->lsa_body;
1222
  dst_rid = he->lsa.id;
1223
  options = 0;
1224
#else /* OSPFv3 */
1225
  struct ospf_lsa_sum_rt *ls = he->lsa_body;
1226
  dst_rid = ls->drid; 
1227
  options = ls->options & OPTIONS_MASK;
1228
#endif
1229

    
1230
  cli_msg(-1016, "\t\txrouter %R", dst_rid);
1231
}
1232

    
1233

    
1234
static inline void
1235
show_lsa_external(struct top_hash_entry *he)
1236
{
1237
  struct ospf_lsa_header *lsa = &(he->lsa);
1238
  struct ospf_lsa_ext *ext = he->lsa_body;
1239
  struct ospf_lsa_ext_tos *et = (struct ospf_lsa_ext_tos *) (ext + 1);
1240
  char str_via[STD_ADDRESS_P_LENGTH + 8] = "";
1241
  char str_tag[16] = "";
1242
  ip_addr ip, rt_fwaddr;
1243
  int pxlen, ebit, rt_fwaddr_valid;
1244
  u32 rt_tag, rt_metric;
1245

    
1246
  rt_metric = ext->metric & METRIC_MASK;
1247
  ebit = ext->metric & LSA_EXT_EBIT;
1248
#ifdef OSPFv2
1249
  ip = ipa_and(ipa_from_u32(lsa->id), ext->netmask);
1250
  pxlen = ipa_mklen(ext->netmask);
1251
  rt_fwaddr = ext->fwaddr;
1252
  rt_fwaddr_valid = !ipa_equal(rt_fwaddr, IPA_NONE);
1253
  rt_tag = ext->tag;
1254
#else /* OSPFv3 */
1255
  u8 pxopts;
1256
  u16 rest;
1257
  u32 *buf = ext->rest;
1258
  buf = lsa_get_ipv6_prefix(buf, &ip, &pxlen, &pxopts, &rest);
1259

    
1260
  rt_fwaddr_valid = ext->metric & LSA_EXT_FBIT;
1261
  if (rt_fwaddr_valid)
1262
    buf = lsa_get_ipv6_addr(buf, &rt_fwaddr);
1263
  else 
1264
    rt_fwaddr = IPA_NONE;
1265

    
1266
  if (ext->metric & LSA_EXT_TBIT)
1267
    rt_tag = *buf++;
1268
  else
1269
    rt_tag = 0;
1270
#endif
1271
  
1272
  if (rt_fwaddr_valid)
1273
    bsprintf(str_via, " via %I", rt_fwaddr);
1274

    
1275
  if (rt_tag)
1276
    bsprintf(str_tag, " tag %08x", rt_tag);
1277

    
1278
  cli_msg(-1016, "\t\texternal %I/%d metric%s %u%s%s", ip, pxlen,
1279
          ebit ? "2" : "", rt_metric, str_via, str_tag);
1280
}
1281

    
1282
#ifdef OSPFv3
1283
static inline void
1284
show_lsa_prefix(struct top_hash_entry *he, struct ospf_lsa_header *olsa)
1285
{
1286
  struct ospf_lsa_header *lsa = &(he->lsa);
1287
  struct ospf_lsa_prefix *px = he->lsa_body;
1288
  struct ospf_lsa_ext *ext = he->lsa_body;
1289
  char *msg;
1290
  ip_addr pxa;
1291
  int pxlen;
1292
  u8 pxopts;
1293
  u16 metric;
1294
  u32 *buf;
1295
  int i;
1296

    
1297
  /* We check whether given prefix-LSA is related to the last non-prefix-LSA */
1298
  if ((olsa == NULL) || (olsa->type != px->ref_type) || (olsa->rt != px->ref_rt) ||
1299
      !(((px->ref_type == LSA_T_RT)  && (px->ref_id == 0)) ||
1300
        ((px->ref_type == LSA_T_NET) && (px->ref_id == olsa->id))))
1301
    return;
1302

    
1303
  buf = px->rest;
1304
  for (i = 0; i < px->pxcount; i++)
1305
    {
1306
      buf = lsa_get_ipv6_prefix(buf, &pxa, &pxlen, &pxopts, &metric);
1307

    
1308
      if (px->ref_type == LSA_T_RT)
1309
        cli_msg(-1016, "\t\tstubnet %I/%d metric %u", pxa, pxlen, metric);
1310
      else
1311
        cli_msg(-1016, "\t\taddress %I/%d", pxa, pxlen);
1312
    }
1313
}
1314
#endif
1315

    
1316
void
1317
ospf_sh_state(struct proto *p, int verbose)
1318
{
1319
  struct proto_ospf *po = (struct proto_ospf *) p;
1320
  struct top_graph *f = po->gr;
1321
  unsigned int i, j1, j2;
1322
  u32 last_rt = 0xFFFFFFFF;
1323
  u32 last_area = 0xFFFFFFFF;
1324

    
1325
#ifdef OSPFv3
1326
  struct ospf_lsa_header *olsa = NULL;
1327
#endif
1328

    
1329
  if (p->proto_state != PS_UP)
1330
  {
1331
    cli_msg(-1016, "%s: is not up", p->name);
1332
    cli_msg(0, "");
1333
    return;
1334
  }
1335

    
1336
  struct top_hash_entry *hea[f->hash_entries];
1337
  struct top_hash_entry *he;
1338

    
1339
  j1 = j2 = 0;
1340
  WALK_SLIST(he, po->lsal)
1341
  {
1342
    int accept;
1343

    
1344
    switch (he->lsa.type)
1345
      {
1346
      case LSA_T_RT:
1347
      case LSA_T_NET:
1348
        accept = 1;
1349
        break;
1350

    
1351
      case LSA_T_SUM_NET:
1352
      case LSA_T_SUM_RT:
1353
      case LSA_T_EXT:
1354
#ifdef OSPFv3
1355
      case LSA_T_PREFIX:
1356
#endif
1357
        accept = verbose;
1358
        break;
1359

    
1360
      default:
1361
        accept = 0;
1362
      }
1363

    
1364
    if (accept)
1365
      hea[j1++] = he;
1366
    else
1367
      j2++;
1368
  }
1369

    
1370
  if ((j1 + j2) != f->hash_entries)
1371
    die("Fatal mismatch");
1372

    
1373
  qsort(hea, j1, sizeof(struct top_hash_entry *), lsa_compare_for_state);
1374

    
1375
  for (i = 0; i < j1; i++)
1376
  {
1377
    if (last_area != hea[i]->domain)
1378
    {
1379
      cli_msg(-1016, "");
1380
      cli_msg(-1016, "area %R", hea[i]->domain);
1381
      last_area = hea[i]->domain;
1382
      last_rt = 0xFFFFFFFF;
1383
    }
1384

    
1385
    if ((hea[i]->lsa.rt != last_rt) && (hea[i]->lsa.type != LSA_T_NET)
1386
#ifdef OSPFv3
1387
        && (hea[i]->lsa.type != LSA_T_PREFIX)
1388
#endif
1389
        )
1390
    {
1391
      cli_msg(-1016, "");
1392
      cli_msg(-1016, (hea[i]->lsa.type != LSA_T_EXT) ? "\trouter %R" : "\txrouter %R", hea[i]->lsa.rt);
1393
      last_rt = hea[i]->lsa.rt;
1394
    }
1395

    
1396
    switch (hea[i]->lsa.type)
1397
    {
1398
      case LSA_T_RT:
1399
        show_lsa_router(po, hea[i]);
1400
        break;
1401

    
1402
      case LSA_T_NET:
1403
        show_lsa_network(hea[i]);
1404
        break;
1405

    
1406
      case LSA_T_SUM_NET:
1407
        show_lsa_sum_net(hea[i]);
1408
        break;
1409

    
1410
      case LSA_T_SUM_RT:
1411
        show_lsa_sum_rt(hea[i]);
1412
        break;
1413

    
1414
      case LSA_T_EXT:
1415
        show_lsa_external(hea[i]);
1416
        break;
1417

    
1418
#ifdef OSPFv3
1419
      case LSA_T_PREFIX:
1420
        show_lsa_prefix(hea[i], olsa);
1421
        break;
1422
#endif
1423
    }
1424

    
1425
#ifdef OSPFv3
1426
    if (hea[i]->lsa.type != LSA_T_PREFIX)
1427
      olsa = &(hea[i]->lsa);
1428
#endif
1429
  }
1430
  cli_msg(0, "");
1431
}
1432

    
1433

    
1434
static int
1435
lsa_compare_for_lsadb(const void *p1, const void *p2)
1436
{
1437
  struct top_hash_entry * he1 = * (struct top_hash_entry **) p1;
1438
  struct top_hash_entry * he2 = * (struct top_hash_entry **) p2;
1439
  struct ospf_lsa_header *lsa1 = &(he1->lsa);
1440
  struct ospf_lsa_header *lsa2 = &(he2->lsa);
1441
  int sc1 = LSA_SCOPE(lsa1);
1442
  int sc2 = LSA_SCOPE(lsa2);
1443

    
1444
  if (sc1 != sc2)
1445
    return sc2 - sc1;
1446

    
1447
  if (he1->domain != he2->domain)
1448
    return he1->domain - he2->domain;
1449

    
1450
  if (lsa1->rt != lsa2->rt)
1451
    return lsa1->rt - lsa2->rt;
1452
  
1453
  if (lsa1->id != lsa2->id)
1454
    return lsa1->id - lsa2->id;
1455

    
1456
  if (lsa1->type != lsa2->type)
1457
    return lsa1->type - lsa2->type;
1458

    
1459
  return lsa1->sn - lsa2->sn;
1460
}
1461

    
1462
void
1463
ospf_sh_lsadb(struct proto *p)
1464
{
1465
  struct proto_ospf *po = (struct proto_ospf *) p;
1466
  struct top_graph *f = po->gr;
1467
  unsigned int i, j;
1468
  int last_dscope = -1;
1469
  u32 last_domain = 0;
1470

    
1471
  if (p->proto_state != PS_UP)
1472
  {
1473
    cli_msg(-1017, "%s: is not up", p->name);
1474
    cli_msg(0, "");
1475
    return;
1476
  }
1477

    
1478
  struct top_hash_entry *hea[f->hash_entries];
1479
  struct top_hash_entry *he;
1480

    
1481
  j = 0;
1482
  WALK_SLIST(he, po->lsal)
1483
    hea[j++] = he;
1484

    
1485
  if (j != f->hash_entries)
1486
    die("Fatal mismatch");
1487

    
1488
  qsort(hea, j, sizeof(struct top_hash_entry *), lsa_compare_for_lsadb);
1489

    
1490
  for (i = 0; i < j; i++)
1491
  {
1492
    struct ospf_lsa_header *lsa = &(hea[i]->lsa);
1493
    int dscope = LSA_SCOPE(lsa);
1494
    
1495
    if ((dscope != last_dscope) || (hea[i]->domain != last_domain))
1496
    {
1497
      struct iface *ifa;
1498

    
1499
      cli_msg(-1017, "");
1500
      switch (dscope)
1501
      {
1502
        case LSA_SCOPE_AS:
1503
          cli_msg(-1017, "Global");
1504
          break;
1505
        case LSA_SCOPE_AREA:
1506
          cli_msg(-1017, "Area %R", hea[i]->domain);
1507
          break;
1508
#ifdef OSPFv3
1509
        case LSA_SCOPE_LINK:
1510
          ifa = if_find_by_index(hea[i]->domain);
1511
          cli_msg(-1017, "Link %s", (ifa != NULL) ? ifa->name : "?");
1512
          break;
1513
#endif
1514
      }
1515
      cli_msg(-1017, "");
1516
      cli_msg(-1017," Type   LS ID           Router           Age  Sequence  Checksum");
1517

    
1518
      last_dscope = dscope;
1519
      last_domain = hea[i]->domain;
1520
    }
1521

    
1522

    
1523
    cli_msg(-1017," %04x  %-15R %-15R %5u  %08x    %04x",
1524
            lsa->type, lsa->id, lsa->rt, lsa->age, lsa->sn, lsa->checksum);
1525
  }
1526
  cli_msg(0, "");
1527
}
1528

    
1529

    
1530
struct protocol proto_ospf = {
1531
  name:"OSPF",
1532
  template:"ospf%d",
1533
  attr_class:EAP_OSPF,
1534
  init:ospf_init,
1535
  dump:ospf_dump,
1536
  start:ospf_start,
1537
  shutdown:ospf_shutdown,
1538
  get_route_info:ospf_get_route_info,
1539
  get_attr:ospf_get_attr,
1540
  get_status:ospf_get_status,
1541
  reconfigure:ospf_reconfigure
1542
};