Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (38 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

    
92
#include <stdlib.h>
93
#include "ospf.h"
94

    
95
static int ospf_import_control(struct proto *P, rte **new, ea_list **attrs, struct linpool *pool);
96
static struct ea_list *ospf_make_tmp_attrs(struct rte *rt, struct linpool *pool);
97
static void ospf_store_tmp_attrs(struct rte *rt, struct ea_list *attrs);
98
static int ospf_reload_routes(struct proto *P);
99
static int ospf_rte_better(struct rte *new, struct rte *old);
100
static int ospf_rte_same(struct rte *new, struct rte *old);
101
static void ospf_disp(timer *timer);
102

    
103
static void
104
ospf_area_initfib(struct fib_node *fn)
105
{
106
  struct area_net *an = (struct area_net *) fn;
107
  an->hidden = 0;
108
  an->active = 0;
109
}
110

    
111
static void
112
add_area_nets(struct ospf_area *oa, struct ospf_area_config *ac)
113
{
114
  struct ospf_proto *p = oa->po;
115
  struct area_net_config *anc;
116
  struct area_net *an;
117

    
118
  fib_init(&oa->net_fib, p->p.pool, sizeof(struct area_net), 0, ospf_area_initfib);
119
  fib_init(&oa->enet_fib, p->p.pool, sizeof(struct area_net), 0, ospf_area_initfib);
120

    
121
  WALK_LIST(anc, ac->net_list)
122
  {
123
    an = (struct area_net *) fib_get(&oa->net_fib, &anc->px.addr, anc->px.len);
124
    an->hidden = anc->hidden;
125
  }
126

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

    
135
static void
136
ospf_area_add(struct ospf_proto *p, struct ospf_area_config *ac)
137
{
138
  struct ospf_area *oa;
139

    
140
  OSPF_TRACE(D_EVENTS, "Adding area %R", ac->areaid);
141

    
142
  oa = mb_allocz(p->p.pool, sizeof(struct ospf_area));
143
  add_tail(&p->area_list, NODE oa);
144
  p->areano++;
145

    
146
  oa->ac = ac;
147
  oa->areaid = ac->areaid;
148
  oa->rt = NULL;
149
  oa->po = p;
150
  fib_init(&oa->rtr, p->p.pool, sizeof(ort), 0, ospf_rt_initort);
151
  add_area_nets(oa, ac);
152

    
153
  if (oa->areaid == 0)
154
    p->backbone = oa;
155

    
156
  if (ospf_is_v2(p))
157
    oa->options = ac->type;
158
  else
159
    oa->options = ac->type | OPT_V6 | (p->stub_router ? 0 : OPT_R);
160

    
161
  ospf_notify_rt_lsa(oa);
162
}
163

    
164
static void
165
ospf_flush_area(struct ospf_proto *p, u32 areaid)
166
{
167
  struct top_hash_entry *en;
168

    
169
  WALK_SLIST(en, p->lsal)
170
    if ((LSA_SCOPE(en->lsa_type) == LSA_SCOPE_AREA) && (en->domain == areaid))
171
      ospf_flush_lsa(p, en);
172
}
173

    
174
static void
175
ospf_area_remove(struct ospf_area *oa)
176
{
177
  struct ospf_proto *p = oa->po;
178
  OSPF_TRACE(D_EVENTS, "Removing area %R", oa->areaid);
179

    
180
  /* We suppose that interfaces are already removed */
181
  ospf_flush_area(p, oa->areaid);
182
 
183
  fib_free(&oa->rtr);
184
  fib_free(&oa->net_fib);
185
  fib_free(&oa->enet_fib);
186

    
187
  if (oa->translator_timer)
188
    rfree(oa->translator_timer);
189

    
190
  p->areano--;
191
  rem_node(NODE oa);
192
  mb_free(oa);
193
}
194

    
195

    
196
struct ospf_area *
197
ospf_find_area(struct ospf_proto *p, u32 aid)
198
{
199
  struct ospf_area *oa;
200
  WALK_LIST(oa, p->area_list)
201
    if (((struct ospf_area *) oa)->areaid == aid)
202
      return oa;
203
  return NULL;
204
}
205

    
206
static struct ospf_iface *
207
ospf_find_vlink(struct ospf_proto *p, u32 voa, u32 vid)
208
{
209
  struct ospf_iface *ifa;
210
  WALK_LIST(ifa, p->iface_list) 
211
    if ((ifa->type == OSPF_IT_VLINK) && (ifa->voa->areaid == voa) && (ifa->vid == vid))
212
      return ifa;
213
  return NULL;
214
}
215

    
216
static int
217
ospf_start(struct proto *P)
218
{
219
  struct ospf_proto *p = (struct ospf_proto *) P;
220
  struct ospf_config *c = (struct ospf_config *) (P->cf);
221
  struct ospf_area_config *ac;
222

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

    
248
  WALK_LIST(ac, c->area_list)
249
    ospf_area_add(p, ac);
250

    
251
  if (c->abr)
252
    ospf_open_vlink_sk(p);
253

    
254
  /* Add all virtual links */
255
  struct ospf_iface_patt *ic;
256
  WALK_LIST(ic, c->vlink_list)
257
    ospf_iface_new_vlink(p, ic);
258

    
259
  return PS_UP;
260
}
261

    
262
static void
263
ospf_dump(struct proto *P)
264
{
265
  struct ospf_proto *p = (struct ospf_proto *) P;
266
  struct ospf_iface *ifa;
267
  struct ospf_neighbor *n;
268

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

    
271
  WALK_LIST(ifa, p->iface_list)
272
  {
273
    OSPF_TRACE(D_EVENTS, "Interface: %s", ifa->ifname);
274
    OSPF_TRACE(D_EVENTS, "state: %u", ifa->state);
275
    OSPF_TRACE(D_EVENTS, "DR:  %R", ifa->drid);
276
    OSPF_TRACE(D_EVENTS, "BDR: %R", ifa->bdrid);
277
    WALK_LIST(n, ifa->neigh_list)
278
    {
279
      OSPF_TRACE(D_EVENTS, "  neighbor %R in state %u", n->rid, n->state);
280
    }
281
  }
282

    
283
  /*
284
  OSPF_TRACE(D_EVENTS, "LSA graph dump start:");
285
  ospf_top_dump(p->gr, p);
286
  OSPF_TRACE(D_EVENTS, "LSA graph dump finished");
287
  */
288
  neigh_dump_all();
289
}
290

    
291
static struct proto *
292
ospf_init(struct proto_config *c)
293
{
294
  struct ospf_config *oc = (struct ospf_config *) c;  
295
  struct proto *P = proto_new(c, sizeof(struct ospf_proto));
296

    
297
  P->accept_ra_types = RA_OPTIMAL;
298
  P->rt_notify = ospf_rt_notify;
299
  P->if_notify = ospf_if_notify;
300
  P->ifa_notify = oc->ospf2 ? ospf_ifa_notify2 : ospf_ifa_notify3;
301
  P->import_control = ospf_import_control;
302
  P->reload_routes = ospf_reload_routes;
303
  P->make_tmp_attrs = ospf_make_tmp_attrs;
304
  P->store_tmp_attrs = ospf_store_tmp_attrs;
305
  P->rte_better = ospf_rte_better;
306
  P->rte_same = ospf_rte_same;
307

    
308
  return P;
309
}
310

    
311
/* If new is better return 1 */
312
static int
313
ospf_rte_better(struct rte *new, struct rte *old)
314
{
315
  if (new->u.ospf.metric1 == LSINFINITY)
316
    return 0;
317

    
318
  if(new->attrs->source < old->attrs->source) return 1;
319
  if(new->attrs->source > old->attrs->source) return 0;
320

    
321
  if(new->attrs->source == RTS_OSPF_EXT2)
322
  {
323
    if(new->u.ospf.metric2 < old->u.ospf.metric2) return 1;
324
    if(new->u.ospf.metric2 > old->u.ospf.metric2) return 0;
325
  }
326

    
327
  if (new->u.ospf.metric1 < old->u.ospf.metric1)
328
    return 1;
329

    
330
  return 0;                        /* Old is shorter or same */
331
}
332

    
333
static int
334
ospf_rte_same(struct rte *new, struct rte *old)
335
{
336
  /* new->attrs == old->attrs always */
337
  return
338
    new->u.ospf.metric1 == old->u.ospf.metric1 &&
339
    new->u.ospf.metric2 == old->u.ospf.metric2 &&
340
    new->u.ospf.tag == old->u.ospf.tag &&
341
    new->u.ospf.router_id == old->u.ospf.router_id;
342
}
343

    
344
static ea_list *
345
ospf_build_attrs(ea_list * next, struct linpool *pool, u32 m1, u32 m2,
346
                 u32 tag, u32 rid)
347
{
348
  struct ea_list *l =
349
    lp_alloc(pool, sizeof(struct ea_list) + 4 * sizeof(eattr));
350

    
351
  l->next = next;
352
  l->flags = EALF_SORTED;
353
  l->count = 4;
354
  l->attrs[0].id = EA_OSPF_METRIC1;
355
  l->attrs[0].flags = 0;
356
  l->attrs[0].type = EAF_TYPE_INT | EAF_TEMP;
357
  l->attrs[0].u.data = m1;
358
  l->attrs[1].id = EA_OSPF_METRIC2;
359
  l->attrs[1].flags = 0;
360
  l->attrs[1].type = EAF_TYPE_INT | EAF_TEMP;
361
  l->attrs[1].u.data = m2;
362
  l->attrs[2].id = EA_OSPF_TAG;
363
  l->attrs[2].flags = 0;
364
  l->attrs[2].type = EAF_TYPE_INT | EAF_TEMP;
365
  l->attrs[2].u.data = tag;
366
  l->attrs[3].id = EA_OSPF_ROUTER_ID;
367
  l->attrs[3].flags = 0;
368
  l->attrs[3].type = EAF_TYPE_ROUTER_ID | EAF_TEMP;
369
  l->attrs[3].u.data = rid;
370
  return l;
371
}
372

    
373

    
374
void
375
ospf_schedule_rtcalc(struct ospf_proto *p)
376
{
377
  if (p->calcrt)
378
    return;
379

    
380
  OSPF_TRACE(D_EVENTS, "Scheduling routing table calculation");
381
  p->calcrt = 1;
382
}
383

    
384
static int
385
ospf_reload_routes(struct proto *P)
386
{
387
  struct ospf_proto *p = (struct ospf_proto *) P;
388

    
389
  if (p->calcrt != 2)
390
    OSPF_TRACE(D_EVENTS, "Scheduling routing table calculation with route reload");
391

    
392
  p->calcrt = 2;
393

    
394
  return 1;
395
}
396

    
397

    
398
/**
399
 * ospf_disp - invokes routing table calculation, aging and also area_disp()
400
 * @timer: timer usually called every @ospf_proto->tick second, @timer->data
401
 * point to @ospf_proto
402
 */
403
static void
404
ospf_disp(timer * timer)
405
{
406
  struct ospf_proto *p = timer->data;
407

    
408
  /* Originate or flush local topology LSAs */
409
  ospf_update_topology(p);
410

    
411
  /* Process LSA DB */
412
  ospf_update_lsadb(p);
413

    
414
  /* Calculate routing table */
415
  if (p->calcrt)
416
    ospf_rt_spf(p);
417
}
418

    
419

    
420
/**
421
 * ospf_import_control - accept or reject new route from nest's routing table
422
 * @P: OSPF protocol instance
423
 * @new: the new route
424
 * @attrs: list of attributes
425
 * @pool: pool for allocation of attributes
426
 *
427
 * Its quite simple. It does not accept our own routes and leaves the decision on
428
 * import to the filters.
429
 */
430
static int
431
ospf_import_control(struct proto *P, rte **new, ea_list **attrs, struct linpool *pool)
432
{
433
  struct ospf_proto *p = (struct ospf_proto *) P;
434
  struct ospf_area *oa = ospf_main_area(p);
435
  rte *e = *new;
436

    
437
  if (e->attrs->src->proto == P)
438
    return -1;                        /* Reject our own routes */
439

    
440
  if (oa_is_stub(oa))
441
    return -1;                        /* Do not export routes to stub areas */
442

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

    
446
  *attrs = ospf_build_attrs(*attrs, pool, m1, 10000, 0, 0);
447
  return 0;                        /* Leave decision to the filters */
448
}
449

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

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

    
466
/**
467
 * ospf_shutdown - Finish of OSPF instance
468
 * @P: OSPF protocol instance
469
 *
470
 * RFC does not define any action that should be taken before router
471
 * shutdown. To make my neighbors react as fast as possible, I send
472
 * them hello packet with empty neighbor list. They should start
473
 * their neighbor state machine with event %NEIGHBOR_1WAY.
474
 */
475
static int
476
ospf_shutdown(struct proto *P)
477
{
478
  struct ospf_proto *p = (struct ospf_proto *) P;
479
  struct ospf_iface *ifa;
480

    
481
  OSPF_TRACE(D_EVENTS, "Shutdown requested");
482

    
483
  /* And send to all my neighbors 1WAY */
484
  WALK_LIST(ifa, p->iface_list)
485
    ospf_iface_shutdown(ifa);
486

    
487
  /* Cleanup locked rta entries */
488
  FIB_WALK(&p->rtf, nftmp)
489
  {
490
    rta_free(((ort *) nftmp)->old_rta);
491
  }
492
  FIB_WALK_END;
493

    
494
  return PS_DOWN;
495
}
496

    
497
static void
498
ospf_get_status(struct proto *P, byte * buf)
499
{
500
  struct ospf_proto *p = (struct ospf_proto *) P;
501

    
502
  if (p->p.proto_state == PS_DOWN)
503
    buf[0] = 0;
504
  else
505
  {
506
    struct ospf_iface *ifa;
507
    struct ospf_neighbor *n;
508
    int adj = 0;
509

    
510
    WALK_LIST(ifa, p->iface_list)
511
      WALK_LIST(n, ifa->neigh_list) if (n->state == NEIGHBOR_FULL)
512
      adj = 1;
513

    
514
    if (adj == 0)
515
      strcpy(buf, "Alone");
516
    else
517
      strcpy(buf, "Running");
518
  }
519
}
520

    
521
static void
522
ospf_get_route_info(rte * rte, byte * buf, ea_list * attrs UNUSED)
523
{
524
  char *type = "<bug>";
525

    
526
  switch (rte->attrs->source)
527
  {
528
  case RTS_OSPF:
529
    type = "I";
530
    break;
531
  case RTS_OSPF_IA:
532
    type = "IA";
533
    break;
534
  case RTS_OSPF_EXT1:
535
    type = "E1";
536
    break;
537
  case RTS_OSPF_EXT2:
538
    type = "E2";
539
    break;
540
  }
541

    
542
  buf += bsprintf(buf, " %s", type);
543
  buf += bsprintf(buf, " (%d/%d", rte->pref, rte->u.ospf.metric1);
544
  if (rte->attrs->source == RTS_OSPF_EXT2)
545
    buf += bsprintf(buf, "/%d", rte->u.ospf.metric2);
546
  buf += bsprintf(buf, ")");
547
  if ((rte->attrs->source == RTS_OSPF_EXT1 || rte->attrs->source == RTS_OSPF_EXT2) && rte->u.ospf.tag)
548
  {
549
    buf += bsprintf(buf, " [%x]", rte->u.ospf.tag);
550
  }
551
  if (rte->u.ospf.router_id)
552
    buf += bsprintf(buf, " [%R]", rte->u.ospf.router_id);
553
}
554

    
555
static int
556
ospf_get_attr(eattr * a, byte * buf, int buflen UNUSED)
557
{
558
  switch (a->id)
559
  {
560
  case EA_OSPF_METRIC1:
561
    bsprintf(buf, "metric1");
562
    return GA_NAME;
563
  case EA_OSPF_METRIC2:
564
    bsprintf(buf, "metric2");
565
    return GA_NAME;
566
  case EA_OSPF_TAG:
567
    bsprintf(buf, "tag: 0x%08x", a->u.data);
568
    return GA_FULL;
569
  case EA_OSPF_ROUTER_ID:
570
    bsprintf(buf, "router_id");
571
    return GA_NAME;
572
  default:
573
    return GA_UNKNOWN;
574
  }
575
}
576

    
577
static void
578
ospf_area_reconfigure(struct ospf_area *oa, struct ospf_area_config *nac)
579
{
580
  struct ospf_proto *p = oa->po;
581
  struct ospf_area_config *oac = oa->ac;
582
  struct ospf_iface *ifa;
583

    
584
  oa->ac = nac;
585

    
586
  if (ospf_is_v2(p))
587
    oa->options = nac->type;
588
  else
589
    oa->options = nac->type | OPT_V6 | (p->stub_router ? 0 : OPT_R);
590

    
591
  if (nac->type != oac->type)
592
  {
593
    /* Force restart of area interfaces */
594
    WALK_LIST(ifa, p->iface_list)
595
      if (ifa->oa == oa)
596
        ifa->marked = 2;
597
  }
598

    
599
  /* Handle net_list */
600
  fib_free(&oa->net_fib);
601
  fib_free(&oa->enet_fib);
602
  add_area_nets(oa, nac);
603

    
604
  /* No need to handle stubnet_list */
605

    
606
  oa->marked = 0;
607
  ospf_notify_rt_lsa(oa);
608
}
609

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

    
631
  if (proto_get_router_id(c) != p->router_id)
632
    return 0;
633

    
634
  if (p->rfc1583 != new->rfc1583)
635
    return 0;
636

    
637
  if (old->abr != new->abr)
638
    return 0;
639

    
640
  p->stub_router = new->stub_router;
641
  p->merge_external = new->merge_external;
642
  p->asbr = new->asbr;
643
  p->ecmp = new->ecmp;
644
  p->tick = new->tick;
645
  p->disp_timer->recurrent = p->tick;
646
  tm_start(p->disp_timer, 1);
647

    
648
  /* Mark all areas and ifaces */
649
  WALK_LIST(oa, p->area_list)
650
    oa->marked = 1;
651

    
652
  WALK_LIST(ifa, p->iface_list)
653
    ifa->marked = 1;
654

    
655
  /* Add and update areas */
656
  WALK_LIST(nac, new->area_list)
657
  {
658
    oa = ospf_find_area(p, nac->areaid);
659
    if (oa)
660
      ospf_area_reconfigure(oa, nac);
661
    else
662
      ospf_area_add(p, nac);
663
  }
664

    
665
  /* Add and update interfaces */
666
  ospf_reconfigure_ifaces(p);
667

    
668
  /* Add and update vlinks */
669
  WALK_LIST(ip, new->vlink_list)
670
  {
671
    ifa = ospf_find_vlink(p, ip->voa, ip->vid);
672
    if (ifa)
673
      ospf_iface_reconfigure(ifa, ip);
674
    else
675
      ospf_iface_new_vlink(p, ip);
676
  }
677

    
678
  /* Delete remaining ifaces and areas */
679
  WALK_LIST_DELSAFE(ifa, ifx, p->iface_list)
680
    if (ifa->marked)
681
    {
682
      ospf_iface_shutdown(ifa);
683
      ospf_iface_remove(ifa);
684
    }
685

    
686
  WALK_LIST_DELSAFE(oa, oax, p->area_list)
687
    if (oa->marked)
688
      ospf_area_remove(oa);
689

    
690
  ospf_schedule_rtcalc(p);
691
  
692
  return 1;
693
}
694

    
695

    
696
void
697
ospf_sh_neigh(struct proto *P, char *iff)
698
{
699
  struct ospf_proto *p = (struct ospf_proto *) P;
700
  struct ospf_iface *ifa = NULL;
701
  struct ospf_neighbor *n;
702

    
703
  if (p->p.proto_state != PS_UP)
704
  {
705
    cli_msg(-1013, "%s: is not up", p->p.name);
706
    cli_msg(0, "");
707
    return;
708
  }
709

    
710
  cli_msg(-1013, "%s:", p->p.name);
711
  cli_msg(-1013, "%-12s\t%3s\t%-15s\t%-5s\t%-10s %-12s", "Router ID", "Pri",
712
          "     State", "DTime", "Interface", "Router IP");
713
  WALK_LIST(ifa, p->iface_list)
714
    if ((iff == NULL) || patmatch(iff, ifa->ifname))
715
      WALK_LIST(n, ifa->neigh_list)
716
        ospf_sh_neigh_info(n);
717
  cli_msg(0, "");
718
}
719

    
720
void
721
ospf_sh(struct proto *P)
722
{
723
  struct ospf_proto *p = (struct ospf_proto *) P;
724
  struct ospf_area *oa;
725
  struct ospf_iface *ifa;
726
  struct ospf_neighbor *n;
727
  int ifano, nno, adjno, firstfib;
728
  struct area_net *anet;
729

    
730
  if (p->p.proto_state != PS_UP)
731
  {
732
    cli_msg(-1014, "%s: is not up", p->p.name);
733
    cli_msg(0, "");
734
    return;
735
  }
736

    
737
  cli_msg(-1014, "%s:", p->p.name);
738
  cli_msg(-1014, "RFC1583 compatibility: %s", (p->rfc1583 ? "enable" : "disabled"));
739
  cli_msg(-1014, "Stub router: %s", (p->stub_router ? "Yes" : "No"));
740
  cli_msg(-1014, "RT scheduler tick: %d", p->tick);
741
  cli_msg(-1014, "Number of areas: %u", p->areano);
742
  cli_msg(-1014, "Number of LSAs in DB:\t%u", p->gr->hash_entries);
743

    
744
  WALK_LIST(oa, p->area_list)
745
  {
746
    cli_msg(-1014, "\tArea: %R (%u) %s", oa->areaid, oa->areaid,
747
            oa->areaid == 0 ? "[BACKBONE]" : "");
748
    ifano = 0;
749
    nno = 0;
750
    adjno = 0;
751
    WALK_LIST(ifa, p->iface_list)
752
    {
753
      if (oa == ifa->oa)
754
      {
755
        ifano++;
756
        WALK_LIST(n, ifa->neigh_list)
757
        {
758
          nno++;
759
          if (n->state == NEIGHBOR_FULL)
760
            adjno++;
761
        }
762
      }
763
    }
764

    
765
    cli_msg(-1014, "\t\tStub:\t%s", oa_is_stub(oa) ? "Yes" : "No");
766
    cli_msg(-1014, "\t\tNSSA:\t%s", oa_is_nssa(oa) ? "Yes" : "No");
767
    cli_msg(-1014, "\t\tTransit:\t%s", oa->trcap ? "Yes" : "No");
768

    
769
    if (oa_is_nssa(oa))
770
      cli_msg(-1014, "\t\tNSSA translation:\t%s%s", oa->translate ? "Yes" : "No",
771
              oa->translate == TRANS_WAIT ? " (run down)" : "");
772
    cli_msg(-1014, "\t\tNumber of interfaces:\t%u", ifano);
773
    cli_msg(-1014, "\t\tNumber of neighbors:\t%u", nno);
774
    cli_msg(-1014, "\t\tNumber of adjacent neighbors:\t%u", adjno);
775

    
776
    firstfib = 1;
777
    FIB_WALK(&oa->net_fib, nftmp)
778
    {
779
      anet = (struct area_net *) nftmp;
780
      if(firstfib)
781
      {
782
        cli_msg(-1014, "\t\tArea networks:");
783
        firstfib = 0;
784
      }
785
      cli_msg(-1014, "\t\t\t%1I/%u\t%s\t%s", anet->fn.prefix, anet->fn.pxlen,
786
                anet->hidden ? "Hidden" : "Advertise", anet->active ? "Active" : "");
787
    }
788
    FIB_WALK_END;
789

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

    
804
  }
805
  cli_msg(0, "");
806
}
807

    
808
void
809
ospf_sh_iface(struct proto *P, char *iff)
810
{
811
  struct ospf_proto *p = (struct ospf_proto *) P;
812
  struct ospf_iface *ifa = NULL;
813

    
814
  if (p->p.proto_state != PS_UP)
815
  {
816
    cli_msg(-1015, "%s: is not up", p->p.name);
817
    cli_msg(0, "");
818
    return;
819
  }
820

    
821
  cli_msg(-1015, "%s:", p->p.name);
822
  WALK_LIST(ifa, p->iface_list)
823
    if ((iff == NULL) || patmatch(iff, ifa->ifname))
824
      ospf_iface_info(ifa);
825
  cli_msg(0, "");
826
}
827

    
828
/* lsa_compare_for_state() - Compare function for 'show ospf state'
829
 *
830
 * First we want to separate network-LSAs and other LSAs (because network-LSAs
831
 * will be presented as network nodes and other LSAs together as router nodes)
832
 * Network-LSAs are sorted according to network prefix, other LSAs are sorted
833
 * according to originating router id (to get all LSA needed to represent one
834
 * router node together). Then, according to LSA type, ID and age.
835
 *
836
 * For OSPFv3, we have to handle also Prefix-LSAs. We would like to put each
837
 * immediately after the referenced LSA. We will make faked LSA based on ref_
838
 * values
839
 */
840

    
841
static struct ospf_lsa_header *
842
fake_lsa_from_prefix_lsa(struct ospf_lsa_header *dst, struct ospf_lsa_header *src,
843
                         struct ospf_lsa_prefix *px)
844
{
845
  dst->age = src->age;
846
  dst->type_raw = px->ref_type;
847
  dst->id = px->ref_id;
848
  dst->rt = px->ref_rt;
849
  dst->sn = src->sn;
850

    
851
  return dst;
852
}
853

    
854

    
855
static int lsa_compare_ospf3;
856

    
857
static int
858
lsa_compare_for_state(const void *p1, const void *p2)
859
{
860
  struct top_hash_entry *he1 = * (struct top_hash_entry **) p1;
861
  struct top_hash_entry *he2 = * (struct top_hash_entry **) p2;
862
  struct ospf_lsa_header *lsa1 = &(he1->lsa);
863
  struct ospf_lsa_header *lsa2 = &(he2->lsa);
864
  struct ospf_lsa_header lsatmp1, lsatmp2;
865
  u16 lsa1_type = he1->lsa_type;
866
  u16 lsa2_type = he2->lsa_type;
867

    
868
  if (he1->domain < he2->domain)
869
    return -1;
870
  if (he1->domain > he2->domain)
871
    return 1;
872

    
873

    
874
  /* px1 or px2 assumes OSPFv3 */
875
  int px1 = (lsa1_type == LSA_T_PREFIX);
876
  int px2 = (lsa2_type == LSA_T_PREFIX);
877

    
878
  if (px1)
879
  {
880
    lsa1 = fake_lsa_from_prefix_lsa(&lsatmp1, lsa1, he1->lsa_body);
881
    lsa1_type = lsa1->type_raw;        /* FIXME: handle unknown ref_type */
882
  }
883

    
884
  if (px2)
885
  {
886
    lsa2 = fake_lsa_from_prefix_lsa(&lsatmp2, lsa2, he2->lsa_body);
887
    lsa2_type = lsa2->type_raw;
888
  }
889

    
890

    
891
  int nt1 = (lsa1_type == LSA_T_NET);
892
  int nt2 = (lsa2_type == LSA_T_NET);
893

    
894
  if (nt1 != nt2)
895
    return nt1 - nt2;
896

    
897
  if (nt1)
898
  {
899
    /* In OSPFv3, networks are named based on ID of DR */
900
    if (lsa_compare_ospf3)
901
    {
902
      if (lsa1->rt < lsa2->rt)
903
        return -1;
904
      if (lsa1->rt > lsa2->rt)
905
        return 1;
906
    }
907

    
908
    /* For OSPFv2, this is IP of the network,
909
       for OSPFv3, this is interface ID */
910
    if (lsa1->id < lsa2->id)
911
      return -1;
912
    if (lsa1->id > lsa2->id)
913
      return 1;
914

    
915
    if (px1 != px2)
916
      return px1 - px2;
917

    
918
    return lsa1->sn - lsa2->sn;
919
  }
920
  else 
921
  {
922
    if (lsa1->rt < lsa2->rt)
923
      return -1;
924
    if (lsa1->rt > lsa2->rt)
925
      return 1;
926

    
927
    if (lsa1_type < lsa2_type)
928
      return -1;
929
    if (lsa1_type > lsa2_type)
930
      return 1;
931

    
932
    if (lsa1->id < lsa2->id)
933
      return -1;
934
    if (lsa1->id > lsa2->id)
935
      return 1;
936

    
937
    if (px1 != px2)
938
      return px1 - px2;
939
  
940
    return lsa1->sn - lsa2->sn;
941
  }
942
}
943

    
944
static int
945
ext_compare_for_state(const void *p1, const void *p2)
946
{
947
  struct top_hash_entry * he1 = * (struct top_hash_entry **) p1;
948
  struct top_hash_entry * he2 = * (struct top_hash_entry **) p2;
949
  struct ospf_lsa_header *lsa1 = &(he1->lsa);
950
  struct ospf_lsa_header *lsa2 = &(he2->lsa);
951

    
952
  if (lsa1->rt < lsa2->rt)
953
    return -1;
954
  if (lsa1->rt > lsa2->rt)
955
    return 1;
956

    
957
  if (lsa1->id < lsa2->id)
958
    return -1;
959
  if (lsa1->id > lsa2->id)
960
    return 1;
961

    
962
  return lsa1->sn - lsa2->sn;
963
}
964

    
965
static inline void
966
show_lsa_distance(struct top_hash_entry *he)
967
{
968
  if (he->color == INSPF)
969
    cli_msg(-1016, "\t\tdistance %u", he->dist);
970
  else
971
    cli_msg(-1016, "\t\tunreachable");
972
}
973

    
974
static inline void
975
show_lsa_router(struct ospf_proto *p, struct top_hash_entry *he, int verbose)
976
{
977
  struct ospf_lsa_rt_walk rtl;
978

    
979
  cli_msg(-1016, "");
980
  cli_msg(-1016, "\trouter %R", he->lsa.rt);
981
  show_lsa_distance(he);
982

    
983
  lsa_walk_rt_init(p, he, &rtl);
984
  while (lsa_walk_rt(&rtl))
985
    if (rtl.type == LSART_VLNK)
986
      cli_msg(-1016, "\t\tvlink %R metric %u", rtl.id, rtl.metric);
987

    
988
  lsa_walk_rt_init(p, he, &rtl);
989
  while (lsa_walk_rt(&rtl))
990
    if (rtl.type == LSART_PTP)
991
      cli_msg(-1016, "\t\trouter %R metric %u", rtl.id, rtl.metric);
992

    
993
  lsa_walk_rt_init(p, he, &rtl);
994
  while (lsa_walk_rt(&rtl))
995
    if (rtl.type == LSART_NET)
996
    {
997
      if (ospf_is_v2(p))
998
      {
999
        /* In OSPFv2, we try to find network-LSA to get prefix/pxlen */
1000
        struct top_hash_entry *net_he = ospf_hash_find_net2(p->gr, he->domain, rtl.id);
1001

    
1002
        if (net_he && (net_he->lsa.age < LSA_MAXAGE))
1003
        {
1004
          struct ospf_lsa_header *net_lsa = &(net_he->lsa);
1005
          struct ospf_lsa_net *net_ln = net_he->lsa_body;
1006

    
1007
          cli_msg(-1016, "\t\tnetwork %I/%d metric %u", 
1008
                  ipa_from_u32(net_lsa->id & net_ln->optx),
1009
                  u32_masklen(net_ln->optx), rtl.metric);
1010
        }
1011
        else
1012
          cli_msg(-1016, "\t\tnetwork [%R] metric %u", rtl.id, rtl.metric);
1013
      }
1014
      else
1015
        cli_msg(-1016, "\t\tnetwork [%R-%u] metric %u", rtl.id, rtl.nif, rtl.metric);
1016
    }
1017

    
1018
  if (ospf_is_v2(p) && verbose)
1019
  {
1020
    lsa_walk_rt_init(p, he, &rtl);
1021
    while (lsa_walk_rt(&rtl))
1022
      if (rtl.type == LSART_STUB)
1023
        cli_msg(-1016, "\t\tstubnet %I/%d metric %u",
1024
                ipa_from_u32(rtl.id), u32_masklen(rtl.data), rtl.metric);
1025
  }
1026
}
1027

    
1028
static inline void
1029
show_lsa_network(struct top_hash_entry *he, int ospf2)
1030
{
1031
  struct ospf_lsa_header *lsa = &(he->lsa);
1032
  struct ospf_lsa_net *ln = he->lsa_body;
1033
  u32 i;
1034

    
1035
  if (ospf2)
1036
  {
1037
    cli_msg(-1016, "");
1038
    cli_msg(-1016, "\tnetwork %I/%d", ipa_from_u32(lsa->id & ln->optx), u32_masklen(ln->optx));
1039
    cli_msg(-1016, "\t\tdr %R", lsa->rt);
1040
  }
1041
  else
1042
  {
1043
    cli_msg(-1016, "");
1044
    cli_msg(-1016, "\tnetwork [%R-%u]", lsa->rt, lsa->id);
1045
  }
1046

    
1047
  show_lsa_distance(he);
1048

    
1049
  for (i = 0; i < lsa_net_count(lsa); i++)
1050
    cli_msg(-1016, "\t\trouter %R", ln->routers[i]);
1051
}
1052

    
1053
static inline void
1054
show_lsa_sum_net(struct top_hash_entry *he, int ospf2)
1055
{
1056
  ip_addr ip;
1057
  int pxlen;
1058
  u8 pxopts;
1059
  u32 metric;
1060

    
1061
  lsa_parse_sum_net(he, ospf2, &ip, &pxlen, &pxopts, &metric);
1062
  cli_msg(-1016, "\t\txnetwork %I/%d metric %u", ip, pxlen, metric);
1063
}
1064

    
1065
static inline void
1066
show_lsa_sum_rt(struct top_hash_entry *he, int ospf2)
1067
{
1068
  u32 metric;
1069
  u32 dst_rid;
1070
  u32 options;
1071

    
1072
  lsa_parse_sum_rt(he, ospf2, &dst_rid, &metric, &options);
1073
  cli_msg(-1016, "\t\txrouter %R metric %u", dst_rid, metric);
1074
}
1075

    
1076

    
1077
static inline void
1078
show_lsa_external(struct top_hash_entry *he, int ospf2)
1079
{
1080
  struct ospf_lsa_ext_local rt;
1081
  char str_via[STD_ADDRESS_P_LENGTH + 8] = "";
1082
  char str_tag[16] = "";
1083

    
1084
  if (he->lsa_type == LSA_T_EXT)
1085
    he->domain = 0; /* Unmark the LSA */
1086

    
1087
  lsa_parse_ext(he, ospf2, &rt);
1088
  
1089
  if (rt.fbit)
1090
    bsprintf(str_via, " via %I", rt.fwaddr);
1091

    
1092
  if (rt.tag)
1093
    bsprintf(str_tag, " tag %08x", rt.tag);
1094

    
1095
  cli_msg(-1016, "\t\t%s %I/%d metric%s %u%s%s",
1096
          (he->lsa_type == LSA_T_NSSA) ? "nssa-ext" : "external",
1097
          rt.ip, rt.pxlen, rt.ebit ? "2" : "", rt.metric, str_via, str_tag);
1098
}
1099

    
1100
static inline void
1101
show_lsa_prefix(struct top_hash_entry *he, struct top_hash_entry *cnode)
1102
{
1103
  struct ospf_lsa_prefix *px = he->lsa_body;
1104
  ip_addr pxa;
1105
  int pxlen;
1106
  u8 pxopts;
1107
  u16 metric;
1108
  u32 *buf;
1109
  int i;
1110

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

    
1115
  if ((px->ref_type == LSA_T_RT) && (px->ref_id != 0))
1116
    return;
1117

    
1118
  if ((px->ref_type == LSA_T_NET) && (px->ref_id != cnode->lsa.id))
1119
    return;
1120

    
1121
  buf = px->rest;
1122
  for (i = 0; i < px->pxcount; i++)
1123
    {
1124
      buf = lsa_get_ipv6_prefix(buf, &pxa, &pxlen, &pxopts, &metric);
1125

    
1126
      if (px->ref_type == LSA_T_RT)
1127
        cli_msg(-1016, "\t\tstubnet %I/%d metric %u", pxa, pxlen, metric);
1128
      else
1129
        cli_msg(-1016, "\t\taddress %I/%d", pxa, pxlen);
1130
    }
1131
}
1132

    
1133
void
1134
ospf_sh_state(struct proto *P, int verbose, int reachable)
1135
{
1136
  struct ospf_proto *p = (struct ospf_proto *) P;
1137
  int ospf2 = ospf_is_v2(p);
1138
  uint i, ix, j1, jx;
1139
  u32 last_area = 0xFFFFFFFF;
1140

    
1141
  if (p->p.proto_state != PS_UP)
1142
  {
1143
    cli_msg(-1016, "%s: is not up", p->p.name);
1144
    cli_msg(0, "");
1145
    return;
1146
  }
1147

    
1148
  /* We store interesting area-scoped LSAs in array hea and 
1149
     global-scoped (LSA_T_EXT) LSAs in array hex */
1150

    
1151
  int num = p->gr->hash_entries;
1152
  struct top_hash_entry *hea[num];
1153
  struct top_hash_entry *hex[verbose ? num : 0];
1154
  struct top_hash_entry *he;
1155
  struct top_hash_entry *cnode = NULL;
1156

    
1157
  j1 = jx = 0;
1158
  WALK_SLIST(he, p->lsal)
1159
  {
1160
    int accept;
1161

    
1162
    if (he->lsa.age == LSA_MAXAGE)
1163
      continue;
1164

    
1165
    switch (he->lsa_type)
1166
    {
1167
    case LSA_T_RT:
1168
    case LSA_T_NET:
1169
      accept = 1;
1170
      break;
1171

    
1172
    case LSA_T_SUM_NET:
1173
    case LSA_T_SUM_RT:
1174
    case LSA_T_NSSA:
1175
    case LSA_T_PREFIX:
1176
      accept = verbose;
1177
      break;
1178

    
1179
    case LSA_T_EXT:
1180
      if (verbose)
1181
      {
1182
        he->domain = 1; /* Abuse domain field to mark the LSA */
1183
        hex[jx++] = he;
1184
      }
1185
    default:
1186
      accept = 0;
1187
    }
1188

    
1189
    if (accept)
1190
      hea[j1++] = he;
1191
  }
1192

    
1193
  ASSERT(j1 <= num && jx <= num);
1194

    
1195
  lsa_compare_ospf3 = !ospf2;
1196
  qsort(hea, j1, sizeof(struct top_hash_entry *), lsa_compare_for_state);
1197
  qsort(hex, jx, sizeof(struct top_hash_entry *), ext_compare_for_state);
1198

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

    
1218
  ix = 0;
1219
  for (i = 0; i < j1; i++)
1220
  {
1221
    he = hea[i];
1222

    
1223
    /* If there is no opened node, we open the LSA (if appropriate) or skip to the next one */
1224
    if (!cnode)
1225
    {
1226
      if (((he->lsa_type == LSA_T_RT) || (he->lsa_type == LSA_T_NET))
1227
          && ((he->color == INSPF) || !reachable))
1228
      {
1229
        cnode = he;
1230

    
1231
        if (he->domain != last_area)
1232
        {
1233
          cli_msg(-1016, "");
1234
          cli_msg(-1016, "area %R", he->domain);
1235
          last_area = he->domain;
1236
          ix = 0;
1237
        }
1238
      }
1239
      else
1240
        continue;
1241
    }
1242

    
1243
    ASSERT(cnode && (he->domain == last_area) && (he->lsa.rt == cnode->lsa.rt));
1244

    
1245
    switch (he->lsa_type)
1246
    {
1247
    case LSA_T_RT:
1248
      if (he->lsa.id == cnode->lsa.id)
1249
        show_lsa_router(p, he, verbose);
1250
      break;
1251

    
1252
    case LSA_T_NET:
1253
      show_lsa_network(he, ospf2);
1254
      break;
1255

    
1256
    case LSA_T_SUM_NET:
1257
      if (cnode->lsa_type == LSA_T_RT)
1258
        show_lsa_sum_net(he, ospf2);
1259
      break;
1260

    
1261
    case LSA_T_SUM_RT:
1262
      if (cnode->lsa_type == LSA_T_RT)
1263
        show_lsa_sum_rt(he, ospf2);
1264
      break;
1265

    
1266
    case LSA_T_EXT:
1267
    case LSA_T_NSSA:
1268
      show_lsa_external(he, ospf2);
1269
      break;
1270

    
1271
    case LSA_T_PREFIX:
1272
      show_lsa_prefix(he, cnode);
1273
      break;
1274
    }
1275

    
1276
    /* In these cases, we close the current node */
1277
    if ((i+1 == j1)
1278
        || (hea[i+1]->domain != last_area)
1279
        || (hea[i+1]->lsa.rt != cnode->lsa.rt)
1280
        || (hea[i+1]->lsa_type == LSA_T_NET))
1281
    {
1282
      while ((ix < jx) && (hex[ix]->lsa.rt < cnode->lsa.rt))
1283
        ix++;
1284

    
1285
      while ((ix < jx) && (hex[ix]->lsa.rt == cnode->lsa.rt))
1286
        show_lsa_external(hex[ix++], ospf2);
1287

    
1288
      cnode = NULL;
1289
    }
1290
  }
1291

    
1292
  int hdr = 0;
1293
  u32 last_rt = 0xFFFFFFFF;
1294
  for (ix = 0; ix < jx; ix++)
1295
  {
1296
    he = hex[ix];
1297

    
1298
    /* If it is still marked, we show it now. */
1299
    if (he->domain)
1300
    {
1301
      he->domain = 0;
1302

    
1303
      if ((he->color != INSPF) && reachable)
1304
        continue;
1305

    
1306
      if (!hdr)
1307
      {
1308
        cli_msg(-1016, "");
1309
        cli_msg(-1016, "other ASBRs");
1310
        hdr = 1;
1311
      }
1312

    
1313
      if (he->lsa.rt != last_rt)
1314
      {
1315
        cli_msg(-1016, "");
1316
        cli_msg(-1016, "\trouter %R", he->lsa.rt);
1317
        last_rt = he->lsa.rt;
1318
      }
1319

    
1320
      show_lsa_external(he, ospf2);
1321
    }
1322
  }
1323

    
1324
  cli_msg(0, "");
1325
}
1326

    
1327

    
1328
static int
1329
lsa_compare_for_lsadb(const void *p1, const void *p2)
1330
{
1331
  struct top_hash_entry * he1 = * (struct top_hash_entry **) p1;
1332
  struct top_hash_entry * he2 = * (struct top_hash_entry **) p2;
1333
  struct ospf_lsa_header *lsa1 = &(he1->lsa);
1334
  struct ospf_lsa_header *lsa2 = &(he2->lsa);
1335
  int sc1 = LSA_SCOPE(he1->lsa_type);
1336
  int sc2 = LSA_SCOPE(he2->lsa_type);
1337

    
1338
  if (sc1 != sc2)
1339
    return sc2 - sc1;
1340

    
1341
  if (he1->domain != he2->domain)
1342
    return he1->domain - he2->domain;
1343

    
1344
  if (lsa1->rt != lsa2->rt)
1345
    return lsa1->rt - lsa2->rt;
1346
  
1347
  if (lsa1->id != lsa2->id)
1348
    return lsa1->id - lsa2->id;
1349

    
1350
  if (he1->lsa_type != he2->lsa_type)
1351
    return he1->lsa_type - he2->lsa_type;
1352

    
1353
  return lsa1->sn - lsa2->sn;
1354
}
1355

    
1356
void
1357
ospf_sh_lsadb(struct lsadb_show_data *ld)
1358
{
1359
  struct ospf_proto *p = (struct ospf_proto *) proto_get_named(ld->name, &proto_ospf);
1360
  uint num = p->gr->hash_entries;
1361
  uint i, j;
1362
  int last_dscope = -1;
1363
  u32 last_domain = 0;
1364
  u16 type_mask = ospf_is_v2(p) ?  0x00ff : 0xffff;        /* see lsa_etype() */
1365

    
1366
  if (p->p.proto_state != PS_UP)
1367
  {
1368
    cli_msg(-1017, "%s: is not up", p->p.name);
1369
    cli_msg(0, "");
1370
    return;
1371
  }
1372

    
1373
  if (ld->router == SH_ROUTER_SELF)
1374
    ld->router = p->router_id;
1375

    
1376
  struct top_hash_entry *hea[num];
1377
  struct top_hash_entry *he;
1378

    
1379
  j = 0;
1380
  WALK_SLIST(he, p->lsal)
1381
    if (he->lsa_body)
1382
      hea[j++] = he;
1383

    
1384
  ASSERT(j <= num);
1385

    
1386
  qsort(hea, j, sizeof(struct top_hash_entry *), lsa_compare_for_lsadb);
1387

    
1388
  for (i = 0; i < j; i++)
1389
  {
1390
    struct ospf_lsa_header *lsa = &(hea[i]->lsa);
1391
    u16 lsa_type = lsa->type_raw & type_mask;
1392
    u16 dscope = LSA_SCOPE(hea[i]->lsa_type);
1393
    
1394
    /* Hack: 1 is used for LSA_SCOPE_LINK, fixed by & 0xf000 */
1395
    if (ld->scope && (dscope != (ld->scope & 0xf000)))
1396
      continue;
1397

    
1398
    if ((ld->scope == LSA_SCOPE_AREA) && (hea[i]->domain != ld->area))
1399
      continue;
1400

    
1401
    /* For user convenience ignore high nibble */
1402
    if (ld->type && ((lsa_type & 0x0fff) != (ld->type & 0x0fff)))
1403
      continue;
1404

    
1405
    if (ld->lsid && (lsa->id != ld->lsid))
1406
      continue;
1407

    
1408
    if (ld->router && (lsa->rt != ld->router))
1409
      continue;
1410
    
1411
    if ((dscope != last_dscope) || (hea[i]->domain != last_domain))
1412
    {
1413
      cli_msg(-1017, "");
1414
      switch (dscope)
1415
      {
1416
      case LSA_SCOPE_AS:
1417
        cli_msg(-1017, "Global");
1418
        break;
1419

    
1420
      case LSA_SCOPE_AREA:
1421
        cli_msg(-1017, "Area %R", hea[i]->domain);
1422
        break;
1423

    
1424
      case LSA_SCOPE_LINK:
1425
        {
1426
          struct iface *ifa = if_find_by_index(hea[i]->domain);
1427
          cli_msg(-1017, "Link %s", (ifa != NULL) ? ifa->name : "?");
1428
        }
1429
        break;
1430
      }
1431
      cli_msg(-1017, "");
1432
      cli_msg(-1017," Type   LS ID           Router           Age  Sequence  Checksum");
1433

    
1434
      last_dscope = dscope;
1435
      last_domain = hea[i]->domain;
1436
    }
1437

    
1438
    cli_msg(-1017," %04x  %-15R %-15R %5u  %08x    %04x",
1439
            lsa_type, lsa->id, lsa->rt, lsa->age, lsa->sn, lsa->checksum);
1440
  }
1441
  cli_msg(0, "");
1442
}
1443

    
1444

    
1445
struct protocol proto_ospf = {
1446
  .name =                "OSPF",
1447
  .template =                "ospf%d",
1448
  .attr_class =                EAP_OSPF,
1449
  .preference =                DEF_PREF_OSPF,
1450
  .init =                ospf_init,
1451
  .dump =                ospf_dump,
1452
  .start =                ospf_start,
1453
  .shutdown =                ospf_shutdown,
1454
  .reconfigure =        ospf_reconfigure,
1455
  .get_status =                ospf_get_status,
1456
  .get_attr =                ospf_get_attr,
1457
  .get_route_info =        ospf_get_route_info
1458
};
1459