Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (39 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, struct rtable *table UNUSED, net * n, rte * new, rte * old UNUSED, ea_list * attrs);
82
static int ospf_rte_better(struct rte *new, struct rte *old);
83
static int ospf_rte_same(struct rte *new, struct rte *old);
84
static void ospf_disp(timer *timer);
85

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

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

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

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

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

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

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

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

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

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

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

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

    
214
  WALK_LIST(ifa, po->iface_list)
215
  {
216
    OSPF_TRACE(D_EVENTS, "Interface: %s", (ifa->iface ? ifa->iface->name : "(null)"));
217
    OSPF_TRACE(D_EVENTS, "state: %u", ifa->state);
218
    OSPF_TRACE(D_EVENTS, "DR:  %R", ifa->drid);
219
    OSPF_TRACE(D_EVENTS, "BDR: %R", ifa->bdrid);
220
    WALK_LIST(n, ifa->neigh_list)
221
    {
222
      OSPF_TRACE(D_EVENTS, "  neighbor %R in state %u", n->rid, n->state);
223
    }
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
  */
231
  neigh_dump_all();
232
}
233

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

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

    
250
  return p;
251
}
252

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
364
  po->calcrt = 2;
365

    
366
  return 1;
367
}
368

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

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

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

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

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

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

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

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

    
423

    
424

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

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

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

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

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

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

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

    
481
  /* And send to all my neighbors 1WAY */
482
  WALK_LIST(ifa, po->iface_list)
483
    if (ifa->state > OSPF_IS_DOWN)
484
      ospf_iface_shutdown(ifa);
485

    
486
  return PS_DOWN;
487
}
488

    
489
static void
490
ospf_rt_notify(struct proto *p, rtable *tbl UNUSED, net * n, rte * new, rte * old UNUSED, ea_list * attrs)
491
{
492
  struct proto_ospf *po = (struct proto_ospf *) p;
493

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

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

    
505
static void
506
ospf_get_status(struct proto *p, byte * buf)
507
{
508
  struct proto_ospf *po = (struct proto_ospf *) p;
509

    
510
  if (p->proto_state == PS_DOWN)
511
    buf[0] = 0;
512
  else
513
  {
514
    struct ospf_iface *ifa;
515
    struct ospf_neighbor *n;
516
    int adj = 0;
517

    
518
    WALK_LIST(ifa, po->iface_list)
519
      WALK_LIST(n, ifa->neigh_list) if (n->state == NEIGHBOR_FULL)
520
      adj = 1;
521

    
522
    if (adj == 0)
523
      strcpy(buf, "Alone");
524
    else
525
      strcpy(buf, "Running");
526
  }
527
}
528

    
529
static void
530
ospf_get_route_info(rte * rte, byte * buf, ea_list * attrs UNUSED)
531
{
532
  char *type = "<bug>";
533

    
534
  switch(rte->attrs->source)
535
  {
536
    case RTS_OSPF:
537
      type = "I";
538
      break;
539
    case RTS_OSPF_IA:
540
      type = "IA";
541
      break;
542
    case RTS_OSPF_EXT1:
543
      type = "E1";
544
      break;
545
    case RTS_OSPF_EXT2:
546
      type = "E2";
547
      break;
548
  }
549

    
550
  buf += bsprintf(buf, " %s", type);
551
  buf += bsprintf(buf, " (%d/%d", rte->pref, rte->u.ospf.metric1);
552
  if (rte->attrs->source == RTS_OSPF_EXT2)
553
    buf += bsprintf(buf, "/%d", rte->u.ospf.metric2);
554
  buf += bsprintf(buf, ")");
555
  if ((rte->attrs->source == RTS_OSPF_EXT2 || rte->attrs->source == RTS_OSPF_EXT1) && rte->u.ospf.tag)
556
  {
557
    buf += bsprintf(buf, " [%x]", rte->u.ospf.tag);
558
  }
559
  if (rte->u.ospf.router_id)
560
    buf += bsprintf(buf, " [%R]", rte->u.ospf.router_id);
561
}
562

    
563
static int
564
ospf_get_attr(eattr * a, byte * buf, int buflen UNUSED)
565
{
566
  switch (a->id)
567
  {
568
  case EA_OSPF_METRIC1:
569
    bsprintf(buf, "metric1");
570
    return GA_NAME;
571
  case EA_OSPF_METRIC2:
572
    bsprintf(buf, "metric2");
573
    return GA_NAME;
574
  case EA_OSPF_TAG:
575
    bsprintf(buf, "tag: %08x (%u)", a->u.data, a->u.data);
576
    return GA_FULL;
577
 case EA_OSPF_ROUTER_ID:
578
   bsprintf(buf, "router_id: %R (%u)", a->u.data, a->u.data);
579
    return GA_FULL;
580
  default:
581
    return GA_UNKNOWN;
582
  }
583
}
584

    
585
static int
586
ospf_patt_compare(struct ospf_iface_patt *a, struct ospf_iface_patt *b)
587
{
588
  return (a->type == b->type);
589
}
590

    
591
/**
592
 * ospf_reconfigure - reconfiguration hook
593
 * @p: current instance of protocol (with old configuration)
594
 * @c: new configuration requested by user
595
 *
596
 * This hook tries to be a little bit intelligent. Instance of OSPF
597
 * will survive change of many constants like hello interval,
598
 * password change, addition or deletion of some neighbor on
599
 * nonbroadcast network, cost of interface, etc.
600
 */
601
static int
602
ospf_reconfigure(struct proto *p, struct proto_config *c)
603
{
604
  struct ospf_config *old = (struct ospf_config *) (p->cf);
605
  struct ospf_config *new = (struct ospf_config *) c;
606
  struct ospf_area_config *oldac, *newac;
607
  struct proto_ospf *po = (struct proto_ospf *) p;
608
  struct ospf_iface_patt *oldip, *newip;
609
  struct ospf_iface *ifa;
610
  struct nbma_node *nb1, *nb2, *nbnx;
611
  struct ospf_area *oa = NULL;
612
  int found, olddead, newdead;
613
  struct area_net_config *anc;
614
  struct area_net *an;
615

    
616
  if (po->rfc1583 != new->rfc1583)
617
    return 0;
618

    
619
  schedule_rtcalc(po);
620

    
621
  po->tick = new->tick;
622
  po->disp_timer->recurrent = po->tick;
623
  tm_start(po->disp_timer, 1);
624

    
625
  oldac = HEAD(old->area_list);
626
  newac = HEAD(new->area_list);
627

    
628
  /* I should get it in the same order */
629

    
630
  while (((NODE(oldac))->next != NULL) && ((NODE(newac))->next != NULL))
631
  {
632
    if (oldac->areaid != newac->areaid)
633
      return 0;
634

    
635
    WALK_LIST(oa, po->area_list)
636
      if (oa->areaid == newac->areaid)
637
        break;
638

    
639
    if (!oa)
640
      return 0;
641

    
642
    oa->ac = newac;
643
    oa->stub = newac->stub;
644
    if (newac->stub && (oa->areaid == 0)) oa->stub = 0;
645

    
646
    /* Check stubnet_list */
647
    struct ospf_stubnet_config *oldsn = HEAD(oldac->stubnet_list);
648
    struct ospf_stubnet_config *newsn = HEAD(newac->stubnet_list);
649

    
650
    while (((NODE(oldsn))->next != NULL) && ((NODE(newsn))->next != NULL))
651
      {
652
        if (!ipa_equal(oldsn->px.addr, newsn->px.addr) ||
653
            (oldsn->px.len != newsn->px.len) ||
654
            (oldsn->hidden != newsn->hidden) ||
655
            (oldsn->summary != newsn->summary) ||
656
            (oldsn->cost != newsn->cost))
657
          break;
658

    
659
        oldsn = (struct ospf_stubnet_config *)(NODE(oldsn))->next;
660
        newsn = (struct ospf_stubnet_config *)(NODE(newsn))->next;
661
      }
662

    
663
    /* If there is no change, both pointers should be NULL */
664
    if (((NODE(oldsn))->next) != ((NODE(newsn))->next))
665
      schedule_rt_lsa(oa);
666

    
667
    /* Change net_list */
668
    FIB_WALK(&oa->net_fib, nf)        /* First check if some networks are deleted */
669
    {
670
      found = 0;
671
      WALK_LIST(anc, newac->net_list)
672
      {
673
        if (ipa_equal(anc->px.addr, nf->prefix) && (anc->px.len == nf->pxlen))
674
        {
675
          found = 1;
676
          break;
677
        }
678
        if (!found) flush_sum_lsa(oa, nf, ORT_NET);        /* And flush them */
679
      }
680
    }
681
    FIB_WALK_END;
682

    
683
    WALK_LIST(anc, newac->net_list)        /* Second add new networks */
684
    {
685
      an = fib_get(&oa->net_fib, &anc->px.addr, anc->px.len);
686
      an->hidden = anc->hidden;
687
    }
688

    
689
    if (!iface_patts_equal(&oldac->patt_list, &newac->patt_list,
690
                           (void *) ospf_patt_compare))
691
      return 0;
692

    
693
    WALK_LIST(ifa, po->iface_list)
694
    {
695
      /* FIXME: better handling of vlinks */
696
      if (ifa->iface == NULL)
697
        continue;
698

    
699
      /* FIXME: better matching of interface_id in OSPFv3 */
700
      if (oldip = (struct ospf_iface_patt *)
701
          iface_patt_find(&oldac->patt_list, ifa->iface, ifa->addr))
702
      {
703
        /* Now reconfigure interface */
704
        if (!(newip = (struct ospf_iface_patt *)
705
              iface_patt_find(&newac->patt_list, ifa->iface, ifa->addr)))
706
          return 0;
707

    
708
        /* HELLO TIMER */
709
        if (oldip->helloint != newip->helloint)
710
        {
711
          ifa->helloint = newip->helloint;
712
          ifa->hello_timer->recurrent = ifa->helloint;
713
          tm_start(ifa->hello_timer, ifa->helloint);
714
          OSPF_TRACE(D_EVENTS,
715
                     "Changing hello interval on interface %s from %d to %d",
716
                     ifa->iface->name, oldip->helloint, newip->helloint);
717
        }
718

    
719
        /* POLL TIMER */
720
        if (oldip->pollint != newip->pollint)
721
        {
722
          ifa->pollint = newip->helloint;
723
          ifa->poll_timer->recurrent = ifa->pollint;
724
          tm_start(ifa->poll_timer, ifa->pollint);
725
          OSPF_TRACE(D_EVENTS,
726
                     "Changing poll interval on interface %s from %d to %d",
727
                     ifa->iface->name, oldip->pollint, newip->pollint);
728
        }
729

    
730
        /* COST */
731
        if (oldip->cost != newip->cost)
732
        {
733
          ifa->cost = newip->cost;
734
          OSPF_TRACE(D_EVENTS,
735
                     "Changing cost interface %s from %d to %d",
736
                     ifa->iface->name, oldip->cost, newip->cost);
737
          schedule_rt_lsa(ifa->oa);
738
        }
739

    
740
        /* RX BUFF */
741
        if (oldip->rxbuf != newip->rxbuf)
742
        {
743
          ifa->rxbuf = newip->rxbuf;
744
          OSPF_TRACE(D_EVENTS,
745
                     "Changing rxbuf interface %s from %d to %d",
746
                     ifa->iface->name, oldip->rxbuf, newip->rxbuf);
747
          ospf_iface_change_mtu(po, ifa);
748
        }
749

    
750
        /* strict nbma */
751
        if ((oldip->strictnbma == 0) && (newip->strictnbma != 0))
752
        {
753
          ifa->strictnbma = newip->strictnbma;
754
          OSPF_TRACE(D_EVENTS,
755
                     "Interface %s is now strict NBMA.", ifa->iface->name);
756
        }
757
        if ((oldip->strictnbma != 0) && (newip->strictnbma == 0))
758
        {
759
          ifa->strictnbma = newip->strictnbma;
760
          OSPF_TRACE(D_EVENTS,
761
                     "Interface %s is no longer strict NBMA.",
762
                     ifa->iface->name);
763
        }
764

    
765
        /* stub */
766
        int old_stub = ospf_iface_stubby(oldip, ifa->addr);
767
        int new_stub = ospf_iface_stubby(newip, ifa->addr);
768
        if (!old_stub && new_stub)
769
        {
770
          ifa->stub = 1;
771
          OSPF_TRACE(D_EVENTS, "Interface %s is now stub.", ifa->iface->name);
772
        }
773
        if (old_stub && !new_stub && (ifa->ioprob == OSPF_I_OK))
774
        {
775
          ifa->stub = 0;
776
          OSPF_TRACE(D_EVENTS, "Interface %s is no longer stub.", ifa->iface->name);
777
        }
778

    
779
#ifdef OSPFv2        
780
        /* AUTHENTICATION */
781
        if (oldip->autype != newip->autype)
782
        {
783
          ifa->autype = newip->autype;
784
          OSPF_TRACE(D_EVENTS,
785
                     "Changing authentication type on interface %s",
786
                     ifa->iface->name);
787
        }
788
        /* Add *passwords */
789
        ifa->passwords = newip->passwords;
790
#endif
791

    
792
        /* priority */
793
        if (oldip->priority != newip->priority)
794
        {
795
          ifa->priority = newip->priority;
796
          OSPF_TRACE(D_EVENTS,
797
                     "Changing priority on interface %s from %d to %d",
798
                     ifa->iface->name, oldip->priority, newip->priority);
799
        }
800

    
801
        /* RXMT */
802
        if (oldip->rxmtint != newip->rxmtint)
803
        {
804
          ifa->rxmtint = newip->rxmtint;
805
          OSPF_TRACE(D_EVENTS,
806
                     "Changing retransmit interval on interface %s from %d to %d",
807
                     ifa->iface->name, oldip->rxmtint, newip->rxmtint);
808
        }
809

    
810
        /* WAIT */
811
        if (oldip->waitint != newip->waitint)
812
        {
813
          ifa->waitint = newip->waitint;
814
          if (ifa->wait_timer->expires != 0)
815
            tm_start(ifa->wait_timer, ifa->waitint);
816
          OSPF_TRACE(D_EVENTS,
817
                     "Changing wait interval on interface %s from %d to %d",
818
                     ifa->iface->name, oldip->waitint, newip->waitint);
819
        }
820

    
821
        /* INFTRANS */
822
        if (oldip->inftransdelay != newip->inftransdelay)
823
        {
824
          ifa->inftransdelay = newip->inftransdelay;
825
          OSPF_TRACE(D_EVENTS,
826
                     "Changing transmit delay on interface %s from %d to %d",
827
                     ifa->iface->name, oldip->inftransdelay,
828
                     newip->inftransdelay);
829
        }
830

    
831
        /* DEAD */
832
        olddead = (oldip->dead == 0) ? oldip->deadc * oldip->helloint : oldip->dead;
833
        newdead = (newip->dead == 0) ? newip->deadc * newip->helloint : newip->dead;
834
        if (olddead != newdead)
835
        {
836
          ifa->dead = newdead;
837
          OSPF_TRACE(D_EVENTS,
838
                     "Changing dead interval on interface %s from %d to %d",
839
                     ifa->iface->name, olddead, newdead);
840
        }
841

    
842
        /* NBMA LIST */
843
        /* First remove old */
844
        WALK_LIST_DELSAFE(nb1, nbnx, ifa->nbma_list)
845
        {
846
          found = 0;
847
          WALK_LIST(nb2, newip->nbma_list)
848
            if (ipa_compare(nb1->ip, nb2->ip) == 0)
849
          {
850
            found = 1;
851
            if (nb1->eligible != nb2->eligible)
852
              OSPF_TRACE(D_EVENTS,
853
                         "Changing neighbor eligibility %I on interface %s",
854
                         nb1->ip, ifa->iface->name);
855
            break;
856
          }
857

    
858
          if (!found)
859
          {
860
            OSPF_TRACE(D_EVENTS,
861
                       "Removing NBMA neighbor %I on interface %s",
862
                       nb1->ip, ifa->iface->name);
863
            rem_node(NODE nb1);
864
            mb_free(nb1);
865
          }
866
        }
867
        /* And then add new */
868
        WALK_LIST(nb2, newip->nbma_list)
869
        {
870
          found = 0;
871
          WALK_LIST(nb1, ifa->nbma_list)
872
            if (ipa_compare(nb1->ip, nb2->ip) == 0)
873
          {
874
            found = 1;
875
            break;
876
          }
877
          if (!found)
878
          {
879
            nb1 = mb_alloc(p->pool, sizeof(struct nbma_node));
880
            nb1->ip = nb2->ip;
881
            nb1->eligible = nb2->eligible;
882
            add_tail(&ifa->nbma_list, NODE nb1);
883
            OSPF_TRACE(D_EVENTS,
884
                       "Adding NBMA neighbor %I on interface %s",
885
                       nb1->ip, ifa->iface->name);
886
          }
887
        }
888
      }
889
    }
890

    
891
    oldac = (struct ospf_area_config *)(NODE(oldac))->next;
892
    newac = (struct ospf_area_config *)(NODE(newac))->next;
893
  }
894

    
895
  if (((NODE(oldac))->next) != ((NODE(newac))->next))
896
    return 0;                        /* One is not null */
897

    
898
  return 1;                        /* Everything OK :-) */
899
}
900

    
901
void
902
ospf_sh_neigh(struct proto *p, char *iff)
903
{
904
  struct ospf_iface *ifa = NULL;
905
  struct ospf_neighbor *n;
906
  struct proto_ospf *po = (struct proto_ospf *) p;
907

    
908
  if (p->proto_state != PS_UP)
909
  {
910
    cli_msg(-1013, "%s: is not up", p->name);
911
    cli_msg(0, "");
912
    return;
913
  }
914

    
915
  cli_msg(-1013, "%s:", p->name);
916
  cli_msg(-1013, "%-12s\t%3s\t%-15s\t%-5s\t%-10s %-12s", "Router ID", "Pri",
917
          "     State", "DTime", "Interface", "Router IP");
918
  WALK_LIST(ifa, po->iface_list)
919
    if ((iff == NULL) || patmatch(iff, ifa->iface->name))
920
      WALK_LIST(n, ifa->neigh_list)
921
        ospf_sh_neigh_info(n);
922
  cli_msg(0, "");
923
}
924

    
925
void
926
ospf_sh(struct proto *p)
927
{
928
  struct ospf_area *oa;
929
  struct proto_ospf *po = (struct proto_ospf *) p;
930
  struct ospf_iface *ifa;
931
  struct ospf_neighbor *n;
932
  int ifano, nno, adjno, firstfib;
933
  struct area_net *anet;
934

    
935
  if (p->proto_state != PS_UP)
936
  {
937
    cli_msg(-1014, "%s: is not up", p->name);
938
    cli_msg(0, "");
939
    return;
940
  }
941

    
942
  cli_msg(-1014, "%s:", p->name);
943
  cli_msg(-1014, "RFC1583 compatibility: %s", (po->rfc1583 ? "enable" : "disabled"));
944
  cli_msg(-1014, "RT scheduler tick: %d", po->tick);
945
  cli_msg(-1014, "Number of areas: %u", po->areano);
946
  cli_msg(-1014, "Number of LSAs in DB:\t%u", po->gr->hash_entries);
947

    
948
  WALK_LIST(oa, po->area_list)
949
  {
950
    cli_msg(-1014, "\tArea: %R (%u) %s", oa->areaid, oa->areaid,
951
            oa->areaid == 0 ? "[BACKBONE]" : "");
952
    ifano = 0;
953
    nno = 0;
954
    adjno = 0;
955
    WALK_LIST(ifa, po->iface_list)
956
    {
957
      if (oa == ifa->oa)
958
      {
959
        ifano++;
960
        WALK_LIST(n, ifa->neigh_list)
961
        {
962
          nno++;
963
          if (n->state == NEIGHBOR_FULL)
964
            adjno++;
965
        }
966
      }
967
    }
968
    cli_msg(-1014, "\t\tStub:\t%s", oa->stub ? "Yes" : "No");
969
    cli_msg(-1014, "\t\tTransit:\t%s", oa->trcap ? "Yes" : "No");
970
    cli_msg(-1014, "\t\tNumber of interfaces:\t%u", ifano);
971
    cli_msg(-1014, "\t\tNumber of neighbors:\t%u", nno);
972
    cli_msg(-1014, "\t\tNumber of adjacent neighbors:\t%u", adjno);
973

    
974
    firstfib = 1;
975
    FIB_WALK(&oa->net_fib, nftmp)
976
    {
977
      anet = (struct area_net *) nftmp;
978
      if(firstfib)
979
      {
980
        cli_msg(-1014, "\t\tArea networks:");
981
        firstfib = 0;
982
      }
983
      cli_msg(-1014, "\t\t\t%1I/%u\t%s\t%s", anet->fn.prefix, anet->fn.pxlen,
984
                anet->hidden ? "Hidden" : "Advertise", anet->active ? "Active" : "");
985
    }
986
    FIB_WALK_END;
987
  }
988
  cli_msg(0, "");
989
}
990

    
991
void
992
ospf_sh_iface(struct proto *p, char *iff)
993
{
994
  struct proto_ospf *po = (struct proto_ospf *) p;
995
  struct ospf_iface *ifa = NULL;
996

    
997
  if (p->proto_state != PS_UP)
998
  {
999
    cli_msg(-1015, "%s: is not up", p->name);
1000
    cli_msg(0, "");
1001
    return;
1002
  }
1003

    
1004
  cli_msg(-1015, "%s:", p->name);
1005
  WALK_LIST(ifa, po->iface_list)
1006
    if ((iff == NULL) || patmatch(iff, ifa->iface->name))
1007
      ospf_iface_info(ifa);
1008
  cli_msg(0, "");
1009
}
1010

    
1011
/* lsa_compare_for_state() - Compare function for 'show ospf state'
1012
 *
1013
 * First we want to separate network-LSAs and other LSAs (because network-LSAs
1014
 * will be presented as network nodes and other LSAs together as router nodes)
1015
 * Network-LSAs are sorted according to network prefix, other LSAs are sorted
1016
 * according to originating router id (to get all LSA needed to represent one
1017
 * router node together). Then, according to LSA type, ID and age.
1018
 *
1019
 * For OSPFv3, we have to handle also Prefix-LSAs. We would like to put each
1020
 * immediately after the referenced LSA. We will make faked LSA based on ref_
1021
 * values
1022
 */
1023

    
1024
#ifdef OSPFv3
1025

    
1026
static struct ospf_lsa_header *
1027
fake_lsa_from_prefix_lsa(struct ospf_lsa_header *dst, struct ospf_lsa_header *src,
1028
                         struct ospf_lsa_prefix *px)
1029
{
1030
  dst->age = src->age;
1031
  dst->type = px->ref_type;
1032
  dst->id = px->ref_id;
1033
  dst->rt = px->ref_rt;
1034
  dst->sn = src->sn;
1035

    
1036
  return dst;
1037
}
1038

    
1039
#endif
1040

    
1041
static int
1042
lsa_compare_for_state(const void *p1, const void *p2)
1043
{
1044
  struct top_hash_entry * he1 = * (struct top_hash_entry **) p1;
1045
  struct top_hash_entry * he2 = * (struct top_hash_entry **) p2;
1046
  struct ospf_lsa_header *lsa1 = &(he1->lsa);
1047
  struct ospf_lsa_header *lsa2 = &(he2->lsa);
1048

    
1049
  if (he1->domain != he2->domain)
1050
    return he1->domain - he2->domain;
1051

    
1052
#ifdef OSPFv3
1053
  struct ospf_lsa_header lsatmp1, lsatmp2;
1054

    
1055
  int px1 = (lsa1->type == LSA_T_PREFIX);
1056
  int px2 = (lsa2->type == LSA_T_PREFIX);
1057

    
1058
  if (px1)
1059
    lsa1 = fake_lsa_from_prefix_lsa(&lsatmp1, lsa1, he1->lsa_body);
1060

    
1061
  if (px2)
1062
    lsa2 = fake_lsa_from_prefix_lsa(&lsatmp2, lsa2, he2->lsa_body);
1063
#endif
1064

    
1065
  int nt1 = (lsa1->type == LSA_T_NET);
1066
  int nt2 = (lsa2->type == LSA_T_NET);
1067

    
1068
  if (nt1 != nt2)
1069
    return nt1 - nt2;
1070

    
1071
  if (nt1)
1072
  {
1073
#ifdef OSPFv3
1074
    /* In OSPFv3, neworks are named base on ID of DR */
1075
    if (lsa1->rt != lsa2->rt)
1076
      return lsa1->rt - lsa2->rt;
1077
#endif
1078

    
1079
    /* For OSPFv2, this is IP of the network,
1080
       for OSPFv3, this is interface ID */
1081
    if (lsa1->id != lsa2->id)
1082
      return lsa1->id - lsa2->id;
1083

    
1084
#ifdef OSPFv3
1085
    if (px1 != px2)
1086
      return px1 - px2;
1087
#endif
1088

    
1089
    return lsa1->sn - lsa2->sn;
1090
  }
1091
  else 
1092
  {
1093
    if (lsa1->rt != lsa2->rt)
1094
      return lsa1->rt - lsa2->rt;
1095

    
1096
    if (lsa1->type != lsa2->type)
1097
      return lsa1->type - lsa2->type;
1098
  
1099
    if (lsa1->id != lsa2->id)
1100
      return lsa1->id - lsa2->id;
1101

    
1102
#ifdef OSPFv3
1103
    if (px1 != px2)
1104
      return px1 - px2;
1105
#endif
1106
  
1107
    return lsa1->sn - lsa2->sn;
1108
  }
1109
}
1110

    
1111
static inline void
1112
show_lsa_router(struct proto_ospf *po, struct top_hash_entry *he)
1113
{
1114
  struct ospf_lsa_header *lsa = &(he->lsa);
1115
  struct ospf_lsa_rt *rt = he->lsa_body;
1116
  struct ospf_lsa_rt_link *rr = (struct ospf_lsa_rt_link *) (rt + 1);
1117
  int max = lsa_rt_count(lsa);
1118
  int i;
1119

    
1120
  for (i = 0; i < max; i++)
1121
    if (rr[i].type == LSART_VLNK)
1122
      cli_msg(-1016, "\t\tvlink %R metric %u", rr[i].id, rr[i].metric);
1123

    
1124
  for (i = 0; i < max; i++)
1125
    if (rr[i].type == LSART_PTP)
1126
      cli_msg(-1016, "\t\trouter %R metric %u", rr[i].id, rr[i].metric);
1127

    
1128
  for (i = 0; i < max; i++)
1129
    if (rr[i].type == LSART_NET)
1130
    {
1131
#ifdef OSPFv2
1132
      struct top_hash_entry *net_he = ospf_hash_find_net(po->gr, he->domain, rr[i].id);
1133

    
1134
      if (net_he)
1135
      {
1136
        struct ospf_lsa_header *net_lsa = &(net_he->lsa);
1137
        struct ospf_lsa_net *net_ln = net_he->lsa_body;
1138

    
1139
        cli_msg(-1016, "\t\tnetwork %I/%d metric %u", 
1140
                ipa_and(ipa_from_u32(net_lsa->id), net_ln->netmask),
1141
                ipa_mklen(net_ln->netmask), rr[i].metric);
1142
      }
1143
      else
1144
        cli_msg(-1016, "\t\tnetwork [%R] metric %u", rr[i].id, rr[i].metric);
1145

    
1146
#else /* OSPFv3 */
1147
      cli_msg(-1016, "\t\tnetwork [%R-%u] metric %u", rr[i].id, rr[i].nif, rr[i].metric);
1148
#endif
1149
    }
1150

    
1151
#ifdef OSPFv2
1152
  for (i = 0; i < max; i++)
1153
    if (rr[i].type == LSART_STUB)
1154
      cli_msg(-1016, "\t\tstubnet %I/%d metric %u", ipa_from_u32(rr[i].id),
1155
              ipa_mklen(ipa_from_u32(rr[i].data)), rr[i].metric);
1156
#endif
1157
}
1158

    
1159
static inline void
1160
show_lsa_network(struct top_hash_entry *he)
1161
{
1162
  struct ospf_lsa_header *lsa = &(he->lsa);
1163
  struct ospf_lsa_net *ln = he->lsa_body;
1164
  u32 i;
1165

    
1166
#ifdef OSPFv2
1167
  cli_msg(-1016, "");
1168
  cli_msg(-1016, "\tnetwork %I/%d", ipa_and(ipa_from_u32(lsa->id), ln->netmask), ipa_mklen(ln->netmask));
1169
  cli_msg(-1016, "\t\tdr %R", lsa->rt);
1170
#else /* OSPFv3 */
1171
  cli_msg(-1016, "");
1172
  cli_msg(-1016, "\tnetwork [%R-%u]", lsa->rt, lsa->id);
1173
#endif
1174

    
1175
  for (i = 0; i < lsa_net_count(lsa); i++)
1176
    cli_msg(-1016, "\t\trouter %R", ln->routers[i]);
1177
}
1178

    
1179
static inline void
1180
show_lsa_sum_net(struct top_hash_entry *he)
1181
{
1182
  ip_addr ip;
1183
  int pxlen;
1184

    
1185
#ifdef OSPFv2
1186
  struct ospf_lsa_sum *ls = he->lsa_body;
1187
  pxlen = ipa_mklen(ls->netmask);
1188
  ip = ipa_and(ipa_from_u32(he->lsa.id), ls->netmask);
1189
#else /* OSPFv3 */
1190
  u8 pxopts;
1191
  u16 rest;
1192
  struct ospf_lsa_sum_net *ls = he->lsa_body;
1193
  lsa_get_ipv6_prefix(ls->prefix, &ip, &pxlen, &pxopts, &rest);
1194
#endif
1195

    
1196
  cli_msg(-1016, "\t\txnetwork %I/%d metric %u", ip, pxlen, ls->metric);
1197
}
1198

    
1199
static inline void
1200
show_lsa_sum_rt(struct top_hash_entry *he)
1201
{
1202
  u32 dst_rid, options;
1203

    
1204
#ifdef OSPFv2
1205
  struct ospf_lsa_sum *ls = he->lsa_body;
1206
  dst_rid = he->lsa.id;
1207
  options = 0;
1208
#else /* OSPFv3 */
1209
  struct ospf_lsa_sum_rt *ls = he->lsa_body;
1210
  dst_rid = ls->drid; 
1211
  options = ls->options & OPTIONS_MASK;
1212
#endif
1213

    
1214
  cli_msg(-1016, "\t\txrouter %R metric %u", dst_rid, ls->metric);
1215
}
1216

    
1217

    
1218
static inline void
1219
show_lsa_external(struct top_hash_entry *he)
1220
{
1221
  struct ospf_lsa_ext *ext = he->lsa_body;
1222
  char str_via[STD_ADDRESS_P_LENGTH + 8] = "";
1223
  char str_tag[16] = "";
1224
  ip_addr ip, rt_fwaddr;
1225
  int pxlen, ebit, rt_fwaddr_valid;
1226
  u32 rt_tag, rt_metric;
1227

    
1228
  rt_metric = ext->metric & METRIC_MASK;
1229
  ebit = ext->metric & LSA_EXT_EBIT;
1230
#ifdef OSPFv2
1231
  ip = ipa_and(ipa_from_u32(he->lsa.id), ext->netmask);
1232
  pxlen = ipa_mklen(ext->netmask);
1233
  rt_fwaddr = ext->fwaddr;
1234
  rt_fwaddr_valid = !ipa_equal(rt_fwaddr, IPA_NONE);
1235
  rt_tag = ext->tag;
1236
#else /* OSPFv3 */
1237
  u8 pxopts;
1238
  u16 rest;
1239
  u32 *buf = ext->rest;
1240
  buf = lsa_get_ipv6_prefix(buf, &ip, &pxlen, &pxopts, &rest);
1241

    
1242
  rt_fwaddr_valid = ext->metric & LSA_EXT_FBIT;
1243
  if (rt_fwaddr_valid)
1244
    buf = lsa_get_ipv6_addr(buf, &rt_fwaddr);
1245
  else 
1246
    rt_fwaddr = IPA_NONE;
1247

    
1248
  if (ext->metric & LSA_EXT_TBIT)
1249
    rt_tag = *buf++;
1250
  else
1251
    rt_tag = 0;
1252
#endif
1253
  
1254
  if (rt_fwaddr_valid)
1255
    bsprintf(str_via, " via %I", rt_fwaddr);
1256

    
1257
  if (rt_tag)
1258
    bsprintf(str_tag, " tag %08x", rt_tag);
1259

    
1260
  cli_msg(-1016, "\t\texternal %I/%d metric%s %u%s%s", ip, pxlen,
1261
          ebit ? "2" : "", rt_metric, str_via, str_tag);
1262
}
1263

    
1264
#ifdef OSPFv3
1265
static inline void
1266
show_lsa_prefix(struct top_hash_entry *he, struct ospf_lsa_header *olsa)
1267
{
1268
  struct ospf_lsa_prefix *px = he->lsa_body;
1269
  ip_addr pxa;
1270
  int pxlen;
1271
  u8 pxopts;
1272
  u16 metric;
1273
  u32 *buf;
1274
  int i;
1275

    
1276
  /* We check whether given prefix-LSA is related to the last non-prefix-LSA */
1277
  if ((olsa == NULL) || (olsa->type != px->ref_type) || (olsa->rt != px->ref_rt) ||
1278
      !(((px->ref_type == LSA_T_RT)  && (px->ref_id == 0)) ||
1279
        ((px->ref_type == LSA_T_NET) && (px->ref_id == olsa->id))))
1280
    return;
1281

    
1282
  buf = px->rest;
1283
  for (i = 0; i < px->pxcount; i++)
1284
    {
1285
      buf = lsa_get_ipv6_prefix(buf, &pxa, &pxlen, &pxopts, &metric);
1286

    
1287
      if (px->ref_type == LSA_T_RT)
1288
        cli_msg(-1016, "\t\tstubnet %I/%d metric %u", pxa, pxlen, metric);
1289
      else
1290
        cli_msg(-1016, "\t\taddress %I/%d", pxa, pxlen);
1291
    }
1292
}
1293
#endif
1294

    
1295
void
1296
ospf_sh_state(struct proto *p, int verbose)
1297
{
1298
  struct proto_ospf *po = (struct proto_ospf *) p;
1299
  struct top_graph *f = po->gr;
1300
  unsigned int i, j1, j2;
1301
  u32 last_rt = 0xFFFFFFFF;
1302
  u32 last_area = 0xFFFFFFFF;
1303

    
1304
#ifdef OSPFv3
1305
  struct ospf_lsa_header *olsa = NULL;
1306
#endif
1307

    
1308
  if (p->proto_state != PS_UP)
1309
  {
1310
    cli_msg(-1016, "%s: is not up", p->name);
1311
    cli_msg(0, "");
1312
    return;
1313
  }
1314

    
1315
  struct top_hash_entry *hea[f->hash_entries];
1316
  struct top_hash_entry *he;
1317

    
1318
  j1 = j2 = 0;
1319
  WALK_SLIST(he, po->lsal)
1320
  {
1321
    int accept;
1322

    
1323
    switch (he->lsa.type)
1324
      {
1325
      case LSA_T_RT:
1326
      case LSA_T_NET:
1327
        accept = 1;
1328
        break;
1329

    
1330
      case LSA_T_SUM_NET:
1331
      case LSA_T_SUM_RT:
1332
      case LSA_T_EXT:
1333
#ifdef OSPFv3
1334
      case LSA_T_PREFIX:
1335
#endif
1336
        accept = verbose;
1337
        break;
1338

    
1339
      default:
1340
        accept = 0;
1341
      }
1342

    
1343
    if (accept)
1344
      hea[j1++] = he;
1345
    else
1346
      j2++;
1347
  }
1348

    
1349
  if ((j1 + j2) != f->hash_entries)
1350
    die("Fatal mismatch");
1351

    
1352
  qsort(hea, j1, sizeof(struct top_hash_entry *), lsa_compare_for_state);
1353

    
1354
  for (i = 0; i < j1; i++)
1355
  {
1356
    if (last_area != hea[i]->domain)
1357
    {
1358
      cli_msg(-1016, "");
1359
      cli_msg(-1016, "area %R", hea[i]->domain);
1360
      last_area = hea[i]->domain;
1361
      last_rt = 0xFFFFFFFF;
1362
    }
1363

    
1364
    if ((hea[i]->lsa.rt != last_rt) && (hea[i]->lsa.type != LSA_T_NET)
1365
#ifdef OSPFv3
1366
        && (hea[i]->lsa.type != LSA_T_PREFIX)
1367
#endif
1368
        )
1369
    {
1370
      cli_msg(-1016, "");
1371
      cli_msg(-1016, (hea[i]->lsa.type != LSA_T_EXT) ? "\trouter %R" : "\txrouter %R", hea[i]->lsa.rt);
1372
      last_rt = hea[i]->lsa.rt;
1373
    }
1374

    
1375
    switch (hea[i]->lsa.type)
1376
    {
1377
      case LSA_T_RT:
1378
        show_lsa_router(po, hea[i]);
1379
        break;
1380

    
1381
      case LSA_T_NET:
1382
        show_lsa_network(hea[i]);
1383
        break;
1384

    
1385
      case LSA_T_SUM_NET:
1386
        show_lsa_sum_net(hea[i]);
1387
        break;
1388

    
1389
      case LSA_T_SUM_RT:
1390
        show_lsa_sum_rt(hea[i]);
1391
        break;
1392

    
1393
      case LSA_T_EXT:
1394
        show_lsa_external(hea[i]);
1395
        break;
1396

    
1397
#ifdef OSPFv3
1398
      case LSA_T_PREFIX:
1399
        show_lsa_prefix(hea[i], olsa);
1400
        break;
1401
#endif
1402
    }
1403

    
1404
#ifdef OSPFv3
1405
    if (hea[i]->lsa.type != LSA_T_PREFIX)
1406
      olsa = &(hea[i]->lsa);
1407
#endif
1408
  }
1409
  cli_msg(0, "");
1410
}
1411

    
1412

    
1413
static int
1414
lsa_compare_for_lsadb(const void *p1, const void *p2)
1415
{
1416
  struct top_hash_entry * he1 = * (struct top_hash_entry **) p1;
1417
  struct top_hash_entry * he2 = * (struct top_hash_entry **) p2;
1418
  struct ospf_lsa_header *lsa1 = &(he1->lsa);
1419
  struct ospf_lsa_header *lsa2 = &(he2->lsa);
1420
  int sc1 = LSA_SCOPE(lsa1);
1421
  int sc2 = LSA_SCOPE(lsa2);
1422

    
1423
  if (sc1 != sc2)
1424
    return sc2 - sc1;
1425

    
1426
  if (he1->domain != he2->domain)
1427
    return he1->domain - he2->domain;
1428

    
1429
  if (lsa1->rt != lsa2->rt)
1430
    return lsa1->rt - lsa2->rt;
1431
  
1432
  if (lsa1->id != lsa2->id)
1433
    return lsa1->id - lsa2->id;
1434

    
1435
  if (lsa1->type != lsa2->type)
1436
    return lsa1->type - lsa2->type;
1437

    
1438
  return lsa1->sn - lsa2->sn;
1439
}
1440

    
1441
void
1442
ospf_sh_lsadb(struct proto *p)
1443
{
1444
  struct proto_ospf *po = (struct proto_ospf *) p;
1445
  struct top_graph *f = po->gr;
1446
  unsigned int i, j;
1447
  int last_dscope = -1;
1448
  u32 last_domain = 0;
1449

    
1450
  if (p->proto_state != PS_UP)
1451
  {
1452
    cli_msg(-1017, "%s: is not up", p->name);
1453
    cli_msg(0, "");
1454
    return;
1455
  }
1456

    
1457
  struct top_hash_entry *hea[f->hash_entries];
1458
  struct top_hash_entry *he;
1459

    
1460
  j = 0;
1461
  WALK_SLIST(he, po->lsal)
1462
    hea[j++] = he;
1463

    
1464
  if (j != f->hash_entries)
1465
    die("Fatal mismatch");
1466

    
1467
  qsort(hea, j, sizeof(struct top_hash_entry *), lsa_compare_for_lsadb);
1468

    
1469
  for (i = 0; i < j; i++)
1470
  {
1471
    struct ospf_lsa_header *lsa = &(hea[i]->lsa);
1472
    int dscope = LSA_SCOPE(lsa);
1473
    
1474
    if ((dscope != last_dscope) || (hea[i]->domain != last_domain))
1475
    {
1476
      cli_msg(-1017, "");
1477
      switch (dscope)
1478
      {
1479
        case LSA_SCOPE_AS:
1480
          cli_msg(-1017, "Global");
1481
          break;
1482
        case LSA_SCOPE_AREA:
1483
          cli_msg(-1017, "Area %R", hea[i]->domain);
1484
          break;
1485
#ifdef OSPFv3
1486
        case LSA_SCOPE_LINK:
1487
          {
1488
            struct iface *ifa = if_find_by_index(hea[i]->domain);
1489
            cli_msg(-1017, "Link %s", (ifa != NULL) ? ifa->name : "?");
1490
          }
1491
          break;
1492
#endif
1493
      }
1494
      cli_msg(-1017, "");
1495
      cli_msg(-1017," Type   LS ID           Router           Age  Sequence  Checksum");
1496

    
1497
      last_dscope = dscope;
1498
      last_domain = hea[i]->domain;
1499
    }
1500

    
1501

    
1502
    cli_msg(-1017," %04x  %-15R %-15R %5u  %08x    %04x",
1503
            lsa->type, lsa->id, lsa->rt, lsa->age, lsa->sn, lsa->checksum);
1504
  }
1505
  cli_msg(0, "");
1506
}
1507

    
1508

    
1509
struct protocol proto_ospf = {
1510
  name:"OSPF",
1511
  template:"ospf%d",
1512
  attr_class:EAP_OSPF,
1513
  init:ospf_init,
1514
  dump:ospf_dump,
1515
  start:ospf_start,
1516
  shutdown:ospf_shutdown,
1517
  get_route_info:ospf_get_route_info,
1518
  get_attr:ospf_get_attr,
1519
  get_status:ospf_get_status,
1520
  reconfigure:ospf_reconfigure
1521
};