Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (38.6 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 void ospf_reload_routes(struct channel *C);
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
  if (ospf_is_v3(p))
244
    idm_init(&p->idm, P->pool, 16);
245
  p->areano = 0;
246
  p->gr = ospf_top_new(p, P->pool);
247
  s_init_list(&(p->lsal));
248

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

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

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

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

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

    
267
  return PS_UP;
268
}
269

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

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

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

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

    
299
static struct proto *
300
ospf_init(struct proto_config *CF)
301
{
302
  struct ospf_config *cf = (struct ospf_config *) CF;
303
  struct proto *P = proto_new(CF);
304

    
305
  P->main_channel = proto_add_channel(P, proto_cf_main_channel(CF));
306

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

    
317
  return P;
318
}
319

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

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

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

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

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

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

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

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

    
382

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

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

    
393
static void
394
ospf_reload_routes(struct channel *C)
395
{
396
  struct ospf_proto *p = (struct ospf_proto *) C->proto;
397

    
398
  if (p->calcrt == 2)
399
    return;
400

    
401
  OSPF_TRACE(D_EVENTS, "Scheduling routing table calculation with route reload");
402
  p->calcrt = 2;
403
}
404

    
405

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

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

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

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

    
427

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

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

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

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

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

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

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

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

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

    
500
  OSPF_TRACE(D_EVENTS, "Shutdown requested");
501

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

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

    
513
  return PS_DOWN;
514
}
515

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

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

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

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

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

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

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

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

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

    
603
  oa->ac = nac;
604

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

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

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

    
623
  /* No need to handle stubnet_list */
624

    
625
  oa->marked = 0;
626
  ospf_notify_rt_lsa(oa);
627
}
628

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

    
650
  if (proto_get_router_id(CF) != p->router_id)
651
    return 0;
652

    
653
  if (p->ospf2 != new->ospf2)
654
    return 0;
655

    
656
  if (p->rfc1583 != new->rfc1583)
657
    return 0;
658

    
659
  if (old->abr != new->abr)
660
    return 0;
661

    
662
  if (!proto_configure_channel(P, &P->main_channel, proto_cf_main_channel(CF)))
663
    return 0;
664

    
665
  p->stub_router = new->stub_router;
666
  p->merge_external = new->merge_external;
667
  p->asbr = new->asbr;
668
  p->ecmp = new->ecmp;
669
  p->tick = new->tick;
670
  p->disp_timer->recurrent = p->tick;
671
  tm_start(p->disp_timer, 1);
672

    
673
  /* Mark all areas and ifaces */
674
  WALK_LIST(oa, p->area_list)
675
    oa->marked = 1;
676

    
677
  WALK_LIST(ifa, p->iface_list)
678
    ifa->marked = 1;
679

    
680
  /* Add and update areas */
681
  WALK_LIST(nac, new->area_list)
682
  {
683
    oa = ospf_find_area(p, nac->areaid);
684
    if (oa)
685
      ospf_area_reconfigure(oa, nac);
686
    else
687
      ospf_area_add(p, nac);
688
  }
689

    
690
  /* Add and update interfaces */
691
  ospf_reconfigure_ifaces(p);
692

    
693
  /* Add and update vlinks */
694
  WALK_LIST(ip, new->vlink_list)
695
  {
696
    ifa = ospf_find_vlink(p, ip->voa, ip->vid);
697
    if (ifa)
698
      ospf_iface_reconfigure(ifa, ip);
699
    else
700
      ospf_iface_new_vlink(p, ip);
701
  }
702

    
703
  /* Delete remaining ifaces and areas */
704
  WALK_LIST_DELSAFE(ifa, ifx, p->iface_list)
705
    if (ifa->marked)
706
    {
707
      ospf_iface_shutdown(ifa);
708
      ospf_iface_remove(ifa);
709
    }
710

    
711
  WALK_LIST_DELSAFE(oa, oax, p->area_list)
712
    if (oa->marked)
713
      ospf_area_remove(oa);
714

    
715
  ospf_schedule_rtcalc(p);
716

    
717
  return 1;
718
}
719

    
720

    
721
void
722
ospf_sh_neigh(struct proto *P, char *iff)
723
{
724
  struct ospf_proto *p = (struct ospf_proto *) P;
725
  struct ospf_iface *ifa = NULL;
726
  struct ospf_neighbor *n;
727

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

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

    
745
void
746
ospf_sh(struct proto *P)
747
{
748
  struct ospf_proto *p = (struct ospf_proto *) P;
749
  struct ospf_area *oa;
750
  struct ospf_iface *ifa;
751
  struct ospf_neighbor *n;
752
  int ifano, nno, adjno, firstfib;
753

    
754
  if (p->p.proto_state != PS_UP)
755
  {
756
    cli_msg(-1014, "%s: is not up", p->p.name);
757
    cli_msg(0, "");
758
    return;
759
  }
760

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

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

    
789
    cli_msg(-1014, "\t\tStub:\t%s", oa_is_stub(oa) ? "Yes" : "No");
790
    cli_msg(-1014, "\t\tNSSA:\t%s", oa_is_nssa(oa) ? "Yes" : "No");
791
    cli_msg(-1014, "\t\tTransit:\t%s", oa->trcap ? "Yes" : "No");
792

    
793
    if (oa_is_nssa(oa))
794
      cli_msg(-1014, "\t\tNSSA translation:\t%s%s", oa->translate ? "Yes" : "No",
795
              oa->translate == TRANS_WAIT ? " (run down)" : "");
796
    cli_msg(-1014, "\t\tNumber of interfaces:\t%u", ifano);
797
    cli_msg(-1014, "\t\tNumber of neighbors:\t%u", nno);
798
    cli_msg(-1014, "\t\tNumber of adjacent neighbors:\t%u", adjno);
799

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

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

    
826
  }
827
  cli_msg(0, "");
828
}
829

    
830
void
831
ospf_sh_iface(struct proto *P, char *iff)
832
{
833
  struct ospf_proto *p = (struct ospf_proto *) P;
834
  struct ospf_iface *ifa = NULL;
835

    
836
  if (p->p.proto_state != PS_UP)
837
  {
838
    cli_msg(-1015, "%s: is not up", p->p.name);
839
    cli_msg(0, "");
840
    return;
841
  }
842

    
843
  cli_msg(-1015, "%s:", p->p.name);
844
  WALK_LIST(ifa, p->iface_list)
845
    if ((iff == NULL) || patmatch(iff, ifa->ifname))
846
      ospf_iface_info(ifa);
847
  cli_msg(0, "");
848
}
849

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

    
863
static struct ospf_lsa_header *
864
fake_lsa_from_prefix_lsa(struct ospf_lsa_header *dst, struct ospf_lsa_header *src,
865
                         struct ospf_lsa_prefix *px)
866
{
867
  dst->age = src->age;
868
  dst->type_raw = px->ref_type;
869
  dst->id = px->ref_id;
870
  dst->rt = px->ref_rt;
871
  dst->sn = src->sn;
872

    
873
  return dst;
874
}
875

    
876

    
877
static int lsa_compare_ospf3;
878

    
879
static int
880
lsa_compare_for_state(const void *p1, const void *p2)
881
{
882
  struct top_hash_entry *he1 = * (struct top_hash_entry **) p1;
883
  struct top_hash_entry *he2 = * (struct top_hash_entry **) p2;
884
  struct ospf_lsa_header *lsa1 = &(he1->lsa);
885
  struct ospf_lsa_header *lsa2 = &(he2->lsa);
886
  struct ospf_lsa_header lsatmp1, lsatmp2;
887
  u16 lsa1_type = he1->lsa_type;
888
  u16 lsa2_type = he2->lsa_type;
889

    
890
  if (he1->domain < he2->domain)
891
    return -1;
892
  if (he1->domain > he2->domain)
893
    return 1;
894

    
895

    
896
  /* px1 or px2 assumes OSPFv3 */
897
  int px1 = (lsa1_type == LSA_T_PREFIX);
898
  int px2 = (lsa2_type == LSA_T_PREFIX);
899

    
900
  if (px1)
901
  {
902
    lsa1 = fake_lsa_from_prefix_lsa(&lsatmp1, lsa1, he1->lsa_body);
903
    lsa1_type = lsa1->type_raw;        /* FIXME: handle unknown ref_type */
904
  }
905

    
906
  if (px2)
907
  {
908
    lsa2 = fake_lsa_from_prefix_lsa(&lsatmp2, lsa2, he2->lsa_body);
909
    lsa2_type = lsa2->type_raw;
910
  }
911

    
912

    
913
  int nt1 = (lsa1_type == LSA_T_NET);
914
  int nt2 = (lsa2_type == LSA_T_NET);
915

    
916
  if (nt1 != nt2)
917
    return nt1 - nt2;
918

    
919
  if (nt1)
920
  {
921
    /* In OSPFv3, networks are named based on ID of DR */
922
    if (lsa_compare_ospf3)
923
    {
924
      if (lsa1->rt < lsa2->rt)
925
        return -1;
926
      if (lsa1->rt > lsa2->rt)
927
        return 1;
928
    }
929

    
930
    /* For OSPFv2, this is IP of the network,
931
       for OSPFv3, this is interface ID */
932
    if (lsa1->id < lsa2->id)
933
      return -1;
934
    if (lsa1->id > lsa2->id)
935
      return 1;
936

    
937
    if (px1 != px2)
938
      return px1 - px2;
939

    
940
    return lsa1->sn - lsa2->sn;
941
  }
942
  else
943
  {
944
    if (lsa1->rt < lsa2->rt)
945
      return -1;
946
    if (lsa1->rt > lsa2->rt)
947
      return 1;
948

    
949
    if (lsa1_type < lsa2_type)
950
      return -1;
951
    if (lsa1_type > lsa2_type)
952
      return 1;
953

    
954
    if (lsa1->id < lsa2->id)
955
      return -1;
956
    if (lsa1->id > lsa2->id)
957
      return 1;
958

    
959
    if (px1 != px2)
960
      return px1 - px2;
961

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

    
966
static int
967
ext_compare_for_state(const void *p1, const void *p2)
968
{
969
  struct top_hash_entry * he1 = * (struct top_hash_entry **) p1;
970
  struct top_hash_entry * he2 = * (struct top_hash_entry **) p2;
971
  struct ospf_lsa_header *lsa1 = &(he1->lsa);
972
  struct ospf_lsa_header *lsa2 = &(he2->lsa);
973

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

    
979
  if (lsa1->id < lsa2->id)
980
    return -1;
981
  if (lsa1->id > lsa2->id)
982
    return 1;
983

    
984
  return lsa1->sn - lsa2->sn;
985
}
986

    
987
static inline void
988
show_lsa_distance(struct top_hash_entry *he)
989
{
990
  if (he->color == INSPF)
991
    cli_msg(-1016, "\t\tdistance %u", he->dist);
992
  else
993
    cli_msg(-1016, "\t\tunreachable");
994
}
995

    
996
static inline void
997
show_lsa_router(struct ospf_proto *p, struct top_hash_entry *he, int verbose)
998
{
999
  struct ospf_lsa_rt_walk rtl;
1000

    
1001
  cli_msg(-1016, "");
1002
  cli_msg(-1016, "\trouter %R", he->lsa.rt);
1003
  show_lsa_distance(he);
1004

    
1005
  lsa_walk_rt_init(p, he, &rtl);
1006
  while (lsa_walk_rt(&rtl))
1007
    if (rtl.type == LSART_VLNK)
1008
      cli_msg(-1016, "\t\tvlink %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_PTP)
1013
      cli_msg(-1016, "\t\trouter %R metric %u", rtl.id, rtl.metric);
1014

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

    
1024
        if (net_he && (net_he->lsa.age < LSA_MAXAGE))
1025
        {
1026
          struct ospf_lsa_header *net_lsa = &(net_he->lsa);
1027
          struct ospf_lsa_net *net_ln = net_he->lsa_body;
1028

    
1029
          cli_msg(-1016, "\t\tnetwork %I/%d metric %u",
1030
                  ipa_from_u32(net_lsa->id & net_ln->optx),
1031
                  u32_masklen(net_ln->optx), rtl.metric);
1032
        }
1033
        else
1034
          cli_msg(-1016, "\t\tnetwork [%R] metric %u", rtl.id, rtl.metric);
1035
      }
1036
      else
1037
        cli_msg(-1016, "\t\tnetwork [%R-%u] metric %u", rtl.id, rtl.nif, rtl.metric);
1038
    }
1039

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

    
1050
static inline void
1051
show_lsa_network(struct top_hash_entry *he, int ospf2)
1052
{
1053
  struct ospf_lsa_header *lsa = &(he->lsa);
1054
  struct ospf_lsa_net *ln = he->lsa_body;
1055
  u32 i;
1056

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

    
1069
  show_lsa_distance(he);
1070

    
1071
  for (i = 0; i < lsa_net_count(lsa); i++)
1072
    cli_msg(-1016, "\t\trouter %R", ln->routers[i]);
1073
}
1074

    
1075
static inline void
1076
show_lsa_sum_net(struct top_hash_entry *he, int ospf2)
1077
{
1078
  net_addr net;
1079
  u8 pxopts;
1080
  u32 metric;
1081

    
1082
  lsa_parse_sum_net(he, ospf2, &net, &pxopts, &metric);
1083
  cli_msg(-1016, "\t\txnetwork %N metric %u", &net, metric);
1084
}
1085

    
1086
static inline void
1087
show_lsa_sum_rt(struct top_hash_entry *he, int ospf2)
1088
{
1089
  u32 metric;
1090
  u32 dst_rid;
1091
  u32 options;
1092

    
1093
  lsa_parse_sum_rt(he, ospf2, &dst_rid, &metric, &options);
1094
  cli_msg(-1016, "\t\txrouter %R metric %u", dst_rid, metric);
1095
}
1096

    
1097

    
1098
static inline void
1099
show_lsa_external(struct top_hash_entry *he, int ospf2)
1100
{
1101
  struct ospf_lsa_ext_local rt;
1102
  char str_via[IPA_MAX_TEXT_LENGTH + 8] = "";
1103
  char str_tag[16] = "";
1104

    
1105
  if (he->lsa_type == LSA_T_EXT)
1106
    he->domain = 0; /* Unmark the LSA */
1107

    
1108
  lsa_parse_ext(he, ospf2, &rt);
1109

    
1110
  if (rt.fbit)
1111
    bsprintf(str_via, " via %I", rt.fwaddr);
1112

    
1113
  if (rt.tag)
1114
    bsprintf(str_tag, " tag %08x", rt.tag);
1115

    
1116
  cli_msg(-1016, "\t\t%s %N metric%s %u%s%s",
1117
          (he->lsa_type == LSA_T_NSSA) ? "nssa-ext" : "external",
1118
          &rt.net, rt.ebit ? "2" : "", rt.metric, str_via, str_tag);
1119
}
1120

    
1121
static inline void
1122
show_lsa_prefix(struct top_hash_entry *he, struct top_hash_entry *cnode)
1123
{
1124
  struct ospf_lsa_prefix *px = he->lsa_body;
1125
  u32 *buf;
1126
  int i;
1127

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

    
1132
  if ((px->ref_type == LSA_T_RT) && (px->ref_id != 0))
1133
    return;
1134

    
1135
  if ((px->ref_type == LSA_T_NET) && (px->ref_id != cnode->lsa.id))
1136
    return;
1137

    
1138
  buf = px->rest;
1139
  for (i = 0; i < px->pxcount; i++)
1140
  {
1141
    net_addr net;
1142
    u8 pxopts;
1143
    u16 metric;
1144

    
1145
    buf = ospf_get_ipv6_prefix(buf, &net, &pxopts, &metric);
1146

    
1147
    if (px->ref_type == LSA_T_RT)
1148
      cli_msg(-1016, "\t\tstubnet %N metric %u", &net, metric);
1149
    else
1150
      cli_msg(-1016, "\t\taddress %N", &net);
1151
  }
1152
}
1153

    
1154
void
1155
ospf_sh_state(struct proto *P, int verbose, int reachable)
1156
{
1157
  struct ospf_proto *p = (struct ospf_proto *) P;
1158
  int ospf2 = ospf_is_v2(p);
1159
  uint i, ix, j1, jx;
1160
  u32 last_area = 0xFFFFFFFF;
1161

    
1162
  if (p->p.proto_state != PS_UP)
1163
  {
1164
    cli_msg(-1016, "%s: is not up", p->p.name);
1165
    cli_msg(0, "");
1166
    return;
1167
  }
1168

    
1169
  /* We store interesting area-scoped LSAs in array hea and
1170
     global-scoped (LSA_T_EXT) LSAs in array hex */
1171

    
1172
  int num = p->gr->hash_entries;
1173
  struct top_hash_entry *hea[num];
1174
  struct top_hash_entry *hex[verbose ? num : 0];
1175
  struct top_hash_entry *he;
1176
  struct top_hash_entry *cnode = NULL;
1177

    
1178
  j1 = jx = 0;
1179
  WALK_SLIST(he, p->lsal)
1180
  {
1181
    int accept;
1182

    
1183
    if (he->lsa.age == LSA_MAXAGE)
1184
      continue;
1185

    
1186
    switch (he->lsa_type)
1187
    {
1188
    case LSA_T_RT:
1189
    case LSA_T_NET:
1190
      accept = 1;
1191
      break;
1192

    
1193
    case LSA_T_SUM_NET:
1194
    case LSA_T_SUM_RT:
1195
    case LSA_T_NSSA:
1196
    case LSA_T_PREFIX:
1197
      accept = verbose;
1198
      break;
1199

    
1200
    case LSA_T_EXT:
1201
      if (verbose)
1202
      {
1203
        he->domain = 1; /* Abuse domain field to mark the LSA */
1204
        hex[jx++] = he;
1205
      }
1206
    default:
1207
      accept = 0;
1208
    }
1209

    
1210
    if (accept)
1211
      hea[j1++] = he;
1212
  }
1213

    
1214
  ASSERT(j1 <= num && jx <= num);
1215

    
1216
  lsa_compare_ospf3 = !ospf2;
1217
  qsort(hea, j1, sizeof(struct top_hash_entry *), lsa_compare_for_state);
1218
  qsort(hex, jx, sizeof(struct top_hash_entry *), ext_compare_for_state);
1219

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

    
1239
  ix = 0;
1240
  for (i = 0; i < j1; i++)
1241
  {
1242
    he = hea[i];
1243

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

    
1252
        if (he->domain != last_area)
1253
        {
1254
          cli_msg(-1016, "");
1255
          cli_msg(-1016, "area %R", he->domain);
1256
          last_area = he->domain;
1257
          ix = 0;
1258
        }
1259
      }
1260
      else
1261
        continue;
1262
    }
1263

    
1264
    ASSERT(cnode && (he->domain == last_area) && (he->lsa.rt == cnode->lsa.rt));
1265

    
1266
    switch (he->lsa_type)
1267
    {
1268
    case LSA_T_RT:
1269
      if (he->lsa.id == cnode->lsa.id)
1270
        show_lsa_router(p, he, verbose);
1271
      break;
1272

    
1273
    case LSA_T_NET:
1274
      show_lsa_network(he, ospf2);
1275
      break;
1276

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

    
1282
    case LSA_T_SUM_RT:
1283
      if (cnode->lsa_type == LSA_T_RT)
1284
        show_lsa_sum_rt(he, ospf2);
1285
      break;
1286

    
1287
    case LSA_T_EXT:
1288
    case LSA_T_NSSA:
1289
      show_lsa_external(he, ospf2);
1290
      break;
1291

    
1292
    case LSA_T_PREFIX:
1293
      show_lsa_prefix(he, cnode);
1294
      break;
1295
    }
1296

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

    
1306
      while ((ix < jx) && (hex[ix]->lsa.rt == cnode->lsa.rt))
1307
        show_lsa_external(hex[ix++], ospf2);
1308

    
1309
      cnode = NULL;
1310
    }
1311
  }
1312

    
1313
  int hdr = 0;
1314
  u32 last_rt = 0xFFFFFFFF;
1315
  for (ix = 0; ix < jx; ix++)
1316
  {
1317
    he = hex[ix];
1318

    
1319
    /* If it is still marked, we show it now. */
1320
    if (he->domain)
1321
    {
1322
      he->domain = 0;
1323

    
1324
      if ((he->color != INSPF) && reachable)
1325
        continue;
1326

    
1327
      if (!hdr)
1328
      {
1329
        cli_msg(-1016, "");
1330
        cli_msg(-1016, "other ASBRs");
1331
        hdr = 1;
1332
      }
1333

    
1334
      if (he->lsa.rt != last_rt)
1335
      {
1336
        cli_msg(-1016, "");
1337
        cli_msg(-1016, "\trouter %R", he->lsa.rt);
1338
        last_rt = he->lsa.rt;
1339
      }
1340

    
1341
      show_lsa_external(he, ospf2);
1342
    }
1343
  }
1344

    
1345
  cli_msg(0, "");
1346
}
1347

    
1348

    
1349
static int
1350
lsa_compare_for_lsadb(const void *p1, const void *p2)
1351
{
1352
  struct top_hash_entry * he1 = * (struct top_hash_entry **) p1;
1353
  struct top_hash_entry * he2 = * (struct top_hash_entry **) p2;
1354
  struct ospf_lsa_header *lsa1 = &(he1->lsa);
1355
  struct ospf_lsa_header *lsa2 = &(he2->lsa);
1356
  int sc1 = LSA_SCOPE(he1->lsa_type);
1357
  int sc2 = LSA_SCOPE(he2->lsa_type);
1358

    
1359
  if (sc1 != sc2)
1360
    return sc2 - sc1;
1361

    
1362
  if (he1->domain != he2->domain)
1363
    return he1->domain - he2->domain;
1364

    
1365
  if (lsa1->rt != lsa2->rt)
1366
    return lsa1->rt - lsa2->rt;
1367

    
1368
  if (lsa1->id != lsa2->id)
1369
    return lsa1->id - lsa2->id;
1370

    
1371
  if (he1->lsa_type != he2->lsa_type)
1372
    return he1->lsa_type - he2->lsa_type;
1373

    
1374
  return lsa1->sn - lsa2->sn;
1375
}
1376

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

    
1387
  if (p->p.proto_state != PS_UP)
1388
  {
1389
    cli_msg(-1017, "%s: is not up", p->p.name);
1390
    cli_msg(0, "");
1391
    return;
1392
  }
1393

    
1394
  if (ld->router == SH_ROUTER_SELF)
1395
    ld->router = p->router_id;
1396

    
1397
  struct top_hash_entry *hea[num];
1398
  struct top_hash_entry *he;
1399

    
1400
  j = 0;
1401
  WALK_SLIST(he, p->lsal)
1402
    if (he->lsa_body)
1403
      hea[j++] = he;
1404

    
1405
  ASSERT(j <= num);
1406

    
1407
  qsort(hea, j, sizeof(struct top_hash_entry *), lsa_compare_for_lsadb);
1408

    
1409
  for (i = 0; i < j; i++)
1410
  {
1411
    struct ospf_lsa_header *lsa = &(hea[i]->lsa);
1412
    u16 lsa_type = lsa->type_raw & type_mask;
1413
    u16 dscope = LSA_SCOPE(hea[i]->lsa_type);
1414

    
1415
    /* Hack: 1 is used for LSA_SCOPE_LINK, fixed by & 0xf000 */
1416
    if (ld->scope && (dscope != (ld->scope & 0xf000)))
1417
      continue;
1418

    
1419
    if ((ld->scope == LSA_SCOPE_AREA) && (hea[i]->domain != ld->area))
1420
      continue;
1421

    
1422
    /* For user convenience ignore high nibble */
1423
    if (ld->type && ((lsa_type & 0x0fff) != (ld->type & 0x0fff)))
1424
      continue;
1425

    
1426
    if (ld->lsid && (lsa->id != ld->lsid))
1427
      continue;
1428

    
1429
    if (ld->router && (lsa->rt != ld->router))
1430
      continue;
1431

    
1432
    if ((dscope != last_dscope) || (hea[i]->domain != last_domain))
1433
    {
1434
      cli_msg(-1017, "");
1435
      switch (dscope)
1436
      {
1437
      case LSA_SCOPE_AS:
1438
        cli_msg(-1017, "Global");
1439
        break;
1440

    
1441
      case LSA_SCOPE_AREA:
1442
        cli_msg(-1017, "Area %R", hea[i]->domain);
1443
        break;
1444

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

    
1455
      last_dscope = dscope;
1456
      last_domain = hea[i]->domain;
1457
    }
1458

    
1459
    cli_msg(-1017," %04x  %-15R %-15R  %08x %5u    %04x",
1460
            lsa_type, lsa->id, lsa->rt, lsa->sn, lsa->age, lsa->checksum);
1461
  }
1462
  cli_msg(0, "");
1463
}
1464

    
1465

    
1466
struct protocol proto_ospf = {
1467
  .name =                "OSPF",
1468
  .template =                "ospf%d",
1469
  .attr_class =                EAP_OSPF,
1470
  .preference =                DEF_PREF_OSPF,
1471
  .channel_mask =        NB_IP,
1472
  .proto_size =                sizeof(struct ospf_proto),
1473
  .config_size =        sizeof(struct ospf_config),
1474
  .init =                ospf_init,
1475
  .dump =                ospf_dump,
1476
  .start =                ospf_start,
1477
  .shutdown =                ospf_shutdown,
1478
  .reconfigure =        ospf_reconfigure,
1479
  .get_status =                ospf_get_status,
1480
  .get_attr =                ospf_get_attr,
1481
  .get_route_info =        ospf_get_route_info
1482
};