Statistics
| Branch: | Revision:

iof-bird-daemon / proto / ospf / ospf.c @ 74c838a8

History | View | Annotate | Download (38.5 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
  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 *c)
301
{
302
  struct ospf_config *oc = (struct ospf_config *) c;
303
  struct proto *P = proto_new(c, sizeof(struct ospf_proto));
304

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

    
316
  return P;
317
}
318

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

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

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

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

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

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

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

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

    
381

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

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

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

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

    
400
  p->calcrt = 2;
401

    
402
  return 1;
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 *c)
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 *) c;
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(c) != 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
  p->stub_router = new->stub_router;
663
  p->merge_external = new->merge_external;
664
  p->asbr = new->asbr;
665
  p->ecmp = new->ecmp;
666
  p->tick = new->tick;
667
  p->disp_timer->recurrent = p->tick;
668
  tm_start(p->disp_timer, 1);
669

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

    
674
  WALK_LIST(ifa, p->iface_list)
675
    ifa->marked = 1;
676

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

    
687
  /* Add and update interfaces */
688
  ospf_reconfigure_ifaces(p);
689

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

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

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

    
712
  ospf_schedule_rtcalc(p);
713

    
714
  return 1;
715
}
716

    
717

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

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

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

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

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

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

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

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

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

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

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

    
823
  }
824
  cli_msg(0, "");
825
}
826

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

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

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

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

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

    
870
  return dst;
871
}
872

    
873

    
874
static int lsa_compare_ospf3;
875

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

    
887
  if (he1->domain < he2->domain)
888
    return -1;
889
  if (he1->domain > he2->domain)
890
    return 1;
891

    
892

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

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

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

    
909

    
910
  int nt1 = (lsa1_type == LSA_T_NET);
911
  int nt2 = (lsa2_type == LSA_T_NET);
912

    
913
  if (nt1 != nt2)
914
    return nt1 - nt2;
915

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

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

    
934
    if (px1 != px2)
935
      return px1 - px2;
936

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

    
946
    if (lsa1_type < lsa2_type)
947
      return -1;
948
    if (lsa1_type > lsa2_type)
949
      return 1;
950

    
951
    if (lsa1->id < lsa2->id)
952
      return -1;
953
    if (lsa1->id > lsa2->id)
954
      return 1;
955

    
956
    if (px1 != px2)
957
      return px1 - px2;
958

    
959
    return lsa1->sn - lsa2->sn;
960
  }
961
}
962

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

    
971
  if (lsa1->rt < lsa2->rt)
972
    return -1;
973
  if (lsa1->rt > lsa2->rt)
974
    return 1;
975

    
976
  if (lsa1->id < lsa2->id)
977
    return -1;
978
  if (lsa1->id > lsa2->id)
979
    return 1;
980

    
981
  return lsa1->sn - lsa2->sn;
982
}
983

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

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

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

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

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

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

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

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

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

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

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

    
1066
  show_lsa_distance(he);
1067

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

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

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

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

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

    
1094

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

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

    
1105
  lsa_parse_ext(he, ospf2, &rt);
1106

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

    
1110
  if (rt.tag)
1111
    bsprintf(str_tag, " tag %08x", rt.tag);
1112

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

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

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

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

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

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

    
1142
    buf = ospf_get_ipv6_prefix(buf, &net, &pxopts, &metric);
1143

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

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

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

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

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

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

    
1180
    if (he->lsa.age == LSA_MAXAGE)
1181
      continue;
1182

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

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

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

    
1207
    if (accept)
1208
      hea[j1++] = he;
1209
  }
1210

    
1211
  ASSERT(j1 <= num && jx <= num);
1212

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

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

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

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

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

    
1261
    ASSERT(cnode && (he->domain == last_area) && (he->lsa.rt == cnode->lsa.rt));
1262

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

    
1270
    case LSA_T_NET:
1271
      show_lsa_network(he, ospf2);
1272
      break;
1273

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

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

    
1284
    case LSA_T_EXT:
1285
    case LSA_T_NSSA:
1286
      show_lsa_external(he, ospf2);
1287
      break;
1288

    
1289
    case LSA_T_PREFIX:
1290
      show_lsa_prefix(he, cnode);
1291
      break;
1292
    }
1293

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

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

    
1306
      cnode = NULL;
1307
    }
1308
  }
1309

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

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

    
1321
      if ((he->color != INSPF) && reachable)
1322
        continue;
1323

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

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

    
1338
      show_lsa_external(he, ospf2);
1339
    }
1340
  }
1341

    
1342
  cli_msg(0, "");
1343
}
1344

    
1345

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

    
1356
  if (sc1 != sc2)
1357
    return sc2 - sc1;
1358

    
1359
  if (he1->domain != he2->domain)
1360
    return he1->domain - he2->domain;
1361

    
1362
  if (lsa1->rt != lsa2->rt)
1363
    return lsa1->rt - lsa2->rt;
1364

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

    
1368
  if (he1->lsa_type != he2->lsa_type)
1369
    return he1->lsa_type - he2->lsa_type;
1370

    
1371
  return lsa1->sn - lsa2->sn;
1372
}
1373

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

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

    
1391
  if (ld->router == SH_ROUTER_SELF)
1392
    ld->router = p->router_id;
1393

    
1394
  struct top_hash_entry *hea[num];
1395
  struct top_hash_entry *he;
1396

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

    
1402
  ASSERT(j <= num);
1403

    
1404
  qsort(hea, j, sizeof(struct top_hash_entry *), lsa_compare_for_lsadb);
1405

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

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

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

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

    
1423
    if (ld->lsid && (lsa->id != ld->lsid))
1424
      continue;
1425

    
1426
    if (ld->router && (lsa->rt != ld->router))
1427
      continue;
1428

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

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

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

    
1452
      last_dscope = dscope;
1453
      last_domain = hea[i]->domain;
1454
    }
1455

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

    
1462

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