Statistics
| Branch: | Revision:

iof-bird-daemon / proto / ospf / ospf.c @ 6f8bbaa1

History | View | Annotate | Download (38.3 KB)

1
/*
2
 *        BIRD -- OSPF
3
 *
4
 *        (c) 1999--2004 Ondrej Filip <feela@network.cz>
5
 *        (c) 2009--2014 Ondrej Zajicek <santiago@crfreenet.org>
6
 *        (c) 2009--2014 CZ.NIC z.s.p.o.
7
 *
8
 *        Can be freely distributed and used under the terms of the GNU GPL.
9
 */
10

    
11
/**
12
 * DOC: Open Shortest Path First (OSPF)
13
 *
14
 * The OSPF protocol is quite complicated and its complex implemenation is split
15
 * to many files. In |ospf.c|, you will find mainly the interface for
16
 * communication with the core (e.g., reconfiguration hooks, shutdown and
17
 * initialisation and so on). File |iface.c| contains the interface state
18
 * machine and functions for allocation and deallocation of OSPF's interface
19
 * data structures. Source |neighbor.c| includes the neighbor state machine and
20
 * functions for election of Designated Router and Backup Designated router. In
21
 * |packet.c|, you will find various functions for sending and receiving generic
22
 * OSPF packets. There are also routines for authentication and checksumming.
23
 * In |hello.c|, there are routines for sending and receiving of hello packets
24
 * as well as functions for maintaining wait times and the inactivity timer.
25
 * Files |lsreq.c|, |lsack.c|, |dbdes.c| contain functions for sending and
26
 * receiving of link-state requests, link-state acknowledgements and database
27
 * descriptions respectively.  In |lsupd.c|, there are functions for sending and
28
 * receiving of link-state updates and also the flooding algorithm. Source
29
 * |topology.c| is a place where routines for searching LSAs in the link-state
30
 * database, adding and deleting them reside, there also are functions for
31
 * originating of various types of LSAs (router LSA, net LSA, external LSA).
32
 * File |rt.c| contains routines for calculating the routing table. |lsalib.c|
33
 * is a set of various functions for working with the LSAs (endianity
34
 * conversions, calculation of checksum etc.).
35
 *
36
 * One instance of the protocol is able to hold LSA databases for multiple OSPF
37
 * areas, to exchange routing information between multiple neighbors and to
38
 * calculate the routing tables. The core structure is &ospf_proto to which
39
 * multiple &ospf_area and &ospf_iface structures are connected. &ospf_proto is
40
 * also connected to &top_hash_graph which is a dynamic hashing structure that
41
 * describes the link-state database. It allows fast search, addition and
42
 * 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
 * In OSPFv2 specification, it is implied that there is one IP prefix for each
46
 * physical network/interface (unless it is an ptp link). But in modern systems,
47
 * there might be more independent IP prefixes associated with an interface.  To
48
 * handle this situation, we have one &ospf_iface for each active IP prefix
49
 * (instead for each active iface); This behaves like virtual interface for the
50
 * purpose of OSPF.  If we receive packet, we associate it with a proper virtual
51
 * interface mainly according to its source address.
52
 *
53
 * OSPF keeps one socket per &ospf_iface. This allows us (compared to one socket
54
 * approach) to evade problems with a limit of multicast groups per socket and
55
 * with sending multicast packets to appropriate interface in a portable way.
56
 * The socket is associated with underlying physical iface and should not
57
 * receive packets received on other ifaces (unfortunately, this is not true on
58
 * BSD). Generally, one packet can be received by more sockets (for example, if
59
 * there are more &ospf_iface on one physical iface), therefore we explicitly
60
 * filter received packets according to src/dst IP address and received iface.
61
 *
62
 * Vlinks are implemented using particularly degenerate form of &ospf_iface,
63
 * which has several exceptions: it does not have its iface or socket (it copies
64
 * these from 'parent' &ospf_iface) and it is present in iface list even when
65
 * down (it is not freed in ospf_iface_down()).
66
 *
67
 * The heart beat of ospf is ospf_disp(). It is called at regular intervals
68
 * (&ospf_proto->tick). It is responsible for aging and flushing of LSAs in the
69
 * database, updating topology information in LSAs and for routing table
70
 * calculation.
71
 *
72
 * To every &ospf_iface, we connect one or more &ospf_neighbor's -- a structure
73
 * containing many timers and queues for building adjacency and for exchange of
74
 * routing messages.
75
 *
76
 * BIRD's OSPF implementation respects RFC2328 in every detail, but some of
77
 * internal algorithms do differ. The RFC recommends making a snapshot of the
78
 * link-state database when a new adjacency is forming and sending the database
79
 * description packets based on the information in this snapshot. The database
80
 * can be quite large in some networks, so rather we walk through a &slist
81
 * structure which allows us to continue even if the actual LSA we were working
82
 * with is deleted. New LSAs are added at the tail of this &slist.
83
 *
84
 * We also do not keep a separate OSPF routing table, because the core helps us
85
 * by being able to recognize when a route is updated to an identical one and it
86
 * suppresses the update automatically. Due to this, we can flush all the routes
87
 * we have recalculated and also those we have deleted to the core's routing
88
 * table and the core will take care of the rest. This simplifies the process
89
 * and conserves memory.
90
 *
91
 * Supported standards:
92
 * - RFC 2328 - main OSPFv2 standard
93
 * - RFC 5340 - main OSPFv3 standard
94
 * - RFC 3101 - OSPFv2 NSSA areas
95
 * - RFC 6549 - OSPFv2 multi-instance extensions
96
 * - RFC 6987 - OSPF stub router advertisement
97
 */
98

    
99
#include <stdlib.h>
100
#include "ospf.h"
101

    
102
static int ospf_import_control(struct proto *P, rte **new, ea_list **attrs, struct linpool *pool);
103
static struct ea_list *ospf_make_tmp_attrs(struct rte *rt, struct linpool *pool);
104
static void ospf_store_tmp_attrs(struct rte *rt, struct ea_list *attrs);
105
static int ospf_reload_routes(struct proto *P);
106
static int ospf_rte_better(struct rte *new, struct rte *old);
107
static int ospf_rte_same(struct rte *new, struct rte *old);
108
static void ospf_disp(timer *timer);
109

    
110
static void
111
ospf_area_initfib(struct fib_node *fn)
112
{
113
  struct area_net *an = (struct area_net *) fn;
114
  an->hidden = 0;
115
  an->active = 0;
116
}
117

    
118
static void
119
add_area_nets(struct ospf_area *oa, struct ospf_area_config *ac)
120
{
121
  struct ospf_proto *p = oa->po;
122
  struct area_net_config *anc;
123
  struct area_net *an;
124

    
125
  fib_init(&oa->net_fib, p->p.pool, sizeof(struct area_net), 0, ospf_area_initfib);
126
  fib_init(&oa->enet_fib, p->p.pool, sizeof(struct area_net), 0, ospf_area_initfib);
127

    
128
  WALK_LIST(anc, ac->net_list)
129
  {
130
    an = (struct area_net *) fib_get(&oa->net_fib, &anc->px.addr, anc->px.len);
131
    an->hidden = anc->hidden;
132
  }
133

    
134
  WALK_LIST(anc, ac->enet_list)
135
  {
136
    an = (struct area_net *) fib_get(&oa->enet_fib, &anc->px.addr, anc->px.len);
137
    an->hidden = anc->hidden;
138
    an->tag = anc->tag;
139
  }
140
}
141

    
142
static void
143
ospf_area_add(struct ospf_proto *p, struct ospf_area_config *ac)
144
{
145
  struct ospf_area *oa;
146

    
147
  OSPF_TRACE(D_EVENTS, "Adding area %R", ac->areaid);
148

    
149
  oa = mb_allocz(p->p.pool, sizeof(struct ospf_area));
150
  add_tail(&p->area_list, NODE oa);
151
  p->areano++;
152

    
153
  oa->ac = ac;
154
  oa->areaid = ac->areaid;
155
  oa->rt = NULL;
156
  oa->po = p;
157
  fib_init(&oa->rtr, p->p.pool, sizeof(ort), 0, ospf_rt_initort);
158
  add_area_nets(oa, ac);
159

    
160
  if (oa->areaid == 0)
161
    p->backbone = oa;
162

    
163
  if (ospf_is_v2(p))
164
    oa->options = ac->type;
165
  else
166
    oa->options = ac->type | OPT_V6 | (p->stub_router ? 0 : OPT_R);
167

    
168
  ospf_notify_rt_lsa(oa);
169
}
170

    
171
static void
172
ospf_flush_area(struct ospf_proto *p, u32 areaid)
173
{
174
  struct top_hash_entry *en;
175

    
176
  WALK_SLIST(en, p->lsal)
177
    if ((LSA_SCOPE(en->lsa_type) == LSA_SCOPE_AREA) && (en->domain == areaid))
178
      ospf_flush_lsa(p, en);
179
}
180

    
181
static void
182
ospf_area_remove(struct ospf_area *oa)
183
{
184
  struct ospf_proto *p = oa->po;
185
  OSPF_TRACE(D_EVENTS, "Removing area %R", oa->areaid);
186

    
187
  /* We suppose that interfaces are already removed */
188
  ospf_flush_area(p, oa->areaid);
189

    
190
  fib_free(&oa->rtr);
191
  fib_free(&oa->net_fib);
192
  fib_free(&oa->enet_fib);
193

    
194
  if (oa->translator_timer)
195
    rfree(oa->translator_timer);
196

    
197
  p->areano--;
198
  rem_node(NODE oa);
199
  mb_free(oa);
200
}
201

    
202

    
203
struct ospf_area *
204
ospf_find_area(struct ospf_proto *p, u32 aid)
205
{
206
  struct ospf_area *oa;
207
  WALK_LIST(oa, p->area_list)
208
    if (((struct ospf_area *) oa)->areaid == aid)
209
      return oa;
210
  return NULL;
211
}
212

    
213
static struct ospf_iface *
214
ospf_find_vlink(struct ospf_proto *p, u32 voa, u32 vid)
215
{
216
  struct ospf_iface *ifa;
217
  WALK_LIST(ifa, p->iface_list)
218
    if ((ifa->type == OSPF_IT_VLINK) && (ifa->voa->areaid == voa) && (ifa->vid == vid))
219
      return ifa;
220
  return NULL;
221
}
222

    
223
static int
224
ospf_start(struct proto *P)
225
{
226
  struct ospf_proto *p = (struct ospf_proto *) P;
227
  struct ospf_config *c = (struct ospf_config *) (P->cf);
228
  struct ospf_area_config *ac;
229

    
230
  p->router_id = proto_get_router_id(P->cf);
231
  p->ospf2 = c->ospf2;
232
  p->rfc1583 = c->rfc1583;
233
  p->stub_router = c->stub_router;
234
  p->merge_external = c->merge_external;
235
  p->asbr = c->asbr;
236
  p->ecmp = c->ecmp;
237
  p->tick = c->tick;
238
  p->disp_timer = tm_new_set(P->pool, ospf_disp, p, 0, p->tick);
239
  tm_start(p->disp_timer, 1);
240
  p->lsab_size = 256;
241
  p->lsab_used = 0;
242
  p->lsab = mb_alloc(P->pool, p->lsab_size);
243
  p->nhpool = lp_new(P->pool, 12*sizeof(struct mpnh));
244
  init_list(&(p->iface_list));
245
  init_list(&(p->area_list));
246
  fib_init(&p->rtf, P->pool, sizeof(ort), 0, ospf_rt_initort);
247
  p->areano = 0;
248
  p->gr = ospf_top_new(p, P->pool);
249
  s_init_list(&(p->lsal));
250

    
251
  p->flood_event = ev_new(P->pool);
252
  p->flood_event->hook = ospf_flood_event;
253
  p->flood_event->data = p;
254

    
255
  p->log_pkt_tbf = (struct tbf){ .rate = 1, .burst = 5 };
256
  p->log_lsa_tbf = (struct tbf){ .rate = 4, .burst = 20 };
257

    
258
  WALK_LIST(ac, c->area_list)
259
    ospf_area_add(p, ac);
260

    
261
  if (c->abr)
262
    ospf_open_vlink_sk(p);
263

    
264
  /* Add all virtual links */
265
  struct ospf_iface_patt *ic;
266
  WALK_LIST(ic, c->vlink_list)
267
    ospf_iface_new_vlink(p, ic);
268

    
269
  return PS_UP;
270
}
271

    
272
static void
273
ospf_dump(struct proto *P)
274
{
275
  struct ospf_proto *p = (struct ospf_proto *) P;
276
  struct ospf_iface *ifa;
277
  struct ospf_neighbor *n;
278

    
279
  OSPF_TRACE(D_EVENTS, "Area number: %d", p->areano);
280

    
281
  WALK_LIST(ifa, p->iface_list)
282
  {
283
    OSPF_TRACE(D_EVENTS, "Interface: %s", ifa->ifname);
284
    OSPF_TRACE(D_EVENTS, "state: %u", ifa->state);
285
    OSPF_TRACE(D_EVENTS, "DR:  %R", ifa->drid);
286
    OSPF_TRACE(D_EVENTS, "BDR: %R", ifa->bdrid);
287
    WALK_LIST(n, ifa->neigh_list)
288
    {
289
      OSPF_TRACE(D_EVENTS, "  neighbor %R in state %u", n->rid, n->state);
290
    }
291
  }
292

    
293
  /*
294
  OSPF_TRACE(D_EVENTS, "LSA graph dump start:");
295
  ospf_top_dump(p->gr, p);
296
  OSPF_TRACE(D_EVENTS, "LSA graph dump finished");
297
  */
298
  neigh_dump_all();
299
}
300

    
301
static struct proto *
302
ospf_init(struct proto_config *c)
303
{
304
  struct ospf_config *oc = (struct ospf_config *) c;
305
  struct proto *P = proto_new(c, sizeof(struct ospf_proto));
306

    
307
  P->accept_ra_types = RA_OPTIMAL;
308
  P->rt_notify = ospf_rt_notify;
309
  P->if_notify = ospf_if_notify;
310
  P->ifa_notify = oc->ospf2 ? ospf_ifa_notify2 : ospf_ifa_notify3;
311
  P->import_control = ospf_import_control;
312
  P->reload_routes = ospf_reload_routes;
313
  P->make_tmp_attrs = ospf_make_tmp_attrs;
314
  P->store_tmp_attrs = ospf_store_tmp_attrs;
315
  P->rte_better = ospf_rte_better;
316
  P->rte_same = ospf_rte_same;
317

    
318
  return P;
319
}
320

    
321
/* If new is better return 1 */
322
static int
323
ospf_rte_better(struct rte *new, struct rte *old)
324
{
325
  if (new->u.ospf.metric1 == LSINFINITY)
326
    return 0;
327

    
328
  if(new->attrs->source < old->attrs->source) return 1;
329
  if(new->attrs->source > old->attrs->source) return 0;
330

    
331
  if(new->attrs->source == RTS_OSPF_EXT2)
332
  {
333
    if(new->u.ospf.metric2 < old->u.ospf.metric2) return 1;
334
    if(new->u.ospf.metric2 > old->u.ospf.metric2) return 0;
335
  }
336

    
337
  if (new->u.ospf.metric1 < old->u.ospf.metric1)
338
    return 1;
339

    
340
  return 0;                        /* Old is shorter or same */
341
}
342

    
343
static int
344
ospf_rte_same(struct rte *new, struct rte *old)
345
{
346
  /* new->attrs == old->attrs always */
347
  return
348
    new->u.ospf.metric1 == old->u.ospf.metric1 &&
349
    new->u.ospf.metric2 == old->u.ospf.metric2 &&
350
    new->u.ospf.tag == old->u.ospf.tag &&
351
    new->u.ospf.router_id == old->u.ospf.router_id;
352
}
353

    
354
static ea_list *
355
ospf_build_attrs(ea_list * next, struct linpool *pool, u32 m1, u32 m2,
356
                 u32 tag, u32 rid)
357
{
358
  struct ea_list *l =
359
    lp_alloc(pool, sizeof(struct ea_list) + 4 * sizeof(eattr));
360

    
361
  l->next = next;
362
  l->flags = EALF_SORTED;
363
  l->count = 4;
364
  l->attrs[0].id = EA_OSPF_METRIC1;
365
  l->attrs[0].flags = 0;
366
  l->attrs[0].type = EAF_TYPE_INT | EAF_TEMP;
367
  l->attrs[0].u.data = m1;
368
  l->attrs[1].id = EA_OSPF_METRIC2;
369
  l->attrs[1].flags = 0;
370
  l->attrs[1].type = EAF_TYPE_INT | EAF_TEMP;
371
  l->attrs[1].u.data = m2;
372
  l->attrs[2].id = EA_OSPF_TAG;
373
  l->attrs[2].flags = 0;
374
  l->attrs[2].type = EAF_TYPE_INT | EAF_TEMP;
375
  l->attrs[2].u.data = tag;
376
  l->attrs[3].id = EA_OSPF_ROUTER_ID;
377
  l->attrs[3].flags = 0;
378
  l->attrs[3].type = EAF_TYPE_ROUTER_ID | EAF_TEMP;
379
  l->attrs[3].u.data = rid;
380
  return l;
381
}
382

    
383

    
384
void
385
ospf_schedule_rtcalc(struct ospf_proto *p)
386
{
387
  if (p->calcrt)
388
    return;
389

    
390
  OSPF_TRACE(D_EVENTS, "Scheduling routing table calculation");
391
  p->calcrt = 1;
392
}
393

    
394
static int
395
ospf_reload_routes(struct proto *P)
396
{
397
  struct ospf_proto *p = (struct ospf_proto *) P;
398

    
399
  if (p->calcrt != 2)
400
    OSPF_TRACE(D_EVENTS, "Scheduling routing table calculation with route reload");
401

    
402
  p->calcrt = 2;
403

    
404
  return 1;
405
}
406

    
407

    
408
/**
409
 * ospf_disp - invokes routing table calculation, aging and also area_disp()
410
 * @timer: timer usually called every @ospf_proto->tick second, @timer->data
411
 * point to @ospf_proto
412
 */
413
static void
414
ospf_disp(timer * timer)
415
{
416
  struct ospf_proto *p = timer->data;
417

    
418
  /* Originate or flush local topology LSAs */
419
  ospf_update_topology(p);
420

    
421
  /* Process LSA DB */
422
  ospf_update_lsadb(p);
423

    
424
  /* Calculate routing table */
425
  if (p->calcrt)
426
    ospf_rt_spf(p);
427
}
428

    
429

    
430
/**
431
 * ospf_import_control - accept or reject new route from nest's routing table
432
 * @P: OSPF protocol instance
433
 * @new: the new route
434
 * @attrs: list of attributes
435
 * @pool: pool for allocation of attributes
436
 *
437
 * Its quite simple. It does not accept our own routes and leaves the decision on
438
 * import to the filters.
439
 */
440
static int
441
ospf_import_control(struct proto *P, rte **new, ea_list **attrs, struct linpool *pool)
442
{
443
  struct ospf_proto *p = (struct ospf_proto *) P;
444
  struct ospf_area *oa = ospf_main_area(p);
445
  rte *e = *new;
446

    
447
  if (e->attrs->src->proto == P)
448
    return -1;                        /* Reject our own routes */
449

    
450
  if (oa_is_stub(oa))
451
    return -1;                        /* Do not export routes to stub areas */
452

    
453
  eattr *ea = ea_find(e->attrs->eattrs, EA_GEN_IGP_METRIC);
454
  u32 m1 = (ea && (ea->u.data < LSINFINITY)) ? ea->u.data : LSINFINITY;
455

    
456
  *attrs = ospf_build_attrs(*attrs, pool, m1, 10000, 0, 0);
457
  return 0;                        /* Leave decision to the filters */
458
}
459

    
460
static struct ea_list *
461
ospf_make_tmp_attrs(struct rte *rt, struct linpool *pool)
462
{
463
  return ospf_build_attrs(NULL, pool, rt->u.ospf.metric1, rt->u.ospf.metric2,
464
                          rt->u.ospf.tag, rt->u.ospf.router_id);
465
}
466

    
467
static void
468
ospf_store_tmp_attrs(struct rte *rt, struct ea_list *attrs)
469
{
470
  rt->u.ospf.metric1 = ea_get_int(attrs, EA_OSPF_METRIC1, LSINFINITY);
471
  rt->u.ospf.metric2 = ea_get_int(attrs, EA_OSPF_METRIC2, 10000);
472
  rt->u.ospf.tag = ea_get_int(attrs, EA_OSPF_TAG, 0);
473
  rt->u.ospf.router_id = ea_get_int(attrs, EA_OSPF_ROUTER_ID, 0);
474
}
475

    
476
/**
477
 * ospf_shutdown - Finish of OSPF instance
478
 * @P: OSPF protocol instance
479
 *
480
 * RFC does not define any action that should be taken before router
481
 * shutdown. To make my neighbors react as fast as possible, I send
482
 * them hello packet with empty neighbor list. They should start
483
 * their neighbor state machine with event %NEIGHBOR_1WAY.
484
 */
485
static int
486
ospf_shutdown(struct proto *P)
487
{
488
  struct ospf_proto *p = (struct ospf_proto *) P;
489
  struct ospf_iface *ifa;
490

    
491
  OSPF_TRACE(D_EVENTS, "Shutdown requested");
492

    
493
  /* And send to all my neighbors 1WAY */
494
  WALK_LIST(ifa, p->iface_list)
495
    ospf_iface_shutdown(ifa);
496

    
497
  /* Cleanup locked rta entries */
498
  FIB_WALK(&p->rtf, nftmp)
499
  {
500
    rta_free(((ort *) nftmp)->old_rta);
501
  }
502
  FIB_WALK_END;
503

    
504
  return PS_DOWN;
505
}
506

    
507
static void
508
ospf_get_status(struct proto *P, byte * buf)
509
{
510
  struct ospf_proto *p = (struct ospf_proto *) P;
511

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

    
520
    WALK_LIST(ifa, p->iface_list)
521
      WALK_LIST(n, ifa->neigh_list) if (n->state == NEIGHBOR_FULL)
522
      adj = 1;
523

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

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

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

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

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

    
587
static void
588
ospf_area_reconfigure(struct ospf_area *oa, struct ospf_area_config *nac)
589
{
590
  struct ospf_proto *p = oa->po;
591
  struct ospf_area_config *oac = oa->ac;
592
  struct ospf_iface *ifa;
593

    
594
  oa->ac = nac;
595

    
596
  if (ospf_is_v2(p))
597
    oa->options = nac->type;
598
  else
599
    oa->options = nac->type | OPT_V6 | (p->stub_router ? 0 : OPT_R);
600

    
601
  if (nac->type != oac->type)
602
  {
603
    /* Force restart of area interfaces */
604
    WALK_LIST(ifa, p->iface_list)
605
      if (ifa->oa == oa)
606
        ifa->marked = 2;
607
  }
608

    
609
  /* Handle net_list */
610
  fib_free(&oa->net_fib);
611
  fib_free(&oa->enet_fib);
612
  add_area_nets(oa, nac);
613

    
614
  /* No need to handle stubnet_list */
615

    
616
  oa->marked = 0;
617
  ospf_notify_rt_lsa(oa);
618
}
619

    
620
/**
621
 * ospf_reconfigure - reconfiguration hook
622
 * @P: current instance of protocol (with old configuration)
623
 * @c: new configuration requested by user
624
 *
625
 * This hook tries to be a little bit intelligent. Instance of OSPF
626
 * will survive change of many constants like hello interval,
627
 * password change, addition or deletion of some neighbor on
628
 * nonbroadcast network, cost of interface, etc.
629
 */
630
static int
631
ospf_reconfigure(struct proto *P, struct proto_config *c)
632
{
633
  struct ospf_proto *p = (struct ospf_proto *) P;
634
  struct ospf_config *old = (struct ospf_config *) (P->cf);
635
  struct ospf_config *new = (struct ospf_config *) c;
636
  struct ospf_area_config *nac;
637
  struct ospf_area *oa, *oax;
638
  struct ospf_iface *ifa, *ifx;
639
  struct ospf_iface_patt *ip;
640

    
641
  if (proto_get_router_id(c) != p->router_id)
642
    return 0;
643

    
644
  if (p->rfc1583 != new->rfc1583)
645
    return 0;
646

    
647
  if (old->abr != new->abr)
648
    return 0;
649

    
650
  p->stub_router = new->stub_router;
651
  p->merge_external = new->merge_external;
652
  p->asbr = new->asbr;
653
  p->ecmp = new->ecmp;
654
  p->tick = new->tick;
655
  p->disp_timer->recurrent = p->tick;
656
  tm_start(p->disp_timer, 1);
657

    
658
  /* Mark all areas and ifaces */
659
  WALK_LIST(oa, p->area_list)
660
    oa->marked = 1;
661

    
662
  WALK_LIST(ifa, p->iface_list)
663
    ifa->marked = 1;
664

    
665
  /* Add and update areas */
666
  WALK_LIST(nac, new->area_list)
667
  {
668
    oa = ospf_find_area(p, nac->areaid);
669
    if (oa)
670
      ospf_area_reconfigure(oa, nac);
671
    else
672
      ospf_area_add(p, nac);
673
  }
674

    
675
  /* Add and update interfaces */
676
  ospf_reconfigure_ifaces(p);
677

    
678
  /* Add and update vlinks */
679
  WALK_LIST(ip, new->vlink_list)
680
  {
681
    ifa = ospf_find_vlink(p, ip->voa, ip->vid);
682
    if (ifa)
683
      ospf_iface_reconfigure(ifa, ip);
684
    else
685
      ospf_iface_new_vlink(p, ip);
686
  }
687

    
688
  /* Delete remaining ifaces and areas */
689
  WALK_LIST_DELSAFE(ifa, ifx, p->iface_list)
690
    if (ifa->marked)
691
    {
692
      ospf_iface_shutdown(ifa);
693
      ospf_iface_remove(ifa);
694
    }
695

    
696
  WALK_LIST_DELSAFE(oa, oax, p->area_list)
697
    if (oa->marked)
698
      ospf_area_remove(oa);
699

    
700
  ospf_schedule_rtcalc(p);
701

    
702
  return 1;
703
}
704

    
705

    
706
void
707
ospf_sh_neigh(struct proto *P, char *iff)
708
{
709
  struct ospf_proto *p = (struct ospf_proto *) P;
710
  struct ospf_iface *ifa = NULL;
711
  struct ospf_neighbor *n;
712

    
713
  if (p->p.proto_state != PS_UP)
714
  {
715
    cli_msg(-1013, "%s: is not up", p->p.name);
716
    cli_msg(0, "");
717
    return;
718
  }
719

    
720
  cli_msg(-1013, "%s:", p->p.name);
721
  cli_msg(-1013, "%-12s\t%3s\t%-15s\t%-5s\t%-10s %-12s", "Router ID", "Pri",
722
          "     State", "DTime", "Interface", "Router IP");
723
  WALK_LIST(ifa, p->iface_list)
724
    if ((iff == NULL) || patmatch(iff, ifa->ifname))
725
      WALK_LIST(n, ifa->neigh_list)
726
        ospf_sh_neigh_info(n);
727
  cli_msg(0, "");
728
}
729

    
730
void
731
ospf_sh(struct proto *P)
732
{
733
  struct ospf_proto *p = (struct ospf_proto *) P;
734
  struct ospf_area *oa;
735
  struct ospf_iface *ifa;
736
  struct ospf_neighbor *n;
737
  int ifano, nno, adjno, firstfib;
738
  struct area_net *anet;
739

    
740
  if (p->p.proto_state != PS_UP)
741
  {
742
    cli_msg(-1014, "%s: is not up", p->p.name);
743
    cli_msg(0, "");
744
    return;
745
  }
746

    
747
  cli_msg(-1014, "%s:", p->p.name);
748
  cli_msg(-1014, "RFC1583 compatibility: %s", (p->rfc1583 ? "enabled" : "disabled"));
749
  cli_msg(-1014, "Stub router: %s", (p->stub_router ? "Yes" : "No"));
750
  cli_msg(-1014, "RT scheduler tick: %d", p->tick);
751
  cli_msg(-1014, "Number of areas: %u", p->areano);
752
  cli_msg(-1014, "Number of LSAs in DB:\t%u", p->gr->hash_entries);
753

    
754
  WALK_LIST(oa, p->area_list)
755
  {
756
    cli_msg(-1014, "\tArea: %R (%u) %s", oa->areaid, oa->areaid,
757
            oa->areaid == 0 ? "[BACKBONE]" : "");
758
    ifano = 0;
759
    nno = 0;
760
    adjno = 0;
761
    WALK_LIST(ifa, p->iface_list)
762
    {
763
      if (oa == ifa->oa)
764
      {
765
        ifano++;
766
        WALK_LIST(n, ifa->neigh_list)
767
        {
768
          nno++;
769
          if (n->state == NEIGHBOR_FULL)
770
            adjno++;
771
        }
772
      }
773
    }
774

    
775
    cli_msg(-1014, "\t\tStub:\t%s", oa_is_stub(oa) ? "Yes" : "No");
776
    cli_msg(-1014, "\t\tNSSA:\t%s", oa_is_nssa(oa) ? "Yes" : "No");
777
    cli_msg(-1014, "\t\tTransit:\t%s", oa->trcap ? "Yes" : "No");
778

    
779
    if (oa_is_nssa(oa))
780
      cli_msg(-1014, "\t\tNSSA translation:\t%s%s", oa->translate ? "Yes" : "No",
781
              oa->translate == TRANS_WAIT ? " (run down)" : "");
782
    cli_msg(-1014, "\t\tNumber of interfaces:\t%u", ifano);
783
    cli_msg(-1014, "\t\tNumber of neighbors:\t%u", nno);
784
    cli_msg(-1014, "\t\tNumber of adjacent neighbors:\t%u", adjno);
785

    
786
    firstfib = 1;
787
    FIB_WALK(&oa->net_fib, nftmp)
788
    {
789
      anet = (struct area_net *) nftmp;
790
      if(firstfib)
791
      {
792
        cli_msg(-1014, "\t\tArea networks:");
793
        firstfib = 0;
794
      }
795
      cli_msg(-1014, "\t\t\t%1I/%u\t%s\t%s", anet->fn.prefix, anet->fn.pxlen,
796
                anet->hidden ? "Hidden" : "Advertise", anet->active ? "Active" : "");
797
    }
798
    FIB_WALK_END;
799

    
800
    firstfib = 1;
801
    FIB_WALK(&oa->enet_fib, nftmp)
802
    {
803
      anet = (struct area_net *) nftmp;
804
      if(firstfib)
805
      {
806
        cli_msg(-1014, "\t\tArea external networks:");
807
        firstfib = 0;
808
      }
809
      cli_msg(-1014, "\t\t\t%1I/%u\t%s\t%s", anet->fn.prefix, anet->fn.pxlen,
810
                anet->hidden ? "Hidden" : "Advertise", anet->active ? "Active" : "");
811
    }
812
    FIB_WALK_END;
813

    
814
  }
815
  cli_msg(0, "");
816
}
817

    
818
void
819
ospf_sh_iface(struct proto *P, char *iff)
820
{
821
  struct ospf_proto *p = (struct ospf_proto *) P;
822
  struct ospf_iface *ifa = NULL;
823

    
824
  if (p->p.proto_state != PS_UP)
825
  {
826
    cli_msg(-1015, "%s: is not up", p->p.name);
827
    cli_msg(0, "");
828
    return;
829
  }
830

    
831
  cli_msg(-1015, "%s:", p->p.name);
832
  WALK_LIST(ifa, p->iface_list)
833
    if ((iff == NULL) || patmatch(iff, ifa->ifname))
834
      ospf_iface_info(ifa);
835
  cli_msg(0, "");
836
}
837

    
838
/* lsa_compare_for_state() - Compare function for 'show ospf state'
839
 *
840
 * First we want to separate network-LSAs and other LSAs (because network-LSAs
841
 * will be presented as network nodes and other LSAs together as router nodes)
842
 * Network-LSAs are sorted according to network prefix, other LSAs are sorted
843
 * according to originating router id (to get all LSA needed to represent one
844
 * router node together). Then, according to LSA type, ID and age.
845
 *
846
 * For OSPFv3, we have to handle also Prefix-LSAs. We would like to put each
847
 * immediately after the referenced LSA. We will make faked LSA based on ref_
848
 * values
849
 */
850

    
851
static struct ospf_lsa_header *
852
fake_lsa_from_prefix_lsa(struct ospf_lsa_header *dst, struct ospf_lsa_header *src,
853
                         struct ospf_lsa_prefix *px)
854
{
855
  dst->age = src->age;
856
  dst->type_raw = px->ref_type;
857
  dst->id = px->ref_id;
858
  dst->rt = px->ref_rt;
859
  dst->sn = src->sn;
860

    
861
  return dst;
862
}
863

    
864

    
865
static int lsa_compare_ospf3;
866

    
867
static int
868
lsa_compare_for_state(const void *p1, const void *p2)
869
{
870
  struct top_hash_entry *he1 = * (struct top_hash_entry **) p1;
871
  struct top_hash_entry *he2 = * (struct top_hash_entry **) p2;
872
  struct ospf_lsa_header *lsa1 = &(he1->lsa);
873
  struct ospf_lsa_header *lsa2 = &(he2->lsa);
874
  struct ospf_lsa_header lsatmp1, lsatmp2;
875
  u16 lsa1_type = he1->lsa_type;
876
  u16 lsa2_type = he2->lsa_type;
877

    
878
  if (he1->domain < he2->domain)
879
    return -1;
880
  if (he1->domain > he2->domain)
881
    return 1;
882

    
883

    
884
  /* px1 or px2 assumes OSPFv3 */
885
  int px1 = (lsa1_type == LSA_T_PREFIX);
886
  int px2 = (lsa2_type == LSA_T_PREFIX);
887

    
888
  if (px1)
889
  {
890
    lsa1 = fake_lsa_from_prefix_lsa(&lsatmp1, lsa1, he1->lsa_body);
891
    lsa1_type = lsa1->type_raw;        /* FIXME: handle unknown ref_type */
892
  }
893

    
894
  if (px2)
895
  {
896
    lsa2 = fake_lsa_from_prefix_lsa(&lsatmp2, lsa2, he2->lsa_body);
897
    lsa2_type = lsa2->type_raw;
898
  }
899

    
900

    
901
  int nt1 = (lsa1_type == LSA_T_NET);
902
  int nt2 = (lsa2_type == LSA_T_NET);
903

    
904
  if (nt1 != nt2)
905
    return nt1 - nt2;
906

    
907
  if (nt1)
908
  {
909
    /* In OSPFv3, networks are named based on ID of DR */
910
    if (lsa_compare_ospf3)
911
    {
912
      if (lsa1->rt < lsa2->rt)
913
        return -1;
914
      if (lsa1->rt > lsa2->rt)
915
        return 1;
916
    }
917

    
918
    /* For OSPFv2, this is IP of the network,
919
       for OSPFv3, this is interface ID */
920
    if (lsa1->id < lsa2->id)
921
      return -1;
922
    if (lsa1->id > lsa2->id)
923
      return 1;
924

    
925
    if (px1 != px2)
926
      return px1 - px2;
927

    
928
    return lsa1->sn - lsa2->sn;
929
  }
930
  else
931
  {
932
    if (lsa1->rt < lsa2->rt)
933
      return -1;
934
    if (lsa1->rt > lsa2->rt)
935
      return 1;
936

    
937
    if (lsa1_type < lsa2_type)
938
      return -1;
939
    if (lsa1_type > lsa2_type)
940
      return 1;
941

    
942
    if (lsa1->id < lsa2->id)
943
      return -1;
944
    if (lsa1->id > lsa2->id)
945
      return 1;
946

    
947
    if (px1 != px2)
948
      return px1 - px2;
949

    
950
    return lsa1->sn - lsa2->sn;
951
  }
952
}
953

    
954
static int
955
ext_compare_for_state(const void *p1, const void *p2)
956
{
957
  struct top_hash_entry * he1 = * (struct top_hash_entry **) p1;
958
  struct top_hash_entry * he2 = * (struct top_hash_entry **) p2;
959
  struct ospf_lsa_header *lsa1 = &(he1->lsa);
960
  struct ospf_lsa_header *lsa2 = &(he2->lsa);
961

    
962
  if (lsa1->rt < lsa2->rt)
963
    return -1;
964
  if (lsa1->rt > lsa2->rt)
965
    return 1;
966

    
967
  if (lsa1->id < lsa2->id)
968
    return -1;
969
  if (lsa1->id > lsa2->id)
970
    return 1;
971

    
972
  return lsa1->sn - lsa2->sn;
973
}
974

    
975
static inline void
976
show_lsa_distance(struct top_hash_entry *he)
977
{
978
  if (he->color == INSPF)
979
    cli_msg(-1016, "\t\tdistance %u", he->dist);
980
  else
981
    cli_msg(-1016, "\t\tunreachable");
982
}
983

    
984
static inline void
985
show_lsa_router(struct ospf_proto *p, struct top_hash_entry *he, int verbose)
986
{
987
  struct ospf_lsa_rt_walk rtl;
988

    
989
  cli_msg(-1016, "");
990
  cli_msg(-1016, "\trouter %R", he->lsa.rt);
991
  show_lsa_distance(he);
992

    
993
  lsa_walk_rt_init(p, he, &rtl);
994
  while (lsa_walk_rt(&rtl))
995
    if (rtl.type == LSART_VLNK)
996
      cli_msg(-1016, "\t\tvlink %R metric %u", rtl.id, rtl.metric);
997

    
998
  lsa_walk_rt_init(p, he, &rtl);
999
  while (lsa_walk_rt(&rtl))
1000
    if (rtl.type == LSART_PTP)
1001
      cli_msg(-1016, "\t\trouter %R metric %u", rtl.id, rtl.metric);
1002

    
1003
  lsa_walk_rt_init(p, he, &rtl);
1004
  while (lsa_walk_rt(&rtl))
1005
    if (rtl.type == LSART_NET)
1006
    {
1007
      if (ospf_is_v2(p))
1008
      {
1009
        /* In OSPFv2, we try to find network-LSA to get prefix/pxlen */
1010
        struct top_hash_entry *net_he = ospf_hash_find_net2(p->gr, he->domain, rtl.id);
1011

    
1012
        if (net_he && (net_he->lsa.age < LSA_MAXAGE))
1013
        {
1014
          struct ospf_lsa_header *net_lsa = &(net_he->lsa);
1015
          struct ospf_lsa_net *net_ln = net_he->lsa_body;
1016

    
1017
          cli_msg(-1016, "\t\tnetwork %I/%d metric %u",
1018
                  ipa_from_u32(net_lsa->id & net_ln->optx),
1019
                  u32_masklen(net_ln->optx), rtl.metric);
1020
        }
1021
        else
1022
          cli_msg(-1016, "\t\tnetwork [%R] metric %u", rtl.id, rtl.metric);
1023
      }
1024
      else
1025
        cli_msg(-1016, "\t\tnetwork [%R-%u] metric %u", rtl.id, rtl.nif, rtl.metric);
1026
    }
1027

    
1028
  if (ospf_is_v2(p) && verbose)
1029
  {
1030
    lsa_walk_rt_init(p, he, &rtl);
1031
    while (lsa_walk_rt(&rtl))
1032
      if (rtl.type == LSART_STUB)
1033
        cli_msg(-1016, "\t\tstubnet %I/%d metric %u",
1034
                ipa_from_u32(rtl.id), u32_masklen(rtl.data), rtl.metric);
1035
  }
1036
}
1037

    
1038
static inline void
1039
show_lsa_network(struct top_hash_entry *he, int ospf2)
1040
{
1041
  struct ospf_lsa_header *lsa = &(he->lsa);
1042
  struct ospf_lsa_net *ln = he->lsa_body;
1043
  u32 i;
1044

    
1045
  if (ospf2)
1046
  {
1047
    cli_msg(-1016, "");
1048
    cli_msg(-1016, "\tnetwork %I/%d", ipa_from_u32(lsa->id & ln->optx), u32_masklen(ln->optx));
1049
    cli_msg(-1016, "\t\tdr %R", lsa->rt);
1050
  }
1051
  else
1052
  {
1053
    cli_msg(-1016, "");
1054
    cli_msg(-1016, "\tnetwork [%R-%u]", lsa->rt, lsa->id);
1055
  }
1056

    
1057
  show_lsa_distance(he);
1058

    
1059
  for (i = 0; i < lsa_net_count(lsa); i++)
1060
    cli_msg(-1016, "\t\trouter %R", ln->routers[i]);
1061
}
1062

    
1063
static inline void
1064
show_lsa_sum_net(struct top_hash_entry *he, int ospf2)
1065
{
1066
  ip_addr ip;
1067
  int pxlen;
1068
  u8 pxopts;
1069
  u32 metric;
1070

    
1071
  lsa_parse_sum_net(he, ospf2, &ip, &pxlen, &pxopts, &metric);
1072
  cli_msg(-1016, "\t\txnetwork %I/%d metric %u", ip, pxlen, metric);
1073
}
1074

    
1075
static inline void
1076
show_lsa_sum_rt(struct top_hash_entry *he, int ospf2)
1077
{
1078
  u32 metric;
1079
  u32 dst_rid;
1080
  u32 options;
1081

    
1082
  lsa_parse_sum_rt(he, ospf2, &dst_rid, &metric, &options);
1083
  cli_msg(-1016, "\t\txrouter %R metric %u", dst_rid, metric);
1084
}
1085

    
1086

    
1087
static inline void
1088
show_lsa_external(struct top_hash_entry *he, int ospf2)
1089
{
1090
  struct ospf_lsa_ext_local rt;
1091
  char str_via[STD_ADDRESS_P_LENGTH + 8] = "";
1092
  char str_tag[16] = "";
1093

    
1094
  if (he->lsa_type == LSA_T_EXT)
1095
    he->domain = 0; /* Unmark the LSA */
1096

    
1097
  lsa_parse_ext(he, ospf2, &rt);
1098

    
1099
  if (rt.fbit)
1100
    bsprintf(str_via, " via %I", rt.fwaddr);
1101

    
1102
  if (rt.tag)
1103
    bsprintf(str_tag, " tag %08x", rt.tag);
1104

    
1105
  cli_msg(-1016, "\t\t%s %I/%d metric%s %u%s%s",
1106
          (he->lsa_type == LSA_T_NSSA) ? "nssa-ext" : "external",
1107
          rt.ip, rt.pxlen, rt.ebit ? "2" : "", rt.metric, str_via, str_tag);
1108
}
1109

    
1110
static inline void
1111
show_lsa_prefix(struct top_hash_entry *he, struct top_hash_entry *cnode)
1112
{
1113
  struct ospf_lsa_prefix *px = he->lsa_body;
1114
  ip_addr pxa;
1115
  int pxlen;
1116
  u8 pxopts;
1117
  u16 metric;
1118
  u32 *buf;
1119
  int i;
1120

    
1121
  /* We check whether given prefix-LSA is related to the current node */
1122
  if ((px->ref_type != cnode->lsa.type_raw) || (px->ref_rt != cnode->lsa.rt))
1123
    return;
1124

    
1125
  if ((px->ref_type == LSA_T_RT) && (px->ref_id != 0))
1126
    return;
1127

    
1128
  if ((px->ref_type == LSA_T_NET) && (px->ref_id != cnode->lsa.id))
1129
    return;
1130

    
1131
  buf = px->rest;
1132
  for (i = 0; i < px->pxcount; i++)
1133
    {
1134
      buf = lsa_get_ipv6_prefix(buf, &pxa, &pxlen, &pxopts, &metric);
1135

    
1136
      if (px->ref_type == LSA_T_RT)
1137
        cli_msg(-1016, "\t\tstubnet %I/%d metric %u", pxa, pxlen, metric);
1138
      else
1139
        cli_msg(-1016, "\t\taddress %I/%d", pxa, pxlen);
1140
    }
1141
}
1142

    
1143
void
1144
ospf_sh_state(struct proto *P, int verbose, int reachable)
1145
{
1146
  struct ospf_proto *p = (struct ospf_proto *) P;
1147
  int ospf2 = ospf_is_v2(p);
1148
  uint i, ix, j1, jx;
1149
  u32 last_area = 0xFFFFFFFF;
1150

    
1151
  if (p->p.proto_state != PS_UP)
1152
  {
1153
    cli_msg(-1016, "%s: is not up", p->p.name);
1154
    cli_msg(0, "");
1155
    return;
1156
  }
1157

    
1158
  /* We store interesting area-scoped LSAs in array hea and
1159
     global-scoped (LSA_T_EXT) LSAs in array hex */
1160

    
1161
  int num = p->gr->hash_entries;
1162
  struct top_hash_entry *hea[num];
1163
  struct top_hash_entry *hex[verbose ? num : 0];
1164
  struct top_hash_entry *he;
1165
  struct top_hash_entry *cnode = NULL;
1166

    
1167
  j1 = jx = 0;
1168
  WALK_SLIST(he, p->lsal)
1169
  {
1170
    int accept;
1171

    
1172
    if (he->lsa.age == LSA_MAXAGE)
1173
      continue;
1174

    
1175
    switch (he->lsa_type)
1176
    {
1177
    case LSA_T_RT:
1178
    case LSA_T_NET:
1179
      accept = 1;
1180
      break;
1181

    
1182
    case LSA_T_SUM_NET:
1183
    case LSA_T_SUM_RT:
1184
    case LSA_T_NSSA:
1185
    case LSA_T_PREFIX:
1186
      accept = verbose;
1187
      break;
1188

    
1189
    case LSA_T_EXT:
1190
      if (verbose)
1191
      {
1192
        he->domain = 1; /* Abuse domain field to mark the LSA */
1193
        hex[jx++] = he;
1194
      }
1195
    default:
1196
      accept = 0;
1197
    }
1198

    
1199
    if (accept)
1200
      hea[j1++] = he;
1201
  }
1202

    
1203
  ASSERT(j1 <= num && jx <= num);
1204

    
1205
  lsa_compare_ospf3 = !ospf2;
1206
  qsort(hea, j1, sizeof(struct top_hash_entry *), lsa_compare_for_state);
1207
  qsort(hex, jx, sizeof(struct top_hash_entry *), ext_compare_for_state);
1208

    
1209
  /*
1210
   * This code is a bit tricky, we have a primary LSAs (router and
1211
   * network) that are presented as a node, and secondary LSAs that
1212
   * are presented as a part of a primary node. cnode represents an
1213
   * currently opened node (whose header was presented). The LSAs are
1214
   * sorted to get secondary LSAs just after related primary LSA (if
1215
   * available). We present secondary LSAs only when related primary
1216
   * LSA is opened.
1217
   *
1218
   * AS-external LSAs are stored separately as they might be presented
1219
   * several times (for each area when related ASBR is opened). When
1220
   * the node is closed, related external routes are presented. We
1221
   * also have to take into account that in OSPFv3, there might be
1222
   * more router-LSAs and only the first should be considered as a
1223
   * primary. This is handled by not closing old router-LSA when next
1224
   * one is processed (which is not opened because there is already
1225
   * one opened).
1226
   */
1227

    
1228
  ix = 0;
1229
  for (i = 0; i < j1; i++)
1230
  {
1231
    he = hea[i];
1232

    
1233
    /* If there is no opened node, we open the LSA (if appropriate) or skip to the next one */
1234
    if (!cnode)
1235
    {
1236
      if (((he->lsa_type == LSA_T_RT) || (he->lsa_type == LSA_T_NET))
1237
          && ((he->color == INSPF) || !reachable))
1238
      {
1239
        cnode = he;
1240

    
1241
        if (he->domain != last_area)
1242
        {
1243
          cli_msg(-1016, "");
1244
          cli_msg(-1016, "area %R", he->domain);
1245
          last_area = he->domain;
1246
          ix = 0;
1247
        }
1248
      }
1249
      else
1250
        continue;
1251
    }
1252

    
1253
    ASSERT(cnode && (he->domain == last_area) && (he->lsa.rt == cnode->lsa.rt));
1254

    
1255
    switch (he->lsa_type)
1256
    {
1257
    case LSA_T_RT:
1258
      if (he->lsa.id == cnode->lsa.id)
1259
        show_lsa_router(p, he, verbose);
1260
      break;
1261

    
1262
    case LSA_T_NET:
1263
      show_lsa_network(he, ospf2);
1264
      break;
1265

    
1266
    case LSA_T_SUM_NET:
1267
      if (cnode->lsa_type == LSA_T_RT)
1268
        show_lsa_sum_net(he, ospf2);
1269
      break;
1270

    
1271
    case LSA_T_SUM_RT:
1272
      if (cnode->lsa_type == LSA_T_RT)
1273
        show_lsa_sum_rt(he, ospf2);
1274
      break;
1275

    
1276
    case LSA_T_EXT:
1277
    case LSA_T_NSSA:
1278
      show_lsa_external(he, ospf2);
1279
      break;
1280

    
1281
    case LSA_T_PREFIX:
1282
      show_lsa_prefix(he, cnode);
1283
      break;
1284
    }
1285

    
1286
    /* In these cases, we close the current node */
1287
    if ((i+1 == j1)
1288
        || (hea[i+1]->domain != last_area)
1289
        || (hea[i+1]->lsa.rt != cnode->lsa.rt)
1290
        || (hea[i+1]->lsa_type == LSA_T_NET))
1291
    {
1292
      while ((ix < jx) && (hex[ix]->lsa.rt < cnode->lsa.rt))
1293
        ix++;
1294

    
1295
      while ((ix < jx) && (hex[ix]->lsa.rt == cnode->lsa.rt))
1296
        show_lsa_external(hex[ix++], ospf2);
1297

    
1298
      cnode = NULL;
1299
    }
1300
  }
1301

    
1302
  int hdr = 0;
1303
  u32 last_rt = 0xFFFFFFFF;
1304
  for (ix = 0; ix < jx; ix++)
1305
  {
1306
    he = hex[ix];
1307

    
1308
    /* If it is still marked, we show it now. */
1309
    if (he->domain)
1310
    {
1311
      he->domain = 0;
1312

    
1313
      if ((he->color != INSPF) && reachable)
1314
        continue;
1315

    
1316
      if (!hdr)
1317
      {
1318
        cli_msg(-1016, "");
1319
        cli_msg(-1016, "other ASBRs");
1320
        hdr = 1;
1321
      }
1322

    
1323
      if (he->lsa.rt != last_rt)
1324
      {
1325
        cli_msg(-1016, "");
1326
        cli_msg(-1016, "\trouter %R", he->lsa.rt);
1327
        last_rt = he->lsa.rt;
1328
      }
1329

    
1330
      show_lsa_external(he, ospf2);
1331
    }
1332
  }
1333

    
1334
  cli_msg(0, "");
1335
}
1336

    
1337

    
1338
static int
1339
lsa_compare_for_lsadb(const void *p1, const void *p2)
1340
{
1341
  struct top_hash_entry * he1 = * (struct top_hash_entry **) p1;
1342
  struct top_hash_entry * he2 = * (struct top_hash_entry **) p2;
1343
  struct ospf_lsa_header *lsa1 = &(he1->lsa);
1344
  struct ospf_lsa_header *lsa2 = &(he2->lsa);
1345
  int sc1 = LSA_SCOPE(he1->lsa_type);
1346
  int sc2 = LSA_SCOPE(he2->lsa_type);
1347

    
1348
  if (sc1 != sc2)
1349
    return sc2 - sc1;
1350

    
1351
  if (he1->domain != he2->domain)
1352
    return he1->domain - he2->domain;
1353

    
1354
  if (lsa1->rt != lsa2->rt)
1355
    return lsa1->rt - lsa2->rt;
1356

    
1357
  if (lsa1->id != lsa2->id)
1358
    return lsa1->id - lsa2->id;
1359

    
1360
  if (he1->lsa_type != he2->lsa_type)
1361
    return he1->lsa_type - he2->lsa_type;
1362

    
1363
  return lsa1->sn - lsa2->sn;
1364
}
1365

    
1366
void
1367
ospf_sh_lsadb(struct lsadb_show_data *ld)
1368
{
1369
  struct ospf_proto *p = (struct ospf_proto *) proto_get_named(ld->name, &proto_ospf);
1370
  uint num = p->gr->hash_entries;
1371
  uint i, j;
1372
  int last_dscope = -1;
1373
  u32 last_domain = 0;
1374
  u16 type_mask = ospf_is_v2(p) ?  0x00ff : 0xffff;        /* see lsa_etype() */
1375

    
1376
  if (p->p.proto_state != PS_UP)
1377
  {
1378
    cli_msg(-1017, "%s: is not up", p->p.name);
1379
    cli_msg(0, "");
1380
    return;
1381
  }
1382

    
1383
  if (ld->router == SH_ROUTER_SELF)
1384
    ld->router = p->router_id;
1385

    
1386
  struct top_hash_entry *hea[num];
1387
  struct top_hash_entry *he;
1388

    
1389
  j = 0;
1390
  WALK_SLIST(he, p->lsal)
1391
    if (he->lsa_body)
1392
      hea[j++] = he;
1393

    
1394
  ASSERT(j <= num);
1395

    
1396
  qsort(hea, j, sizeof(struct top_hash_entry *), lsa_compare_for_lsadb);
1397

    
1398
  for (i = 0; i < j; i++)
1399
  {
1400
    struct ospf_lsa_header *lsa = &(hea[i]->lsa);
1401
    u16 lsa_type = lsa->type_raw & type_mask;
1402
    u16 dscope = LSA_SCOPE(hea[i]->lsa_type);
1403

    
1404
    /* Hack: 1 is used for LSA_SCOPE_LINK, fixed by & 0xf000 */
1405
    if (ld->scope && (dscope != (ld->scope & 0xf000)))
1406
      continue;
1407

    
1408
    if ((ld->scope == LSA_SCOPE_AREA) && (hea[i]->domain != ld->area))
1409
      continue;
1410

    
1411
    /* For user convenience ignore high nibble */
1412
    if (ld->type && ((lsa_type & 0x0fff) != (ld->type & 0x0fff)))
1413
      continue;
1414

    
1415
    if (ld->lsid && (lsa->id != ld->lsid))
1416
      continue;
1417

    
1418
    if (ld->router && (lsa->rt != ld->router))
1419
      continue;
1420

    
1421
    if ((dscope != last_dscope) || (hea[i]->domain != last_domain))
1422
    {
1423
      cli_msg(-1017, "");
1424
      switch (dscope)
1425
      {
1426
      case LSA_SCOPE_AS:
1427
        cli_msg(-1017, "Global");
1428
        break;
1429

    
1430
      case LSA_SCOPE_AREA:
1431
        cli_msg(-1017, "Area %R", hea[i]->domain);
1432
        break;
1433

    
1434
      case LSA_SCOPE_LINK:
1435
        {
1436
          struct iface *ifa = if_find_by_index(hea[i]->domain);
1437
          cli_msg(-1017, "Link %s", (ifa != NULL) ? ifa->name : "?");
1438
        }
1439
        break;
1440
      }
1441
      cli_msg(-1017, "");
1442
      cli_msg(-1017," Type   LS ID           Router          Sequence   Age  Checksum");
1443

    
1444
      last_dscope = dscope;
1445
      last_domain = hea[i]->domain;
1446
    }
1447

    
1448
    cli_msg(-1017," %04x  %-15R %-15R  %08x %5u    %04x",
1449
            lsa_type, lsa->id, lsa->rt, lsa->sn, lsa->age, lsa->checksum);
1450
  }
1451
  cli_msg(0, "");
1452
}
1453

    
1454

    
1455
struct protocol proto_ospf = {
1456
  .name =                "OSPF",
1457
  .template =                "ospf%d",
1458
  .attr_class =                EAP_OSPF,
1459
  .preference =                DEF_PREF_OSPF,
1460
  .init =                ospf_init,
1461
  .dump =                ospf_dump,
1462
  .start =                ospf_start,
1463
  .shutdown =                ospf_shutdown,
1464
  .reconfigure =        ospf_reconfigure,
1465
  .get_status =                ospf_get_status,
1466
  .get_attr =                ospf_get_attr,
1467
  .get_route_info =        ospf_get_route_info
1468
};