Statistics
| Branch: | Revision:

iof-bird-daemon / proto / ospf / ospf.c @ 178a197a

History | View | Annotate | Download (38.1 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(P->pool);
239
  p->disp_timer->data = p;
240
  p->disp_timer->randomize = 0;
241
  p->disp_timer->hook = ospf_disp;
242
  p->disp_timer->recurrent = p->tick;
243
  tm_start(p->disp_timer, 1);
244
  p->lsab_size = 256;
245
  p->lsab_used = 0;
246
  p->lsab = mb_alloc(P->pool, p->lsab_size);
247
  p->nhpool = lp_new(P->pool, 12*sizeof(struct mpnh));
248
  init_list(&(p->iface_list));
249
  init_list(&(p->area_list));
250
  fib_init(&p->rtf, P->pool, sizeof(ort), 0, ospf_rt_initort);
251
  p->areano = 0;
252
  p->gr = ospf_top_new(p, P->pool);
253
  s_init_list(&(p->lsal));
254

    
255
  WALK_LIST(ac, c->area_list)
256
    ospf_area_add(p, ac);
257

    
258
  if (c->abr)
259
    ospf_open_vlink_sk(p);
260

    
261
  /* Add all virtual links */
262
  struct ospf_iface_patt *ic;
263
  WALK_LIST(ic, c->vlink_list)
264
    ospf_iface_new_vlink(p, ic);
265

    
266
  return PS_UP;
267
}
268

    
269
static void
270
ospf_dump(struct proto *P)
271
{
272
  struct ospf_proto *p = (struct ospf_proto *) P;
273
  struct ospf_iface *ifa;
274
  struct ospf_neighbor *n;
275

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

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

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

    
298
static struct proto *
299
ospf_init(struct proto_config *c)
300
{
301
  struct ospf_config *oc = (struct ospf_config *) c;
302
  struct proto *P = proto_new(c, sizeof(struct ospf_proto));
303

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

    
315
  return P;
316
}
317

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

    
325
  if(new->attrs->source < old->attrs->source) return 1;
326
  if(new->attrs->source > old->attrs->source) return 0;
327

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

    
334
  if (new->u.ospf.metric1 < old->u.ospf.metric1)
335
    return 1;
336

    
337
  return 0;                        /* Old is shorter or same */
338
}
339

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

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

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

    
380

    
381
void
382
ospf_schedule_rtcalc(struct ospf_proto *p)
383
{
384
  if (p->calcrt)
385
    return;
386

    
387
  OSPF_TRACE(D_EVENTS, "Scheduling routing table calculation");
388
  p->calcrt = 1;
389
}
390

    
391
static int
392
ospf_reload_routes(struct proto *P)
393
{
394
  struct ospf_proto *p = (struct ospf_proto *) P;
395

    
396
  if (p->calcrt != 2)
397
    OSPF_TRACE(D_EVENTS, "Scheduling routing table calculation with route reload");
398

    
399
  p->calcrt = 2;
400

    
401
  return 1;
402
}
403

    
404

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

    
415
  /* Originate or flush local topology LSAs */
416
  ospf_update_topology(p);
417

    
418
  /* Process LSA DB */
419
  ospf_update_lsadb(p);
420

    
421
  /* Calculate routing table */
422
  if (p->calcrt)
423
    ospf_rt_spf(p);
424
}
425

    
426

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

    
444
  if (e->attrs->src->proto == P)
445
    return -1;                        /* Reject our own routes */
446

    
447
  if (oa_is_stub(oa))
448
    return -1;                        /* Do not export routes to stub areas */
449

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

    
453
  *attrs = ospf_build_attrs(*attrs, pool, m1, 10000, 0, 0);
454
  return 0;                        /* Leave decision to the filters */
455
}
456

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

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

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

    
488
  OSPF_TRACE(D_EVENTS, "Shutdown requested");
489

    
490
  /* And send to all my neighbors 1WAY */
491
  WALK_LIST(ifa, p->iface_list)
492
    ospf_iface_shutdown(ifa);
493

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

    
501
  return PS_DOWN;
502
}
503

    
504
static void
505
ospf_get_status(struct proto *P, byte * buf)
506
{
507
  struct ospf_proto *p = (struct ospf_proto *) P;
508

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

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

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

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

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

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

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

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

    
591
  oa->ac = nac;
592

    
593
  if (ospf_is_v2(p))
594
    oa->options = nac->type;
595
  else
596
    oa->options = nac->type | OPT_V6 | (p->stub_router ? 0 : OPT_R);
597

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

    
606
  /* Handle net_list */
607
  fib_free(&oa->net_fib);
608
  fib_free(&oa->enet_fib);
609
  add_area_nets(oa, nac);
610

    
611
  /* No need to handle stubnet_list */
612

    
613
  oa->marked = 0;
614
  ospf_notify_rt_lsa(oa);
615
}
616

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

    
638
  if (proto_get_router_id(c) != p->router_id)
639
    return 0;
640

    
641
  if (p->rfc1583 != new->rfc1583)
642
    return 0;
643

    
644
  if (old->abr != new->abr)
645
    return 0;
646

    
647
  p->stub_router = new->stub_router;
648
  p->merge_external = new->merge_external;
649
  p->asbr = new->asbr;
650
  p->ecmp = new->ecmp;
651
  p->tick = new->tick;
652
  p->disp_timer->recurrent = p->tick;
653
  tm_start(p->disp_timer, 1);
654

    
655
  /* Mark all areas and ifaces */
656
  WALK_LIST(oa, p->area_list)
657
    oa->marked = 1;
658

    
659
  WALK_LIST(ifa, p->iface_list)
660
    ifa->marked = 1;
661

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

    
672
  /* Add and update interfaces */
673
  ospf_reconfigure_ifaces(p);
674

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

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

    
693
  WALK_LIST_DELSAFE(oa, oax, p->area_list)
694
    if (oa->marked)
695
      ospf_area_remove(oa);
696

    
697
  ospf_schedule_rtcalc(p);
698

    
699
  return 1;
700
}
701

    
702

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

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

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

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

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

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

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

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

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

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

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

    
811
  }
812
  cli_msg(0, "");
813
}
814

    
815
void
816
ospf_sh_iface(struct proto *P, char *iff)
817
{
818
  struct ospf_proto *p = (struct ospf_proto *) P;
819
  struct ospf_iface *ifa = NULL;
820

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

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

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

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

    
858
  return dst;
859
}
860

    
861

    
862
static int lsa_compare_ospf3;
863

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

    
875
  if (he1->domain < he2->domain)
876
    return -1;
877
  if (he1->domain > he2->domain)
878
    return 1;
879

    
880

    
881
  /* px1 or px2 assumes OSPFv3 */
882
  int px1 = (lsa1_type == LSA_T_PREFIX);
883
  int px2 = (lsa2_type == LSA_T_PREFIX);
884

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

    
891
  if (px2)
892
  {
893
    lsa2 = fake_lsa_from_prefix_lsa(&lsatmp2, lsa2, he2->lsa_body);
894
    lsa2_type = lsa2->type_raw;
895
  }
896

    
897

    
898
  int nt1 = (lsa1_type == LSA_T_NET);
899
  int nt2 = (lsa2_type == LSA_T_NET);
900

    
901
  if (nt1 != nt2)
902
    return nt1 - nt2;
903

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

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

    
922
    if (px1 != px2)
923
      return px1 - px2;
924

    
925
    return lsa1->sn - lsa2->sn;
926
  }
927
  else
928
  {
929
    if (lsa1->rt < lsa2->rt)
930
      return -1;
931
    if (lsa1->rt > lsa2->rt)
932
      return 1;
933

    
934
    if (lsa1_type < lsa2_type)
935
      return -1;
936
    if (lsa1_type > lsa2_type)
937
      return 1;
938

    
939
    if (lsa1->id < lsa2->id)
940
      return -1;
941
    if (lsa1->id > lsa2->id)
942
      return 1;
943

    
944
    if (px1 != px2)
945
      return px1 - px2;
946

    
947
    return lsa1->sn - lsa2->sn;
948
  }
949
}
950

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

    
959
  if (lsa1->rt < lsa2->rt)
960
    return -1;
961
  if (lsa1->rt > lsa2->rt)
962
    return 1;
963

    
964
  if (lsa1->id < lsa2->id)
965
    return -1;
966
  if (lsa1->id > lsa2->id)
967
    return 1;
968

    
969
  return lsa1->sn - lsa2->sn;
970
}
971

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

    
981
static inline void
982
show_lsa_router(struct ospf_proto *p, struct top_hash_entry *he, int verbose)
983
{
984
  struct ospf_lsa_rt_walk rtl;
985

    
986
  cli_msg(-1016, "");
987
  cli_msg(-1016, "\trouter %R", he->lsa.rt);
988
  show_lsa_distance(he);
989

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

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

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

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

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

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

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

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

    
1054
  show_lsa_distance(he);
1055

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

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

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

    
1072
static inline void
1073
show_lsa_sum_rt(struct top_hash_entry *he, int ospf2)
1074
{
1075
  u32 metric;
1076
  u32 dst_rid;
1077
  u32 options;
1078

    
1079
  lsa_parse_sum_rt(he, ospf2, &dst_rid, &metric, &options);
1080
  cli_msg(-1016, "\t\txrouter %R metric %u", dst_rid, metric);
1081
}
1082

    
1083

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

    
1091
  if (he->lsa_type == LSA_T_EXT)
1092
    he->domain = 0; /* Unmark the LSA */
1093

    
1094
  lsa_parse_ext(he, ospf2, &rt);
1095

    
1096
  if (rt.fbit)
1097
    bsprintf(str_via, " via %I", rt.fwaddr);
1098

    
1099
  if (rt.tag)
1100
    bsprintf(str_tag, " tag %08x", rt.tag);
1101

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

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

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

    
1122
  if ((px->ref_type == LSA_T_RT) && (px->ref_id != 0))
1123
    return;
1124

    
1125
  if ((px->ref_type == LSA_T_NET) && (px->ref_id != cnode->lsa.id))
1126
    return;
1127

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

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

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

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

    
1155
  /* We store interesting area-scoped LSAs in array hea and
1156
     global-scoped (LSA_T_EXT) LSAs in array hex */
1157

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

    
1164
  j1 = jx = 0;
1165
  WALK_SLIST(he, p->lsal)
1166
  {
1167
    int accept;
1168

    
1169
    if (he->lsa.age == LSA_MAXAGE)
1170
      continue;
1171

    
1172
    switch (he->lsa_type)
1173
    {
1174
    case LSA_T_RT:
1175
    case LSA_T_NET:
1176
      accept = 1;
1177
      break;
1178

    
1179
    case LSA_T_SUM_NET:
1180
    case LSA_T_SUM_RT:
1181
    case LSA_T_NSSA:
1182
    case LSA_T_PREFIX:
1183
      accept = verbose;
1184
      break;
1185

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

    
1196
    if (accept)
1197
      hea[j1++] = he;
1198
  }
1199

    
1200
  ASSERT(j1 <= num && jx <= num);
1201

    
1202
  lsa_compare_ospf3 = !ospf2;
1203
  qsort(hea, j1, sizeof(struct top_hash_entry *), lsa_compare_for_state);
1204
  qsort(hex, jx, sizeof(struct top_hash_entry *), ext_compare_for_state);
1205

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

    
1225
  ix = 0;
1226
  for (i = 0; i < j1; i++)
1227
  {
1228
    he = hea[i];
1229

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

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

    
1250
    ASSERT(cnode && (he->domain == last_area) && (he->lsa.rt == cnode->lsa.rt));
1251

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

    
1259
    case LSA_T_NET:
1260
      show_lsa_network(he, ospf2);
1261
      break;
1262

    
1263
    case LSA_T_SUM_NET:
1264
      if (cnode->lsa_type == LSA_T_RT)
1265
        show_lsa_sum_net(he, ospf2);
1266
      break;
1267

    
1268
    case LSA_T_SUM_RT:
1269
      if (cnode->lsa_type == LSA_T_RT)
1270
        show_lsa_sum_rt(he, ospf2);
1271
      break;
1272

    
1273
    case LSA_T_EXT:
1274
    case LSA_T_NSSA:
1275
      show_lsa_external(he, ospf2);
1276
      break;
1277

    
1278
    case LSA_T_PREFIX:
1279
      show_lsa_prefix(he, cnode);
1280
      break;
1281
    }
1282

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

    
1292
      while ((ix < jx) && (hex[ix]->lsa.rt == cnode->lsa.rt))
1293
        show_lsa_external(hex[ix++], ospf2);
1294

    
1295
      cnode = NULL;
1296
    }
1297
  }
1298

    
1299
  int hdr = 0;
1300
  u32 last_rt = 0xFFFFFFFF;
1301
  for (ix = 0; ix < jx; ix++)
1302
  {
1303
    he = hex[ix];
1304

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

    
1310
      if ((he->color != INSPF) && reachable)
1311
        continue;
1312

    
1313
      if (!hdr)
1314
      {
1315
        cli_msg(-1016, "");
1316
        cli_msg(-1016, "other ASBRs");
1317
        hdr = 1;
1318
      }
1319

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

    
1327
      show_lsa_external(he, ospf2);
1328
    }
1329
  }
1330

    
1331
  cli_msg(0, "");
1332
}
1333

    
1334

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

    
1345
  if (sc1 != sc2)
1346
    return sc2 - sc1;
1347

    
1348
  if (he1->domain != he2->domain)
1349
    return he1->domain - he2->domain;
1350

    
1351
  if (lsa1->rt != lsa2->rt)
1352
    return lsa1->rt - lsa2->rt;
1353

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

    
1357
  if (he1->lsa_type != he2->lsa_type)
1358
    return he1->lsa_type - he2->lsa_type;
1359

    
1360
  return lsa1->sn - lsa2->sn;
1361
}
1362

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

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

    
1380
  if (ld->router == SH_ROUTER_SELF)
1381
    ld->router = p->router_id;
1382

    
1383
  struct top_hash_entry *hea[num];
1384
  struct top_hash_entry *he;
1385

    
1386
  j = 0;
1387
  WALK_SLIST(he, p->lsal)
1388
    if (he->lsa_body)
1389
      hea[j++] = he;
1390

    
1391
  ASSERT(j <= num);
1392

    
1393
  qsort(hea, j, sizeof(struct top_hash_entry *), lsa_compare_for_lsadb);
1394

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

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

    
1405
    if ((ld->scope == LSA_SCOPE_AREA) && (hea[i]->domain != ld->area))
1406
      continue;
1407

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

    
1412
    if (ld->lsid && (lsa->id != ld->lsid))
1413
      continue;
1414

    
1415
    if (ld->router && (lsa->rt != ld->router))
1416
      continue;
1417

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

    
1427
      case LSA_SCOPE_AREA:
1428
        cli_msg(-1017, "Area %R", hea[i]->domain);
1429
        break;
1430

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

    
1441
      last_dscope = dscope;
1442
      last_domain = hea[i]->domain;
1443
    }
1444

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

    
1451

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