Statistics
| Branch: | Revision:

iof-bird-daemon / proto / ospf / ospf.c @ 600998fc

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

    
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, ospf_is_v2(p) ? NET_IP4 : NET_IP6,
119
           sizeof(struct area_net), OFFSETOF(struct area_net, fn), 0, NULL);
120
  fib_init(&oa->enet_fib, p->p.pool, ospf_is_v2(p) ? NET_IP4 : NET_IP6,
121
           sizeof(struct area_net), OFFSETOF(struct area_net, fn), 0, NULL);
122

    
123
  WALK_LIST(anc, ac->net_list)
124
  {
125
    an = fib_get(&oa->net_fib, &anc->prefix);
126
    an->hidden = anc->hidden;
127
  }
128

    
129
  WALK_LIST(anc, ac->enet_list)
130
  {
131
    an = fib_get(&oa->enet_fib, &anc->prefix);
132
    an->hidden = anc->hidden;
133
    an->tag = anc->tag;
134
  }
135
}
136

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

    
142
  OSPF_TRACE(D_EVENTS, "Adding area %R", ac->areaid);
143

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

    
148
  oa->ac = ac;
149
  oa->areaid = ac->areaid;
150
  oa->rt = NULL;
151
  oa->po = p;
152
  fib_init(&oa->rtr, p->p.pool, NET_IP4, sizeof(ort), OFFSETOF(ort, fn), 0, NULL);
153
  add_area_nets(oa, ac);
154

    
155
  if (oa->areaid == 0)
156
    p->backbone = oa;
157

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

    
163
  ospf_notify_rt_lsa(oa);
164
}
165

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

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

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

    
182
  /* We suppose that interfaces are already removed */
183
  ospf_flush_area(p, oa->areaid);
184

    
185
  fib_free(&oa->rtr);
186
  fib_free(&oa->net_fib);
187
  fib_free(&oa->enet_fib);
188

    
189
  if (oa->translator_timer)
190
    rfree(oa->translator_timer);
191

    
192
  p->areano--;
193
  rem_node(NODE oa);
194
  mb_free(oa);
195
}
196

    
197

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

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

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

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

    
247
  p->flood_event = ev_new(P->pool);
248
  p->flood_event->hook = ospf_flood_event;
249
  p->flood_event->data = p;
250

    
251
  p->log_pkt_tbf = (struct tbf){ .rate = 1, .burst = 5 };
252
  p->log_lsa_tbf = (struct tbf){ .rate = 4, .burst = 20 };
253

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

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

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

    
265
  return PS_UP;
266
}
267

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

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

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

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

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

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

    
314
  return P;
315
}
316

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

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

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

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

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

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

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

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

    
379

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

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

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

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

    
398
  p->calcrt = 2;
399

    
400
  return 1;
401
}
402

    
403

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

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

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

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

    
425

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

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

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

    
449
  ea_list *ea = e->attrs->eattrs;
450
  u32 m0 = ea_get_int(ea, EA_GEN_IGP_METRIC, LSINFINITY);
451
  u32 m1 = MIN(m0, LSINFINITY);
452
  u32 m2 = 10000;
453
  u32 tag = 0;
454

    
455
  /* Hack for setting attributes directly in static protocol */
456
  if (e->attrs->source == RTS_STATIC)
457
  {
458
    m1 = ea_get_int(ea, EA_OSPF_METRIC1, m1);
459
    m2 = ea_get_int(ea, EA_OSPF_METRIC2, 10000);
460
    tag = ea_get_int(ea, EA_OSPF_TAG, 0);
461
  }
462

    
463
  *attrs = ospf_build_attrs(*attrs, pool, m1, m2, tag, 0);
464
  return 0;                        /* Leave decision to the filters */
465
}
466

    
467
static struct ea_list *
468
ospf_make_tmp_attrs(struct rte *rt, struct linpool *pool)
469
{
470
  return ospf_build_attrs(NULL, pool, rt->u.ospf.metric1, rt->u.ospf.metric2,
471
                          rt->u.ospf.tag, rt->u.ospf.router_id);
472
}
473

    
474
static void
475
ospf_store_tmp_attrs(struct rte *rt, struct ea_list *attrs)
476
{
477
  rt->u.ospf.metric1 = ea_get_int(attrs, EA_OSPF_METRIC1, LSINFINITY);
478
  rt->u.ospf.metric2 = ea_get_int(attrs, EA_OSPF_METRIC2, 10000);
479
  rt->u.ospf.tag = ea_get_int(attrs, EA_OSPF_TAG, 0);
480
  rt->u.ospf.router_id = ea_get_int(attrs, EA_OSPF_ROUTER_ID, 0);
481
}
482

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

    
498
  OSPF_TRACE(D_EVENTS, "Shutdown requested");
499

    
500
  /* And send to all my neighbors 1WAY */
501
  WALK_LIST(ifa, p->iface_list)
502
    ospf_iface_shutdown(ifa);
503

    
504
  /* Cleanup locked rta entries */
505
  FIB_WALK(&p->rtf, ort, nf)
506
  {
507
    rta_free(nf->old_rta);
508
  }
509
  FIB_WALK_END;
510

    
511
  return PS_DOWN;
512
}
513

    
514
static void
515
ospf_get_status(struct proto *P, byte * buf)
516
{
517
  struct ospf_proto *p = (struct ospf_proto *) P;
518

    
519
  if (p->p.proto_state == PS_DOWN)
520
    buf[0] = 0;
521
  else
522
  {
523
    struct ospf_iface *ifa;
524
    struct ospf_neighbor *n;
525
    int adj = 0;
526

    
527
    WALK_LIST(ifa, p->iface_list)
528
      WALK_LIST(n, ifa->neigh_list) if (n->state == NEIGHBOR_FULL)
529
      adj = 1;
530

    
531
    if (adj == 0)
532
      strcpy(buf, "Alone");
533
    else
534
      strcpy(buf, "Running");
535
  }
536
}
537

    
538
static void
539
ospf_get_route_info(rte * rte, byte * buf, ea_list * attrs UNUSED)
540
{
541
  char *type = "<bug>";
542

    
543
  switch (rte->attrs->source)
544
  {
545
  case RTS_OSPF:
546
    type = "I";
547
    break;
548
  case RTS_OSPF_IA:
549
    type = "IA";
550
    break;
551
  case RTS_OSPF_EXT1:
552
    type = "E1";
553
    break;
554
  case RTS_OSPF_EXT2:
555
    type = "E2";
556
    break;
557
  }
558

    
559
  buf += bsprintf(buf, " %s", type);
560
  buf += bsprintf(buf, " (%d/%d", rte->pref, rte->u.ospf.metric1);
561
  if (rte->attrs->source == RTS_OSPF_EXT2)
562
    buf += bsprintf(buf, "/%d", rte->u.ospf.metric2);
563
  buf += bsprintf(buf, ")");
564
  if ((rte->attrs->source == RTS_OSPF_EXT1 || rte->attrs->source == RTS_OSPF_EXT2) && rte->u.ospf.tag)
565
  {
566
    buf += bsprintf(buf, " [%x]", rte->u.ospf.tag);
567
  }
568
  if (rte->u.ospf.router_id)
569
    buf += bsprintf(buf, " [%R]", rte->u.ospf.router_id);
570
}
571

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

    
594
static void
595
ospf_area_reconfigure(struct ospf_area *oa, struct ospf_area_config *nac)
596
{
597
  struct ospf_proto *p = oa->po;
598
  struct ospf_area_config *oac = oa->ac;
599
  struct ospf_iface *ifa;
600

    
601
  oa->ac = nac;
602

    
603
  if (ospf_is_v2(p))
604
    oa->options = nac->type;
605
  else
606
    oa->options = nac->type | OPT_V6 | (p->stub_router ? 0 : OPT_R);
607

    
608
  if (nac->type != oac->type)
609
  {
610
    /* Force restart of area interfaces */
611
    WALK_LIST(ifa, p->iface_list)
612
      if (ifa->oa == oa)
613
        ifa->marked = 2;
614
  }
615

    
616
  /* Handle net_list */
617
  fib_free(&oa->net_fib);
618
  fib_free(&oa->enet_fib);
619
  add_area_nets(oa, nac);
620

    
621
  /* No need to handle stubnet_list */
622

    
623
  oa->marked = 0;
624
  ospf_notify_rt_lsa(oa);
625
}
626

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

    
648
  if (proto_get_router_id(c) != p->router_id)
649
    return 0;
650

    
651
  if (p->ospf2 != new->ospf2)
652
    return 0;
653

    
654
  if (p->rfc1583 != new->rfc1583)
655
    return 0;
656

    
657
  if (old->abr != new->abr)
658
    return 0;
659

    
660
  p->stub_router = new->stub_router;
661
  p->merge_external = new->merge_external;
662
  p->asbr = new->asbr;
663
  p->ecmp = new->ecmp;
664
  p->tick = new->tick;
665
  p->disp_timer->recurrent = p->tick;
666
  tm_start(p->disp_timer, 1);
667

    
668
  /* Mark all areas and ifaces */
669
  WALK_LIST(oa, p->area_list)
670
    oa->marked = 1;
671

    
672
  WALK_LIST(ifa, p->iface_list)
673
    ifa->marked = 1;
674

    
675
  /* Add and update areas */
676
  WALK_LIST(nac, new->area_list)
677
  {
678
    oa = ospf_find_area(p, nac->areaid);
679
    if (oa)
680
      ospf_area_reconfigure(oa, nac);
681
    else
682
      ospf_area_add(p, nac);
683
  }
684

    
685
  /* Add and update interfaces */
686
  ospf_reconfigure_ifaces(p);
687

    
688
  /* Add and update vlinks */
689
  WALK_LIST(ip, new->vlink_list)
690
  {
691
    ifa = ospf_find_vlink(p, ip->voa, ip->vid);
692
    if (ifa)
693
      ospf_iface_reconfigure(ifa, ip);
694
    else
695
      ospf_iface_new_vlink(p, ip);
696
  }
697

    
698
  /* Delete remaining ifaces and areas */
699
  WALK_LIST_DELSAFE(ifa, ifx, p->iface_list)
700
    if (ifa->marked)
701
    {
702
      ospf_iface_shutdown(ifa);
703
      ospf_iface_remove(ifa);
704
    }
705

    
706
  WALK_LIST_DELSAFE(oa, oax, p->area_list)
707
    if (oa->marked)
708
      ospf_area_remove(oa);
709

    
710
  ospf_schedule_rtcalc(p);
711

    
712
  return 1;
713
}
714

    
715

    
716
void
717
ospf_sh_neigh(struct proto *P, char *iff)
718
{
719
  struct ospf_proto *p = (struct ospf_proto *) P;
720
  struct ospf_iface *ifa = NULL;
721
  struct ospf_neighbor *n;
722

    
723
  if (p->p.proto_state != PS_UP)
724
  {
725
    cli_msg(-1013, "%s: is not up", p->p.name);
726
    cli_msg(0, "");
727
    return;
728
  }
729

    
730
  cli_msg(-1013, "%s:", p->p.name);
731
  cli_msg(-1013, "%-12s\t%3s\t%-15s\t%-5s\t%-10s %-12s", "Router ID", "Pri",
732
          "     State", "DTime", "Interface", "Router IP");
733
  WALK_LIST(ifa, p->iface_list)
734
    if ((iff == NULL) || patmatch(iff, ifa->ifname))
735
      WALK_LIST(n, ifa->neigh_list)
736
        ospf_sh_neigh_info(n);
737
  cli_msg(0, "");
738
}
739

    
740
void
741
ospf_sh(struct proto *P)
742
{
743
  struct ospf_proto *p = (struct ospf_proto *) P;
744
  struct ospf_area *oa;
745
  struct ospf_iface *ifa;
746
  struct ospf_neighbor *n;
747
  int ifano, nno, adjno, firstfib;
748

    
749
  if (p->p.proto_state != PS_UP)
750
  {
751
    cli_msg(-1014, "%s: is not up", p->p.name);
752
    cli_msg(0, "");
753
    return;
754
  }
755

    
756
  cli_msg(-1014, "%s:", p->p.name);
757
  cli_msg(-1014, "RFC1583 compatibility: %s", (p->rfc1583 ? "enabled" : "disabled"));
758
  cli_msg(-1014, "Stub router: %s", (p->stub_router ? "Yes" : "No"));
759
  cli_msg(-1014, "RT scheduler tick: %d", p->tick);
760
  cli_msg(-1014, "Number of areas: %u", p->areano);
761
  cli_msg(-1014, "Number of LSAs in DB:\t%u", p->gr->hash_entries);
762

    
763
  WALK_LIST(oa, p->area_list)
764
  {
765
    cli_msg(-1014, "\tArea: %R (%u) %s", oa->areaid, oa->areaid,
766
            oa->areaid == 0 ? "[BACKBONE]" : "");
767
    ifano = 0;
768
    nno = 0;
769
    adjno = 0;
770
    WALK_LIST(ifa, p->iface_list)
771
    {
772
      if (oa == ifa->oa)
773
      {
774
        ifano++;
775
        WALK_LIST(n, ifa->neigh_list)
776
        {
777
          nno++;
778
          if (n->state == NEIGHBOR_FULL)
779
            adjno++;
780
        }
781
      }
782
    }
783

    
784
    cli_msg(-1014, "\t\tStub:\t%s", oa_is_stub(oa) ? "Yes" : "No");
785
    cli_msg(-1014, "\t\tNSSA:\t%s", oa_is_nssa(oa) ? "Yes" : "No");
786
    cli_msg(-1014, "\t\tTransit:\t%s", oa->trcap ? "Yes" : "No");
787

    
788
    if (oa_is_nssa(oa))
789
      cli_msg(-1014, "\t\tNSSA translation:\t%s%s", oa->translate ? "Yes" : "No",
790
              oa->translate == TRANS_WAIT ? " (run down)" : "");
791
    cli_msg(-1014, "\t\tNumber of interfaces:\t%u", ifano);
792
    cli_msg(-1014, "\t\tNumber of neighbors:\t%u", nno);
793
    cli_msg(-1014, "\t\tNumber of adjacent neighbors:\t%u", adjno);
794

    
795
    firstfib = 1;
796
    FIB_WALK(&oa->net_fib, struct area_net, anet)
797
    {
798
      if(firstfib)
799
      {
800
        cli_msg(-1014, "\t\tArea networks:");
801
        firstfib = 0;
802
      }
803
      cli_msg(-1014, "\t\t\t%1N\t%s\t%s", anet->fn.addr,
804
                anet->hidden ? "Hidden" : "Advertise", anet->active ? "Active" : "");
805
    }
806
    FIB_WALK_END;
807

    
808
    firstfib = 1;
809
    FIB_WALK(&oa->enet_fib, struct area_net, anet)
810
    {
811
      if(firstfib)
812
      {
813
        cli_msg(-1014, "\t\tArea external networks:");
814
        firstfib = 0;
815
      }
816
      cli_msg(-1014, "\t\t\t%1N\t%s\t%s", anet->fn.addr,
817
                anet->hidden ? "Hidden" : "Advertise", anet->active ? "Active" : "");
818
    }
819
    FIB_WALK_END;
820

    
821
  }
822
  cli_msg(0, "");
823
}
824

    
825
void
826
ospf_sh_iface(struct proto *P, char *iff)
827
{
828
  struct ospf_proto *p = (struct ospf_proto *) P;
829
  struct ospf_iface *ifa = NULL;
830

    
831
  if (p->p.proto_state != PS_UP)
832
  {
833
    cli_msg(-1015, "%s: is not up", p->p.name);
834
    cli_msg(0, "");
835
    return;
836
  }
837

    
838
  cli_msg(-1015, "%s:", p->p.name);
839
  WALK_LIST(ifa, p->iface_list)
840
    if ((iff == NULL) || patmatch(iff, ifa->ifname))
841
      ospf_iface_info(ifa);
842
  cli_msg(0, "");
843
}
844

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

    
858
static struct ospf_lsa_header *
859
fake_lsa_from_prefix_lsa(struct ospf_lsa_header *dst, struct ospf_lsa_header *src,
860
                         struct ospf_lsa_prefix *px)
861
{
862
  dst->age = src->age;
863
  dst->type_raw = px->ref_type;
864
  dst->id = px->ref_id;
865
  dst->rt = px->ref_rt;
866
  dst->sn = src->sn;
867

    
868
  return dst;
869
}
870

    
871

    
872
static int lsa_compare_ospf3;
873

    
874
static int
875
lsa_compare_for_state(const void *p1, const void *p2)
876
{
877
  struct top_hash_entry *he1 = * (struct top_hash_entry **) p1;
878
  struct top_hash_entry *he2 = * (struct top_hash_entry **) p2;
879
  struct ospf_lsa_header *lsa1 = &(he1->lsa);
880
  struct ospf_lsa_header *lsa2 = &(he2->lsa);
881
  struct ospf_lsa_header lsatmp1, lsatmp2;
882
  u16 lsa1_type = he1->lsa_type;
883
  u16 lsa2_type = he2->lsa_type;
884

    
885
  if (he1->domain < he2->domain)
886
    return -1;
887
  if (he1->domain > he2->domain)
888
    return 1;
889

    
890

    
891
  /* px1 or px2 assumes OSPFv3 */
892
  int px1 = (lsa1_type == LSA_T_PREFIX);
893
  int px2 = (lsa2_type == LSA_T_PREFIX);
894

    
895
  if (px1)
896
  {
897
    lsa1 = fake_lsa_from_prefix_lsa(&lsatmp1, lsa1, he1->lsa_body);
898
    lsa1_type = lsa1->type_raw;        /* FIXME: handle unknown ref_type */
899
  }
900

    
901
  if (px2)
902
  {
903
    lsa2 = fake_lsa_from_prefix_lsa(&lsatmp2, lsa2, he2->lsa_body);
904
    lsa2_type = lsa2->type_raw;
905
  }
906

    
907

    
908
  int nt1 = (lsa1_type == LSA_T_NET);
909
  int nt2 = (lsa2_type == LSA_T_NET);
910

    
911
  if (nt1 != nt2)
912
    return nt1 - nt2;
913

    
914
  if (nt1)
915
  {
916
    /* In OSPFv3, networks are named based on ID of DR */
917
    if (lsa_compare_ospf3)
918
    {
919
      if (lsa1->rt < lsa2->rt)
920
        return -1;
921
      if (lsa1->rt > lsa2->rt)
922
        return 1;
923
    }
924

    
925
    /* For OSPFv2, this is IP of the network,
926
       for OSPFv3, this is interface ID */
927
    if (lsa1->id < lsa2->id)
928
      return -1;
929
    if (lsa1->id > lsa2->id)
930
      return 1;
931

    
932
    if (px1 != px2)
933
      return px1 - px2;
934

    
935
    return lsa1->sn - lsa2->sn;
936
  }
937
  else
938
  {
939
    if (lsa1->rt < lsa2->rt)
940
      return -1;
941
    if (lsa1->rt > lsa2->rt)
942
      return 1;
943

    
944
    if (lsa1_type < lsa2_type)
945
      return -1;
946
    if (lsa1_type > lsa2_type)
947
      return 1;
948

    
949
    if (lsa1->id < lsa2->id)
950
      return -1;
951
    if (lsa1->id > lsa2->id)
952
      return 1;
953

    
954
    if (px1 != px2)
955
      return px1 - px2;
956

    
957
    return lsa1->sn - lsa2->sn;
958
  }
959
}
960

    
961
static int
962
ext_compare_for_state(const void *p1, const void *p2)
963
{
964
  struct top_hash_entry * he1 = * (struct top_hash_entry **) p1;
965
  struct top_hash_entry * he2 = * (struct top_hash_entry **) p2;
966
  struct ospf_lsa_header *lsa1 = &(he1->lsa);
967
  struct ospf_lsa_header *lsa2 = &(he2->lsa);
968

    
969
  if (lsa1->rt < lsa2->rt)
970
    return -1;
971
  if (lsa1->rt > lsa2->rt)
972
    return 1;
973

    
974
  if (lsa1->id < lsa2->id)
975
    return -1;
976
  if (lsa1->id > lsa2->id)
977
    return 1;
978

    
979
  return lsa1->sn - lsa2->sn;
980
}
981

    
982
static inline void
983
show_lsa_distance(struct top_hash_entry *he)
984
{
985
  if (he->color == INSPF)
986
    cli_msg(-1016, "\t\tdistance %u", he->dist);
987
  else
988
    cli_msg(-1016, "\t\tunreachable");
989
}
990

    
991
static inline void
992
show_lsa_router(struct ospf_proto *p, struct top_hash_entry *he, int verbose)
993
{
994
  struct ospf_lsa_rt_walk rtl;
995

    
996
  cli_msg(-1016, "");
997
  cli_msg(-1016, "\trouter %R", he->lsa.rt);
998
  show_lsa_distance(he);
999

    
1000
  lsa_walk_rt_init(p, he, &rtl);
1001
  while (lsa_walk_rt(&rtl))
1002
    if (rtl.type == LSART_VLNK)
1003
      cli_msg(-1016, "\t\tvlink %R metric %u", rtl.id, rtl.metric);
1004

    
1005
  lsa_walk_rt_init(p, he, &rtl);
1006
  while (lsa_walk_rt(&rtl))
1007
    if (rtl.type == LSART_PTP)
1008
      cli_msg(-1016, "\t\trouter %R metric %u", rtl.id, rtl.metric);
1009

    
1010
  lsa_walk_rt_init(p, he, &rtl);
1011
  while (lsa_walk_rt(&rtl))
1012
    if (rtl.type == LSART_NET)
1013
    {
1014
      if (ospf_is_v2(p))
1015
      {
1016
        /* In OSPFv2, we try to find network-LSA to get prefix/pxlen */
1017
        struct top_hash_entry *net_he = ospf_hash_find_net2(p->gr, he->domain, rtl.id);
1018

    
1019
        if (net_he && (net_he->lsa.age < LSA_MAXAGE))
1020
        {
1021
          struct ospf_lsa_header *net_lsa = &(net_he->lsa);
1022
          struct ospf_lsa_net *net_ln = net_he->lsa_body;
1023

    
1024
          cli_msg(-1016, "\t\tnetwork %I/%d metric %u",
1025
                  ipa_from_u32(net_lsa->id & net_ln->optx),
1026
                  u32_masklen(net_ln->optx), rtl.metric);
1027
        }
1028
        else
1029
          cli_msg(-1016, "\t\tnetwork [%R] metric %u", rtl.id, rtl.metric);
1030
      }
1031
      else
1032
        cli_msg(-1016, "\t\tnetwork [%R-%u] metric %u", rtl.id, rtl.nif, rtl.metric);
1033
    }
1034

    
1035
  if (ospf_is_v2(p) && verbose)
1036
  {
1037
    lsa_walk_rt_init(p, he, &rtl);
1038
    while (lsa_walk_rt(&rtl))
1039
      if (rtl.type == LSART_STUB)
1040
        cli_msg(-1016, "\t\tstubnet %I/%d metric %u",
1041
                ipa_from_u32(rtl.id), u32_masklen(rtl.data), rtl.metric);
1042
  }
1043
}
1044

    
1045
static inline void
1046
show_lsa_network(struct top_hash_entry *he, int ospf2)
1047
{
1048
  struct ospf_lsa_header *lsa = &(he->lsa);
1049
  struct ospf_lsa_net *ln = he->lsa_body;
1050
  u32 i;
1051

    
1052
  if (ospf2)
1053
  {
1054
    cli_msg(-1016, "");
1055
    cli_msg(-1016, "\tnetwork %I/%d", ipa_from_u32(lsa->id & ln->optx), u32_masklen(ln->optx));
1056
    cli_msg(-1016, "\t\tdr %R", lsa->rt);
1057
  }
1058
  else
1059
  {
1060
    cli_msg(-1016, "");
1061
    cli_msg(-1016, "\tnetwork [%R-%u]", lsa->rt, lsa->id);
1062
  }
1063

    
1064
  show_lsa_distance(he);
1065

    
1066
  for (i = 0; i < lsa_net_count(lsa); i++)
1067
    cli_msg(-1016, "\t\trouter %R", ln->routers[i]);
1068
}
1069

    
1070
static inline void
1071
show_lsa_sum_net(struct top_hash_entry *he, int ospf2)
1072
{
1073
  net_addr net;
1074
  u8 pxopts;
1075
  u32 metric;
1076

    
1077
  lsa_parse_sum_net(he, ospf2, &net, &pxopts, &metric);
1078
  cli_msg(-1016, "\t\txnetwork %N metric %u", &net, metric);
1079
}
1080

    
1081
static inline void
1082
show_lsa_sum_rt(struct top_hash_entry *he, int ospf2)
1083
{
1084
  u32 metric;
1085
  u32 dst_rid;
1086
  u32 options;
1087

    
1088
  lsa_parse_sum_rt(he, ospf2, &dst_rid, &metric, &options);
1089
  cli_msg(-1016, "\t\txrouter %R metric %u", dst_rid, metric);
1090
}
1091

    
1092

    
1093
static inline void
1094
show_lsa_external(struct top_hash_entry *he, int ospf2)
1095
{
1096
  struct ospf_lsa_ext_local rt;
1097
  char str_via[IPA_MAX_TEXT_LENGTH + 8] = "";
1098
  char str_tag[16] = "";
1099

    
1100
  if (he->lsa_type == LSA_T_EXT)
1101
    he->domain = 0; /* Unmark the LSA */
1102

    
1103
  lsa_parse_ext(he, ospf2, &rt);
1104

    
1105
  if (rt.fbit)
1106
    bsprintf(str_via, " via %I", rt.fwaddr);
1107

    
1108
  if (rt.tag)
1109
    bsprintf(str_tag, " tag %08x", rt.tag);
1110

    
1111
  cli_msg(-1016, "\t\t%s %N metric%s %u%s%s",
1112
          (he->lsa_type == LSA_T_NSSA) ? "nssa-ext" : "external",
1113
          &rt.net, rt.ebit ? "2" : "", rt.metric, str_via, str_tag);
1114
}
1115

    
1116
static inline void
1117
show_lsa_prefix(struct top_hash_entry *he, struct top_hash_entry *cnode)
1118
{
1119
  struct ospf_lsa_prefix *px = he->lsa_body;
1120
  u32 *buf;
1121
  int i;
1122

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

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

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

    
1133
  buf = px->rest;
1134
  for (i = 0; i < px->pxcount; i++)
1135
  {
1136
    net_addr net;
1137
    u8 pxopts;
1138
    u16 metric;
1139

    
1140
    buf = ospf_get_ipv6_prefix(buf, &net, &pxopts, &metric);
1141

    
1142
    if (px->ref_type == LSA_T_RT)
1143
      cli_msg(-1016, "\t\tstubnet %N metric %u", &net, metric);
1144
    else
1145
      cli_msg(-1016, "\t\taddress %N", &net);
1146
  }
1147
}
1148

    
1149
void
1150
ospf_sh_state(struct proto *P, int verbose, int reachable)
1151
{
1152
  struct ospf_proto *p = (struct ospf_proto *) P;
1153
  int ospf2 = ospf_is_v2(p);
1154
  uint i, ix, j1, jx;
1155
  u32 last_area = 0xFFFFFFFF;
1156

    
1157
  if (p->p.proto_state != PS_UP)
1158
  {
1159
    cli_msg(-1016, "%s: is not up", p->p.name);
1160
    cli_msg(0, "");
1161
    return;
1162
  }
1163

    
1164
  /* We store interesting area-scoped LSAs in array hea and
1165
     global-scoped (LSA_T_EXT) LSAs in array hex */
1166

    
1167
  int num = p->gr->hash_entries;
1168
  struct top_hash_entry *hea[num];
1169
  struct top_hash_entry *hex[verbose ? num : 0];
1170
  struct top_hash_entry *he;
1171
  struct top_hash_entry *cnode = NULL;
1172

    
1173
  j1 = jx = 0;
1174
  WALK_SLIST(he, p->lsal)
1175
  {
1176
    int accept;
1177

    
1178
    if (he->lsa.age == LSA_MAXAGE)
1179
      continue;
1180

    
1181
    switch (he->lsa_type)
1182
    {
1183
    case LSA_T_RT:
1184
    case LSA_T_NET:
1185
      accept = 1;
1186
      break;
1187

    
1188
    case LSA_T_SUM_NET:
1189
    case LSA_T_SUM_RT:
1190
    case LSA_T_NSSA:
1191
    case LSA_T_PREFIX:
1192
      accept = verbose;
1193
      break;
1194

    
1195
    case LSA_T_EXT:
1196
      if (verbose)
1197
      {
1198
        he->domain = 1; /* Abuse domain field to mark the LSA */
1199
        hex[jx++] = he;
1200
      }
1201
    default:
1202
      accept = 0;
1203
    }
1204

    
1205
    if (accept)
1206
      hea[j1++] = he;
1207
  }
1208

    
1209
  ASSERT(j1 <= num && jx <= num);
1210

    
1211
  lsa_compare_ospf3 = !ospf2;
1212
  qsort(hea, j1, sizeof(struct top_hash_entry *), lsa_compare_for_state);
1213
  qsort(hex, jx, sizeof(struct top_hash_entry *), ext_compare_for_state);
1214

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

    
1234
  ix = 0;
1235
  for (i = 0; i < j1; i++)
1236
  {
1237
    he = hea[i];
1238

    
1239
    /* If there is no opened node, we open the LSA (if appropriate) or skip to the next one */
1240
    if (!cnode)
1241
    {
1242
      if (((he->lsa_type == LSA_T_RT) || (he->lsa_type == LSA_T_NET))
1243
          && ((he->color == INSPF) || !reachable))
1244
      {
1245
        cnode = he;
1246

    
1247
        if (he->domain != last_area)
1248
        {
1249
          cli_msg(-1016, "");
1250
          cli_msg(-1016, "area %R", he->domain);
1251
          last_area = he->domain;
1252
          ix = 0;
1253
        }
1254
      }
1255
      else
1256
        continue;
1257
    }
1258

    
1259
    ASSERT(cnode && (he->domain == last_area) && (he->lsa.rt == cnode->lsa.rt));
1260

    
1261
    switch (he->lsa_type)
1262
    {
1263
    case LSA_T_RT:
1264
      if (he->lsa.id == cnode->lsa.id)
1265
        show_lsa_router(p, he, verbose);
1266
      break;
1267

    
1268
    case LSA_T_NET:
1269
      show_lsa_network(he, ospf2);
1270
      break;
1271

    
1272
    case LSA_T_SUM_NET:
1273
      if (cnode->lsa_type == LSA_T_RT)
1274
        show_lsa_sum_net(he, ospf2);
1275
      break;
1276

    
1277
    case LSA_T_SUM_RT:
1278
      if (cnode->lsa_type == LSA_T_RT)
1279
        show_lsa_sum_rt(he, ospf2);
1280
      break;
1281

    
1282
    case LSA_T_EXT:
1283
    case LSA_T_NSSA:
1284
      show_lsa_external(he, ospf2);
1285
      break;
1286

    
1287
    case LSA_T_PREFIX:
1288
      show_lsa_prefix(he, cnode);
1289
      break;
1290
    }
1291

    
1292
    /* In these cases, we close the current node */
1293
    if ((i+1 == j1)
1294
        || (hea[i+1]->domain != last_area)
1295
        || (hea[i+1]->lsa.rt != cnode->lsa.rt)
1296
        || (hea[i+1]->lsa_type == LSA_T_NET))
1297
    {
1298
      while ((ix < jx) && (hex[ix]->lsa.rt < cnode->lsa.rt))
1299
        ix++;
1300

    
1301
      while ((ix < jx) && (hex[ix]->lsa.rt == cnode->lsa.rt))
1302
        show_lsa_external(hex[ix++], ospf2);
1303

    
1304
      cnode = NULL;
1305
    }
1306
  }
1307

    
1308
  int hdr = 0;
1309
  u32 last_rt = 0xFFFFFFFF;
1310
  for (ix = 0; ix < jx; ix++)
1311
  {
1312
    he = hex[ix];
1313

    
1314
    /* If it is still marked, we show it now. */
1315
    if (he->domain)
1316
    {
1317
      he->domain = 0;
1318

    
1319
      if ((he->color != INSPF) && reachable)
1320
        continue;
1321

    
1322
      if (!hdr)
1323
      {
1324
        cli_msg(-1016, "");
1325
        cli_msg(-1016, "other ASBRs");
1326
        hdr = 1;
1327
      }
1328

    
1329
      if (he->lsa.rt != last_rt)
1330
      {
1331
        cli_msg(-1016, "");
1332
        cli_msg(-1016, "\trouter %R", he->lsa.rt);
1333
        last_rt = he->lsa.rt;
1334
      }
1335

    
1336
      show_lsa_external(he, ospf2);
1337
    }
1338
  }
1339

    
1340
  cli_msg(0, "");
1341
}
1342

    
1343

    
1344
static int
1345
lsa_compare_for_lsadb(const void *p1, const void *p2)
1346
{
1347
  struct top_hash_entry * he1 = * (struct top_hash_entry **) p1;
1348
  struct top_hash_entry * he2 = * (struct top_hash_entry **) p2;
1349
  struct ospf_lsa_header *lsa1 = &(he1->lsa);
1350
  struct ospf_lsa_header *lsa2 = &(he2->lsa);
1351
  int sc1 = LSA_SCOPE(he1->lsa_type);
1352
  int sc2 = LSA_SCOPE(he2->lsa_type);
1353

    
1354
  if (sc1 != sc2)
1355
    return sc2 - sc1;
1356

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

    
1360
  if (lsa1->rt != lsa2->rt)
1361
    return lsa1->rt - lsa2->rt;
1362

    
1363
  if (lsa1->id != lsa2->id)
1364
    return lsa1->id - lsa2->id;
1365

    
1366
  if (he1->lsa_type != he2->lsa_type)
1367
    return he1->lsa_type - he2->lsa_type;
1368

    
1369
  return lsa1->sn - lsa2->sn;
1370
}
1371

    
1372
void
1373
ospf_sh_lsadb(struct lsadb_show_data *ld)
1374
{
1375
  struct ospf_proto *p = (struct ospf_proto *) proto_get_named(ld->name, &proto_ospf);
1376
  uint num = p->gr->hash_entries;
1377
  uint i, j;
1378
  int last_dscope = -1;
1379
  u32 last_domain = 0;
1380
  u16 type_mask = ospf_is_v2(p) ?  0x00ff : 0xffff;        /* see lsa_etype() */
1381

    
1382
  if (p->p.proto_state != PS_UP)
1383
  {
1384
    cli_msg(-1017, "%s: is not up", p->p.name);
1385
    cli_msg(0, "");
1386
    return;
1387
  }
1388

    
1389
  if (ld->router == SH_ROUTER_SELF)
1390
    ld->router = p->router_id;
1391

    
1392
  struct top_hash_entry *hea[num];
1393
  struct top_hash_entry *he;
1394

    
1395
  j = 0;
1396
  WALK_SLIST(he, p->lsal)
1397
    if (he->lsa_body)
1398
      hea[j++] = he;
1399

    
1400
  ASSERT(j <= num);
1401

    
1402
  qsort(hea, j, sizeof(struct top_hash_entry *), lsa_compare_for_lsadb);
1403

    
1404
  for (i = 0; i < j; i++)
1405
  {
1406
    struct ospf_lsa_header *lsa = &(hea[i]->lsa);
1407
    u16 lsa_type = lsa->type_raw & type_mask;
1408
    u16 dscope = LSA_SCOPE(hea[i]->lsa_type);
1409

    
1410
    /* Hack: 1 is used for LSA_SCOPE_LINK, fixed by & 0xf000 */
1411
    if (ld->scope && (dscope != (ld->scope & 0xf000)))
1412
      continue;
1413

    
1414
    if ((ld->scope == LSA_SCOPE_AREA) && (hea[i]->domain != ld->area))
1415
      continue;
1416

    
1417
    /* For user convenience ignore high nibble */
1418
    if (ld->type && ((lsa_type & 0x0fff) != (ld->type & 0x0fff)))
1419
      continue;
1420

    
1421
    if (ld->lsid && (lsa->id != ld->lsid))
1422
      continue;
1423

    
1424
    if (ld->router && (lsa->rt != ld->router))
1425
      continue;
1426

    
1427
    if ((dscope != last_dscope) || (hea[i]->domain != last_domain))
1428
    {
1429
      cli_msg(-1017, "");
1430
      switch (dscope)
1431
      {
1432
      case LSA_SCOPE_AS:
1433
        cli_msg(-1017, "Global");
1434
        break;
1435

    
1436
      case LSA_SCOPE_AREA:
1437
        cli_msg(-1017, "Area %R", hea[i]->domain);
1438
        break;
1439

    
1440
      case LSA_SCOPE_LINK:
1441
        {
1442
          struct iface *ifa = if_find_by_index(hea[i]->domain);
1443
          cli_msg(-1017, "Link %s", (ifa != NULL) ? ifa->name : "?");
1444
        }
1445
        break;
1446
      }
1447
      cli_msg(-1017, "");
1448
      cli_msg(-1017," Type   LS ID           Router          Sequence   Age  Checksum");
1449

    
1450
      last_dscope = dscope;
1451
      last_domain = hea[i]->domain;
1452
    }
1453

    
1454
    cli_msg(-1017," %04x  %-15R %-15R  %08x %5u    %04x",
1455
            lsa_type, lsa->id, lsa->rt, lsa->sn, lsa->age, lsa->checksum);
1456
  }
1457
  cli_msg(0, "");
1458
}
1459

    
1460

    
1461
struct protocol proto_ospf = {
1462
  .name =                "OSPF",
1463
  .template =                "ospf%d",
1464
  .attr_class =                EAP_OSPF,
1465
  .preference =                DEF_PREF_OSPF,
1466
  .config_size =        sizeof(struct ospf_config),
1467
  .init =                ospf_init,
1468
  .dump =                ospf_dump,
1469
  .start =                ospf_start,
1470
  .shutdown =                ospf_shutdown,
1471
  .reconfigure =        ospf_reconfigure,
1472
  .get_status =                ospf_get_status,
1473
  .get_attr =                ospf_get_attr,
1474
  .get_route_info =        ospf_get_route_info
1475
};