Statistics
| Branch: | Revision:

iof-bird / bird-2.0.1 / proto / ospf / ospf.c @ 6b3f1a54

History | View | Annotate | Download (39 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 5709 - OSPFv2 HMAC-SHA Cryptographic Authentication
96
 * - RFC 5838 - OSPFv3 Support of Address Families
97
 * - RFC 6549 - OSPFv2 Multi-Instance Extensions
98
 * - RFC 6987 - OSPF Stub Router Advertisement
99
 */
100

    
101
#include <stdlib.h>
102
#include "ospf.h"
103

    
104
static int ospf_import_control(struct proto *P, rte **new, ea_list **attrs, struct linpool *pool);
105
static struct ea_list *ospf_make_tmp_attrs(struct rte *rt, struct linpool *pool);
106
static void ospf_store_tmp_attrs(struct rte *rt, struct ea_list *attrs);
107
static void ospf_reload_routes(struct channel *C);
108
static int ospf_rte_better(struct rte *new, struct rte *old);
109
static int ospf_rte_same(struct rte *new, struct rte *old);
110
static void ospf_disp(timer *timer);
111

    
112

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

    
120
  fib_init(&oa->net_fib,  p->p.pool, ospf_get_af(p),
121
           sizeof(struct area_net), OFFSETOF(struct area_net, fn), 0, NULL);
122
  fib_init(&oa->enet_fib, p->p.pool, ospf_get_af(p),
123
           sizeof(struct area_net), OFFSETOF(struct area_net, fn), 0, NULL);
124

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

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

    
139
static inline uint
140
ospf_opts(struct ospf_proto *p)
141
{
142
  if (ospf_is_v2(p))
143
    return 0;
144

    
145
  return ((ospf_is_ip6(p) && !p->af_mc) ? OPT_V6 : 0) |
146
    (!p->stub_router ? OPT_R : 0) | (p->af_ext ? OPT_AF : 0);
147
}
148

    
149
static void
150
ospf_area_add(struct ospf_proto *p, struct ospf_area_config *ac)
151
{
152
  struct ospf_area *oa;
153

    
154
  OSPF_TRACE(D_EVENTS, "Adding area %R", ac->areaid);
155

    
156
  oa = mb_allocz(p->p.pool, sizeof(struct ospf_area));
157
  add_tail(&p->area_list, NODE oa);
158
  p->areano++;
159

    
160
  oa->ac = ac;
161
  oa->areaid = ac->areaid;
162
  oa->rt = NULL;
163
  oa->po = p;
164
  fib_init(&oa->rtr, p->p.pool, NET_IP4, sizeof(ort), OFFSETOF(ort, fn), 0, NULL);
165
  add_area_nets(oa, ac);
166

    
167
  if (oa->areaid == 0)
168
    p->backbone = oa;
169

    
170
  oa->options = ac->type | ospf_opts(p);
171

    
172
  ospf_notify_rt_lsa(oa);
173
}
174

    
175
static void
176
ospf_flush_area(struct ospf_proto *p, u32 areaid)
177
{
178
  struct top_hash_entry *en;
179

    
180
  WALK_SLIST(en, p->lsal)
181
    if ((LSA_SCOPE(en->lsa_type) == LSA_SCOPE_AREA) && (en->domain == areaid))
182
      ospf_flush_lsa(p, en);
183
}
184

    
185
static void
186
ospf_area_remove(struct ospf_area *oa)
187
{
188
  struct ospf_proto *p = oa->po;
189
  OSPF_TRACE(D_EVENTS, "Removing area %R", oa->areaid);
190

    
191
  /* We suppose that interfaces are already removed */
192
  ospf_flush_area(p, oa->areaid);
193

    
194
  fib_free(&oa->rtr);
195
  fib_free(&oa->net_fib);
196
  fib_free(&oa->enet_fib);
197

    
198
  if (oa->translator_timer)
199
    rfree(oa->translator_timer);
200

    
201
  p->areano--;
202
  rem_node(NODE oa);
203
  mb_free(oa);
204
}
205

    
206

    
207
struct ospf_area *
208
ospf_find_area(struct ospf_proto *p, u32 aid)
209
{
210
  struct ospf_area *oa;
211
  WALK_LIST(oa, p->area_list)
212
    if (((struct ospf_area *) oa)->areaid == aid)
213
      return oa;
214
  return NULL;
215
}
216

    
217
static struct ospf_iface *
218
ospf_find_vlink(struct ospf_proto *p, u32 voa, u32 vid)
219
{
220
  struct ospf_iface *ifa;
221
  WALK_LIST(ifa, p->iface_list)
222
    if ((ifa->type == OSPF_IT_VLINK) && (ifa->voa->areaid == voa) && (ifa->vid == vid))
223
      return ifa;
224
  return NULL;
225
}
226

    
227
static int
228
ospf_start(struct proto *P)
229
{
230
  struct ospf_proto *p = (struct ospf_proto *) P;
231
  struct ospf_config *c = (struct ospf_config *) (P->cf);
232
  struct ospf_area_config *ac;
233

    
234
  p->router_id = proto_get_router_id(P->cf);
235
  p->ospf2 = c->ospf2;
236
  p->af_ext = c->af_ext;
237
  p->af_mc = c->af_mc;
238
  p->rfc1583 = c->rfc1583;
239
  p->stub_router = c->stub_router;
240
  p->merge_external = c->merge_external;
241
  p->asbr = c->asbr;
242
  p->ecmp = c->ecmp;
243
  p->tick = c->tick;
244
  p->disp_timer = tm_new_init(P->pool, ospf_disp, p, p->tick S, 0);
245
  tm_start(p->disp_timer, 100 MS);
246
  p->lsab_size = 256;
247
  p->lsab_used = 0;
248
  p->lsab = mb_alloc(P->pool, p->lsab_size);
249
  p->nhpool = lp_new(P->pool, 12*sizeof(struct nexthop));
250
  init_list(&(p->iface_list));
251
  init_list(&(p->area_list));
252
  fib_init(&p->rtf, P->pool, ospf_get_af(p), sizeof(ort), OFFSETOF(ort, fn), 0, NULL);
253
  if (ospf_is_v3(p))
254
    idm_init(&p->idm, P->pool, 16);
255
  p->areano = 0;
256
  p->gr = ospf_top_new(p, P->pool);
257
  s_init_list(&(p->lsal));
258

    
259
  p->flood_event = ev_new(P->pool);
260
  p->flood_event->hook = ospf_flood_event;
261
  p->flood_event->data = p;
262

    
263
  p->log_pkt_tbf = (struct tbf){ .rate = 1, .burst = 5 };
264
  p->log_lsa_tbf = (struct tbf){ .rate = 4, .burst = 20 };
265

    
266
  WALK_LIST(ac, c->area_list)
267
    ospf_area_add(p, ac);
268

    
269
  if (c->abr)
270
    ospf_open_vlink_sk(p);
271

    
272
  /* Add all virtual links */
273
  struct ospf_iface_patt *ic;
274
  WALK_LIST(ic, c->vlink_list)
275
    ospf_iface_new_vlink(p, ic);
276

    
277
  return PS_UP;
278
}
279

    
280
static void
281
ospf_dump(struct proto *P)
282
{
283
  struct ospf_proto *p = (struct ospf_proto *) P;
284
  struct ospf_iface *ifa;
285
  struct ospf_neighbor *n;
286

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

    
289
  WALK_LIST(ifa, p->iface_list)
290
  {
291
    OSPF_TRACE(D_EVENTS, "Interface: %s", ifa->ifname);
292
    OSPF_TRACE(D_EVENTS, "state: %u", ifa->state);
293
    OSPF_TRACE(D_EVENTS, "DR:  %R", ifa->drid);
294
    OSPF_TRACE(D_EVENTS, "BDR: %R", ifa->bdrid);
295
    WALK_LIST(n, ifa->neigh_list)
296
    {
297
      OSPF_TRACE(D_EVENTS, "  neighbor %R in state %u", n->rid, n->state);
298
    }
299
  }
300

    
301
  /*
302
  OSPF_TRACE(D_EVENTS, "LSA graph dump start:");
303
  ospf_top_dump(p->gr, p);
304
  OSPF_TRACE(D_EVENTS, "LSA graph dump finished");
305
  */
306
  neigh_dump_all();
307
}
308

    
309
static struct proto *
310
ospf_init(struct proto_config *CF)
311
{
312
  struct ospf_config *cf = (struct ospf_config *) CF;
313
  struct proto *P = proto_new(CF);
314

    
315
  P->main_channel = proto_add_channel(P, proto_cf_main_channel(CF));
316

    
317
  P->rt_notify = ospf_rt_notify;
318
  P->if_notify = ospf_if_notify;
319
  P->ifa_notify = cf->ospf2 ? ospf_ifa_notify2 : ospf_ifa_notify3;
320
  P->import_control = ospf_import_control;
321
  P->reload_routes = ospf_reload_routes;
322
  P->make_tmp_attrs = ospf_make_tmp_attrs;
323
  P->store_tmp_attrs = ospf_store_tmp_attrs;
324
  P->rte_better = ospf_rte_better;
325
  P->rte_same = ospf_rte_same;
326

    
327
  return P;
328
}
329

    
330
/* If new is better return 1 */
331
static int
332
ospf_rte_better(struct rte *new, struct rte *old)
333
{
334
  if (new->u.ospf.metric1 == LSINFINITY)
335
    return 0;
336

    
337
  if(new->attrs->source < old->attrs->source) return 1;
338
  if(new->attrs->source > old->attrs->source) return 0;
339

    
340
  if(new->attrs->source == RTS_OSPF_EXT2)
341
  {
342
    if(new->u.ospf.metric2 < old->u.ospf.metric2) return 1;
343
    if(new->u.ospf.metric2 > old->u.ospf.metric2) return 0;
344
  }
345

    
346
  if (new->u.ospf.metric1 < old->u.ospf.metric1)
347
    return 1;
348

    
349
  return 0;                        /* Old is shorter or same */
350
}
351

    
352
static int
353
ospf_rte_same(struct rte *new, struct rte *old)
354
{
355
  /* new->attrs == old->attrs always */
356
  return
357
    new->u.ospf.metric1 == old->u.ospf.metric1 &&
358
    new->u.ospf.metric2 == old->u.ospf.metric2 &&
359
    new->u.ospf.tag == old->u.ospf.tag &&
360
    new->u.ospf.router_id == old->u.ospf.router_id;
361
}
362

    
363
static ea_list *
364
ospf_build_attrs(ea_list * next, struct linpool *pool, u32 m1, u32 m2,
365
                 u32 tag, u32 rid)
366
{
367
  struct ea_list *l =
368
    lp_alloc(pool, sizeof(struct ea_list) + 4 * sizeof(eattr));
369

    
370
  l->next = next;
371
  l->flags = EALF_SORTED;
372
  l->count = 4;
373
  l->attrs[0].id = EA_OSPF_METRIC1;
374
  l->attrs[0].flags = 0;
375
  l->attrs[0].type = EAF_TYPE_INT | EAF_TEMP;
376
  l->attrs[0].u.data = m1;
377
  l->attrs[1].id = EA_OSPF_METRIC2;
378
  l->attrs[1].flags = 0;
379
  l->attrs[1].type = EAF_TYPE_INT | EAF_TEMP;
380
  l->attrs[1].u.data = m2;
381
  l->attrs[2].id = EA_OSPF_TAG;
382
  l->attrs[2].flags = 0;
383
  l->attrs[2].type = EAF_TYPE_INT | EAF_TEMP;
384
  l->attrs[2].u.data = tag;
385
  l->attrs[3].id = EA_OSPF_ROUTER_ID;
386
  l->attrs[3].flags = 0;
387
  l->attrs[3].type = EAF_TYPE_ROUTER_ID | EAF_TEMP;
388
  l->attrs[3].u.data = rid;
389
  return l;
390
}
391

    
392

    
393
void
394
ospf_schedule_rtcalc(struct ospf_proto *p)
395
{
396
  if (p->calcrt)
397
    return;
398

    
399
  OSPF_TRACE(D_EVENTS, "Scheduling routing table calculation");
400
  p->calcrt = 1;
401
}
402

    
403
static void
404
ospf_reload_routes(struct channel *C)
405
{
406
  struct ospf_proto *p = (struct ospf_proto *) C->proto;
407

    
408
  if (p->calcrt == 2)
409
    return;
410

    
411
  OSPF_TRACE(D_EVENTS, "Scheduling routing table calculation with route reload");
412
  p->calcrt = 2;
413
}
414

    
415

    
416
/**
417
 * ospf_disp - invokes routing table calculation, aging and also area_disp()
418
 * @timer: timer usually called every @ospf_proto->tick second, @timer->data
419
 * point to @ospf_proto
420
 */
421
static void
422
ospf_disp(timer * timer)
423
{
424
  struct ospf_proto *p = timer->data;
425

    
426
  /* Originate or flush local topology LSAs */
427
  ospf_update_topology(p);
428

    
429
  /* Process LSA DB */
430
  ospf_update_lsadb(p);
431

    
432
  /* Calculate routing table */
433
  if (p->calcrt)
434
    ospf_rt_spf(p);
435
}
436

    
437

    
438
/**
439
 * ospf_import_control - accept or reject new route from nest's routing table
440
 * @P: OSPF protocol instance
441
 * @new: the new route
442
 * @attrs: list of attributes
443
 * @pool: pool for allocation of attributes
444
 *
445
 * Its quite simple. It does not accept our own routes and leaves the decision on
446
 * import to the filters.
447
 */
448
static int
449
ospf_import_control(struct proto *P, rte **new, ea_list **attrs, struct linpool *pool)
450
{
451
  struct ospf_proto *p = (struct ospf_proto *) P;
452
  struct ospf_area *oa = ospf_main_area(p);
453
  rte *e = *new;
454

    
455
  if (e->attrs->src->proto == P)
456
    return -1;                        /* Reject our own routes */
457

    
458
  if (oa_is_stub(oa))
459
    return -1;                        /* Do not export routes to stub areas */
460

    
461
  ea_list *ea = e->attrs->eattrs;
462
  u32 m0 = ea_get_int(ea, EA_GEN_IGP_METRIC, LSINFINITY);
463
  u32 m1 = MIN(m0, LSINFINITY);
464
  u32 m2 = 10000;
465
  u32 tag = 0;
466

    
467
  /* Hack for setting attributes directly in static protocol */
468
  if (e->attrs->source == RTS_STATIC)
469
  {
470
    m1 = ea_get_int(ea, EA_OSPF_METRIC1, m1);
471
    m2 = ea_get_int(ea, EA_OSPF_METRIC2, 10000);
472
    tag = ea_get_int(ea, EA_OSPF_TAG, 0);
473
  }
474

    
475
  *attrs = ospf_build_attrs(*attrs, pool, m1, m2, tag, 0);
476
  return 0;                        /* Leave decision to the filters */
477
}
478

    
479
static struct ea_list *
480
ospf_make_tmp_attrs(struct rte *rt, struct linpool *pool)
481
{
482
  return ospf_build_attrs(NULL, pool, rt->u.ospf.metric1, rt->u.ospf.metric2,
483
                          rt->u.ospf.tag, rt->u.ospf.router_id);
484
}
485

    
486
static void
487
ospf_store_tmp_attrs(struct rte *rt, struct ea_list *attrs)
488
{
489
  rt->u.ospf.metric1 = ea_get_int(attrs, EA_OSPF_METRIC1, LSINFINITY);
490
  rt->u.ospf.metric2 = ea_get_int(attrs, EA_OSPF_METRIC2, 10000);
491
  rt->u.ospf.tag = ea_get_int(attrs, EA_OSPF_TAG, 0);
492
  rt->u.ospf.router_id = ea_get_int(attrs, EA_OSPF_ROUTER_ID, 0);
493
}
494

    
495
/**
496
 * ospf_shutdown - Finish of OSPF instance
497
 * @P: OSPF protocol instance
498
 *
499
 * RFC does not define any action that should be taken before router
500
 * shutdown. To make my neighbors react as fast as possible, I send
501
 * them hello packet with empty neighbor list. They should start
502
 * their neighbor state machine with event %NEIGHBOR_1WAY.
503
 */
504
static int
505
ospf_shutdown(struct proto *P)
506
{
507
  struct ospf_proto *p = (struct ospf_proto *) P;
508
  struct ospf_iface *ifa;
509

    
510
  OSPF_TRACE(D_EVENTS, "Shutdown requested");
511

    
512
  /* And send to all my neighbors 1WAY */
513
  WALK_LIST(ifa, p->iface_list)
514
    ospf_iface_shutdown(ifa);
515

    
516
  /* Cleanup locked rta entries */
517
  FIB_WALK(&p->rtf, ort, nf)
518
  {
519
    rta_free(nf->old_rta);
520
  }
521
  FIB_WALK_END;
522

    
523
  return PS_DOWN;
524
}
525

    
526
static void
527
ospf_get_status(struct proto *P, byte * buf)
528
{
529
  struct ospf_proto *p = (struct ospf_proto *) P;
530

    
531
  if (p->p.proto_state == PS_DOWN)
532
    buf[0] = 0;
533
  else
534
  {
535
    struct ospf_iface *ifa;
536
    struct ospf_neighbor *n;
537
    int adj = 0;
538

    
539
    WALK_LIST(ifa, p->iface_list)
540
      WALK_LIST(n, ifa->neigh_list) if (n->state == NEIGHBOR_FULL)
541
      adj = 1;
542

    
543
    if (adj == 0)
544
      strcpy(buf, "Alone");
545
    else
546
      strcpy(buf, "Running");
547
  }
548
}
549

    
550
static void
551
ospf_get_route_info(rte * rte, byte * buf, ea_list * attrs UNUSED)
552
{
553
  char *type = "<bug>";
554

    
555
  switch (rte->attrs->source)
556
  {
557
  case RTS_OSPF:
558
    type = "I";
559
    break;
560
  case RTS_OSPF_IA:
561
    type = "IA";
562
    break;
563
  case RTS_OSPF_EXT1:
564
    type = "E1";
565
    break;
566
  case RTS_OSPF_EXT2:
567
    type = "E2";
568
    break;
569
  }
570

    
571
  buf += bsprintf(buf, " %s", type);
572
  buf += bsprintf(buf, " (%d/%d", rte->pref, rte->u.ospf.metric1);
573
  if (rte->attrs->source == RTS_OSPF_EXT2)
574
    buf += bsprintf(buf, "/%d", rte->u.ospf.metric2);
575
  buf += bsprintf(buf, ")");
576
  if ((rte->attrs->source == RTS_OSPF_EXT1 || rte->attrs->source == RTS_OSPF_EXT2) && rte->u.ospf.tag)
577
  {
578
    buf += bsprintf(buf, " [%x]", rte->u.ospf.tag);
579
  }
580
  if (rte->u.ospf.router_id)
581
    buf += bsprintf(buf, " [%R]", rte->u.ospf.router_id);
582
}
583

    
584
static int
585
ospf_get_attr(eattr * a, byte * buf, int buflen UNUSED)
586
{
587
  switch (a->id)
588
  {
589
  case EA_OSPF_METRIC1:
590
    bsprintf(buf, "metric1");
591
    return GA_NAME;
592
  case EA_OSPF_METRIC2:
593
    bsprintf(buf, "metric2");
594
    return GA_NAME;
595
  case EA_OSPF_TAG:
596
    bsprintf(buf, "tag: 0x%08x", a->u.data);
597
    return GA_FULL;
598
  case EA_OSPF_ROUTER_ID:
599
    bsprintf(buf, "router_id");
600
    return GA_NAME;
601
  default:
602
    return GA_UNKNOWN;
603
  }
604
}
605

    
606
static void
607
ospf_area_reconfigure(struct ospf_area *oa, struct ospf_area_config *nac)
608
{
609
  struct ospf_proto *p = oa->po;
610
  struct ospf_area_config *oac = oa->ac;
611
  struct ospf_iface *ifa;
612

    
613
  oa->ac = nac;
614
  oa->options = nac->type | ospf_opts(p);
615

    
616
  if (nac->type != oac->type)
617
  {
618
    /* Force restart of area interfaces */
619
    WALK_LIST(ifa, p->iface_list)
620
      if (ifa->oa == oa)
621
        ifa->marked = 2;
622
  }
623

    
624
  /* Handle net_list */
625
  fib_free(&oa->net_fib);
626
  fib_free(&oa->enet_fib);
627
  add_area_nets(oa, nac);
628

    
629
  /* No need to handle stubnet_list */
630

    
631
  oa->marked = 0;
632
  ospf_notify_rt_lsa(oa);
633
}
634

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

    
656
  if (proto_get_router_id(CF) != p->router_id)
657
    return 0;
658

    
659
  if (p->ospf2 != new->ospf2)
660
    return 0;
661

    
662
  if (p->rfc1583 != new->rfc1583)
663
    return 0;
664

    
665
  if (old->abr != new->abr)
666
    return 0;
667

    
668
  if ((p->af_ext != new->af_ext) || (p->af_mc != new->af_mc))
669
    return 0;
670

    
671
  if (!proto_configure_channel(P, &P->main_channel, proto_cf_main_channel(CF)))
672
    return 0;
673

    
674
  p->stub_router = new->stub_router;
675
  p->merge_external = new->merge_external;
676
  p->asbr = new->asbr;
677
  p->ecmp = new->ecmp;
678
  p->tick = new->tick;
679
  p->disp_timer->recurrent = p->tick S;
680
  tm_start(p->disp_timer, 100 MS);
681

    
682
  /* Mark all areas and ifaces */
683
  WALK_LIST(oa, p->area_list)
684
    oa->marked = 1;
685

    
686
  WALK_LIST(ifa, p->iface_list)
687
    ifa->marked = 1;
688

    
689
  /* Add and update areas */
690
  WALK_LIST(nac, new->area_list)
691
  {
692
    oa = ospf_find_area(p, nac->areaid);
693
    if (oa)
694
      ospf_area_reconfigure(oa, nac);
695
    else
696
      ospf_area_add(p, nac);
697
  }
698

    
699
  /* Add and update interfaces */
700
  ospf_reconfigure_ifaces(p);
701

    
702
  /* Add and update vlinks */
703
  WALK_LIST(ip, new->vlink_list)
704
  {
705
    ifa = ospf_find_vlink(p, ip->voa, ip->vid);
706
    if (ifa)
707
      ospf_iface_reconfigure(ifa, ip);
708
    else
709
      ospf_iface_new_vlink(p, ip);
710
  }
711

    
712
  /* Delete remaining ifaces and areas */
713
  WALK_LIST_DELSAFE(ifa, ifx, p->iface_list)
714
    if (ifa->marked)
715
    {
716
      ospf_iface_shutdown(ifa);
717
      ospf_iface_remove(ifa);
718
    }
719

    
720
  WALK_LIST_DELSAFE(oa, oax, p->area_list)
721
    if (oa->marked)
722
      ospf_area_remove(oa);
723

    
724
  ospf_schedule_rtcalc(p);
725

    
726
  return 1;
727
}
728

    
729

    
730
void
731
ospf_sh_neigh(struct proto *P, char *iff)
732
{
733
  struct ospf_proto *p = (struct ospf_proto *) P;
734
  struct ospf_iface *ifa = NULL;
735
  struct ospf_neighbor *n;
736

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

    
744
  cli_msg(-1013, "%s:", p->p.name);
745
  cli_msg(-1013, "%-12s\t%3s\t%-15s\t%-5s\t%-10s %-12s", "Router ID", "Pri",
746
          "     State", "DTime", "Interface", "Router IP");
747
  WALK_LIST(ifa, p->iface_list)
748
    if ((iff == NULL) || patmatch(iff, ifa->ifname))
749
      WALK_LIST(n, ifa->neigh_list)
750
        ospf_sh_neigh_info(n);
751
  cli_msg(0, "");
752
}
753

    
754
void
755
ospf_sh(struct proto *P)
756
{
757
  struct ospf_proto *p = (struct ospf_proto *) P;
758
  struct ospf_area *oa;
759
  struct ospf_iface *ifa;
760
  struct ospf_neighbor *n;
761
  int ifano, nno, adjno, firstfib;
762

    
763
  if (p->p.proto_state != PS_UP)
764
  {
765
    cli_msg(-1014, "%s: is not up", p->p.name);
766
    cli_msg(0, "");
767
    return;
768
  }
769

    
770
  cli_msg(-1014, "%s:", p->p.name);
771
  cli_msg(-1014, "RFC1583 compatibility: %s", (p->rfc1583 ? "enabled" : "disabled"));
772
  cli_msg(-1014, "Stub router: %s", (p->stub_router ? "Yes" : "No"));
773
  cli_msg(-1014, "RT scheduler tick: %d", p->tick);
774
  cli_msg(-1014, "Number of areas: %u", p->areano);
775
  cli_msg(-1014, "Number of LSAs in DB:\t%u", p->gr->hash_entries);
776

    
777
  WALK_LIST(oa, p->area_list)
778
  {
779
    cli_msg(-1014, "\tArea: %R (%u) %s", oa->areaid, oa->areaid,
780
            oa->areaid == 0 ? "[BACKBONE]" : "");
781
    ifano = 0;
782
    nno = 0;
783
    adjno = 0;
784
    WALK_LIST(ifa, p->iface_list)
785
    {
786
      if (oa == ifa->oa)
787
      {
788
        ifano++;
789
        WALK_LIST(n, ifa->neigh_list)
790
        {
791
          nno++;
792
          if (n->state == NEIGHBOR_FULL)
793
            adjno++;
794
        }
795
      }
796
    }
797

    
798
    cli_msg(-1014, "\t\tStub:\t%s", oa_is_stub(oa) ? "Yes" : "No");
799
    cli_msg(-1014, "\t\tNSSA:\t%s", oa_is_nssa(oa) ? "Yes" : "No");
800
    cli_msg(-1014, "\t\tTransit:\t%s", oa->trcap ? "Yes" : "No");
801

    
802
    if (oa_is_nssa(oa))
803
      cli_msg(-1014, "\t\tNSSA translation:\t%s%s", oa->translate ? "Yes" : "No",
804
              oa->translate == TRANS_WAIT ? " (run down)" : "");
805
    cli_msg(-1014, "\t\tNumber of interfaces:\t%u", ifano);
806
    cli_msg(-1014, "\t\tNumber of neighbors:\t%u", nno);
807
    cli_msg(-1014, "\t\tNumber of adjacent neighbors:\t%u", adjno);
808

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

    
822
    firstfib = 1;
823
    FIB_WALK(&oa->enet_fib, struct area_net, anet)
824
    {
825
      if(firstfib)
826
      {
827
        cli_msg(-1014, "\t\tArea external networks:");
828
        firstfib = 0;
829
      }
830
      cli_msg(-1014, "\t\t\t%1N\t%s\t%s", anet->fn.addr,
831
                anet->hidden ? "Hidden" : "Advertise", anet->active ? "Active" : "");
832
    }
833
    FIB_WALK_END;
834

    
835
  }
836
  cli_msg(0, "");
837
}
838

    
839
void
840
ospf_sh_iface(struct proto *P, char *iff)
841
{
842
  struct ospf_proto *p = (struct ospf_proto *) P;
843
  struct ospf_iface *ifa = NULL;
844

    
845
  if (p->p.proto_state != PS_UP)
846
  {
847
    cli_msg(-1015, "%s: is not up", p->p.name);
848
    cli_msg(0, "");
849
    return;
850
  }
851

    
852
  cli_msg(-1015, "%s:", p->p.name);
853
  WALK_LIST(ifa, p->iface_list)
854
    if ((iff == NULL) || patmatch(iff, ifa->ifname))
855
      ospf_iface_info(ifa);
856
  cli_msg(0, "");
857
}
858

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

    
872
static struct ospf_lsa_header *
873
fake_lsa_from_prefix_lsa(struct ospf_lsa_header *dst, struct ospf_lsa_header *src,
874
                         struct ospf_lsa_prefix *px)
875
{
876
  dst->age = src->age;
877
  dst->type_raw = px->ref_type;
878
  dst->id = px->ref_id;
879
  dst->rt = px->ref_rt;
880
  dst->sn = src->sn;
881

    
882
  return dst;
883
}
884

    
885

    
886
static int lsa_compare_ospf3;
887

    
888
static int
889
lsa_compare_for_state(const void *p1, const void *p2)
890
{
891
  struct top_hash_entry *he1 = * (struct top_hash_entry **) p1;
892
  struct top_hash_entry *he2 = * (struct top_hash_entry **) p2;
893
  struct ospf_lsa_header *lsa1 = &(he1->lsa);
894
  struct ospf_lsa_header *lsa2 = &(he2->lsa);
895
  struct ospf_lsa_header lsatmp1, lsatmp2;
896
  u16 lsa1_type = he1->lsa_type;
897
  u16 lsa2_type = he2->lsa_type;
898

    
899
  if (he1->domain < he2->domain)
900
    return -1;
901
  if (he1->domain > he2->domain)
902
    return 1;
903

    
904

    
905
  /* px1 or px2 assumes OSPFv3 */
906
  int px1 = (lsa1_type == LSA_T_PREFIX);
907
  int px2 = (lsa2_type == LSA_T_PREFIX);
908

    
909
  if (px1)
910
  {
911
    lsa1 = fake_lsa_from_prefix_lsa(&lsatmp1, lsa1, he1->lsa_body);
912
    lsa1_type = lsa1->type_raw;        /* FIXME: handle unknown ref_type */
913
  }
914

    
915
  if (px2)
916
  {
917
    lsa2 = fake_lsa_from_prefix_lsa(&lsatmp2, lsa2, he2->lsa_body);
918
    lsa2_type = lsa2->type_raw;
919
  }
920

    
921

    
922
  int nt1 = (lsa1_type == LSA_T_NET);
923
  int nt2 = (lsa2_type == LSA_T_NET);
924

    
925
  if (nt1 != nt2)
926
    return nt1 - nt2;
927

    
928
  if (nt1)
929
  {
930
    /* In OSPFv3, networks are named based on ID of DR */
931
    if (lsa_compare_ospf3)
932
    {
933
      if (lsa1->rt < lsa2->rt)
934
        return -1;
935
      if (lsa1->rt > lsa2->rt)
936
        return 1;
937
    }
938

    
939
    /* For OSPFv2, this is IP of the network,
940
       for OSPFv3, this is interface ID */
941
    if (lsa1->id < lsa2->id)
942
      return -1;
943
    if (lsa1->id > lsa2->id)
944
      return 1;
945

    
946
    if (px1 != px2)
947
      return px1 - px2;
948

    
949
    return lsa1->sn - lsa2->sn;
950
  }
951
  else
952
  {
953
    if (lsa1->rt < lsa2->rt)
954
      return -1;
955
    if (lsa1->rt > lsa2->rt)
956
      return 1;
957

    
958
    if (lsa1_type < lsa2_type)
959
      return -1;
960
    if (lsa1_type > lsa2_type)
961
      return 1;
962

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

    
968
    if (px1 != px2)
969
      return px1 - px2;
970

    
971
    return lsa1->sn - lsa2->sn;
972
  }
973
}
974

    
975
static int
976
ext_compare_for_state(const void *p1, const void *p2)
977
{
978
  struct top_hash_entry * he1 = * (struct top_hash_entry **) p1;
979
  struct top_hash_entry * he2 = * (struct top_hash_entry **) p2;
980
  struct ospf_lsa_header *lsa1 = &(he1->lsa);
981
  struct ospf_lsa_header *lsa2 = &(he2->lsa);
982

    
983
  if (lsa1->rt < lsa2->rt)
984
    return -1;
985
  if (lsa1->rt > lsa2->rt)
986
    return 1;
987

    
988
  if (lsa1->id < lsa2->id)
989
    return -1;
990
  if (lsa1->id > lsa2->id)
991
    return 1;
992

    
993
  return lsa1->sn - lsa2->sn;
994
}
995

    
996
static inline void
997
show_lsa_distance(struct top_hash_entry *he)
998
{
999
  if (he->color == INSPF)
1000
    cli_msg(-1016, "\t\tdistance %u", he->dist);
1001
  else
1002
    cli_msg(-1016, "\t\tunreachable");
1003
}
1004

    
1005
static inline void
1006
show_lsa_router(struct ospf_proto *p, struct top_hash_entry *he, int verbose)
1007
{
1008
  struct ospf_lsa_rt_walk rtl;
1009

    
1010
  cli_msg(-1016, "");
1011
  cli_msg(-1016, "\trouter %R", he->lsa.rt);
1012
  show_lsa_distance(he);
1013

    
1014
  lsa_walk_rt_init(p, he, &rtl);
1015
  while (lsa_walk_rt(&rtl))
1016
    if (rtl.type == LSART_VLNK)
1017
      cli_msg(-1016, "\t\tvlink %R metric %u", rtl.id, rtl.metric);
1018

    
1019
  lsa_walk_rt_init(p, he, &rtl);
1020
  while (lsa_walk_rt(&rtl))
1021
    if (rtl.type == LSART_PTP)
1022
      cli_msg(-1016, "\t\trouter %R metric %u", rtl.id, rtl.metric);
1023

    
1024
  lsa_walk_rt_init(p, he, &rtl);
1025
  while (lsa_walk_rt(&rtl))
1026
    if (rtl.type == LSART_NET)
1027
    {
1028
      if (ospf_is_v2(p))
1029
      {
1030
        /* In OSPFv2, we try to find network-LSA to get prefix/pxlen */
1031
        struct top_hash_entry *net_he = ospf_hash_find_net2(p->gr, he->domain, rtl.id);
1032

    
1033
        if (net_he && (net_he->lsa.age < LSA_MAXAGE))
1034
        {
1035
          struct ospf_lsa_header *net_lsa = &(net_he->lsa);
1036
          struct ospf_lsa_net *net_ln = net_he->lsa_body;
1037

    
1038
          cli_msg(-1016, "\t\tnetwork %I/%d metric %u",
1039
                  ipa_from_u32(net_lsa->id & net_ln->optx),
1040
                  u32_masklen(net_ln->optx), rtl.metric);
1041
        }
1042
        else
1043
          cli_msg(-1016, "\t\tnetwork [%R] metric %u", rtl.id, rtl.metric);
1044
      }
1045
      else
1046
        cli_msg(-1016, "\t\tnetwork [%R-%u] metric %u", rtl.id, rtl.nif, rtl.metric);
1047
    }
1048

    
1049
  if (ospf_is_v2(p) && verbose)
1050
  {
1051
    lsa_walk_rt_init(p, he, &rtl);
1052
    while (lsa_walk_rt(&rtl))
1053
      if (rtl.type == LSART_STUB)
1054
        cli_msg(-1016, "\t\tstubnet %I/%d metric %u",
1055
                ipa_from_u32(rtl.id), u32_masklen(rtl.data), rtl.metric);
1056
  }
1057
}
1058

    
1059
static inline void
1060
show_lsa_network(struct top_hash_entry *he, int ospf2)
1061
{
1062
  struct ospf_lsa_header *lsa = &(he->lsa);
1063
  struct ospf_lsa_net *ln = he->lsa_body;
1064
  u32 i;
1065

    
1066
  if (ospf2)
1067
  {
1068
    cli_msg(-1016, "");
1069
    cli_msg(-1016, "\tnetwork %I/%d", ipa_from_u32(lsa->id & ln->optx), u32_masklen(ln->optx));
1070
    cli_msg(-1016, "\t\tdr %R", lsa->rt);
1071
  }
1072
  else
1073
  {
1074
    cli_msg(-1016, "");
1075
    cli_msg(-1016, "\tnetwork [%R-%u]", lsa->rt, lsa->id);
1076
  }
1077

    
1078
  show_lsa_distance(he);
1079

    
1080
  for (i = 0; i < lsa_net_count(lsa); i++)
1081
    cli_msg(-1016, "\t\trouter %R", ln->routers[i]);
1082
}
1083

    
1084
static inline void
1085
show_lsa_sum_net(struct top_hash_entry *he, int ospf2, int af)
1086
{
1087
  net_addr net;
1088
  u8 pxopts;
1089
  u32 metric;
1090

    
1091
  lsa_parse_sum_net(he, ospf2, af, &net, &pxopts, &metric);
1092
  cli_msg(-1016, "\t\txnetwork %N metric %u", &net, metric);
1093
}
1094

    
1095
static inline void
1096
show_lsa_sum_rt(struct top_hash_entry *he, int ospf2)
1097
{
1098
  u32 metric;
1099
  u32 dst_rid;
1100
  u32 options;
1101

    
1102
  lsa_parse_sum_rt(he, ospf2, &dst_rid, &metric, &options);
1103
  cli_msg(-1016, "\t\txrouter %R metric %u", dst_rid, metric);
1104
}
1105

    
1106

    
1107
static inline void
1108
show_lsa_external(struct top_hash_entry *he, int ospf2, int af)
1109
{
1110
  struct ospf_lsa_ext_local rt;
1111
  char str_via[IPA_MAX_TEXT_LENGTH + 8] = "";
1112
  char str_tag[16] = "";
1113

    
1114
  if (he->lsa_type == LSA_T_EXT)
1115
    he->domain = 0; /* Unmark the LSA */
1116

    
1117
  lsa_parse_ext(he, ospf2, af, &rt);
1118

    
1119
  if (rt.fbit)
1120
    bsprintf(str_via, " via %I", rt.fwaddr);
1121

    
1122
  if (rt.tag)
1123
    bsprintf(str_tag, " tag %08x", rt.tag);
1124

    
1125
  cli_msg(-1016, "\t\t%s %N metric%s %u%s%s",
1126
          (he->lsa_type == LSA_T_NSSA) ? "nssa-ext" : "external",
1127
          &rt.net, rt.ebit ? "2" : "", rt.metric, str_via, str_tag);
1128
}
1129

    
1130
static inline void
1131
show_lsa_prefix(struct top_hash_entry *he, struct top_hash_entry *cnode, int af)
1132
{
1133
  struct ospf_lsa_prefix *px = he->lsa_body;
1134
  u32 *buf;
1135
  int i;
1136

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

    
1141
  if ((px->ref_type == LSA_T_RT) && (px->ref_id != 0))
1142
    return;
1143

    
1144
  if ((px->ref_type == LSA_T_NET) && (px->ref_id != cnode->lsa.id))
1145
    return;
1146

    
1147
  buf = px->rest;
1148
  for (i = 0; i < px->pxcount; i++)
1149
  {
1150
    net_addr net;
1151
    u8 pxopts;
1152
    u16 metric;
1153

    
1154
    buf = ospf3_get_prefix(buf, af, &net, &pxopts, &metric);
1155

    
1156
    if (px->ref_type == LSA_T_RT)
1157
      cli_msg(-1016, "\t\tstubnet %N metric %u", &net, metric);
1158
    else
1159
      cli_msg(-1016, "\t\taddress %N", &net);
1160
  }
1161
}
1162

    
1163
void
1164
ospf_sh_state(struct proto *P, int verbose, int reachable)
1165
{
1166
  struct ospf_proto *p = (struct ospf_proto *) P;
1167
  int ospf2 = ospf_is_v2(p);
1168
  int af = ospf_get_af(p);
1169
  uint i, ix, j1, jx;
1170
  u32 last_area = 0xFFFFFFFF;
1171

    
1172
  if (p->p.proto_state != PS_UP)
1173
  {
1174
    cli_msg(-1016, "%s: is not up", p->p.name);
1175
    cli_msg(0, "");
1176
    return;
1177
  }
1178

    
1179
  /* We store interesting area-scoped LSAs in array hea and
1180
     global-scoped (LSA_T_EXT) LSAs in array hex */
1181

    
1182
  uint num = p->gr->hash_entries;
1183
  struct top_hash_entry *hea[num];
1184
  struct top_hash_entry *hex[verbose ? num : 0];
1185
  struct top_hash_entry *he;
1186
  struct top_hash_entry *cnode = NULL;
1187

    
1188
  j1 = jx = 0;
1189
  WALK_SLIST(he, p->lsal)
1190
  {
1191
    int accept;
1192

    
1193
    if (he->lsa.age == LSA_MAXAGE)
1194
      continue;
1195

    
1196
    switch (he->lsa_type)
1197
    {
1198
    case LSA_T_RT:
1199
    case LSA_T_NET:
1200
      accept = 1;
1201
      break;
1202

    
1203
    case LSA_T_SUM_NET:
1204
    case LSA_T_SUM_RT:
1205
    case LSA_T_NSSA:
1206
    case LSA_T_PREFIX:
1207
      accept = verbose;
1208
      break;
1209

    
1210
    case LSA_T_EXT:
1211
      if (verbose)
1212
      {
1213
        he->domain = 1; /* Abuse domain field to mark the LSA */
1214
        hex[jx++] = he;
1215
      }
1216
    default:
1217
      accept = 0;
1218
    }
1219

    
1220
    if (accept)
1221
      hea[j1++] = he;
1222
  }
1223

    
1224
  ASSERT(j1 <= num && jx <= num);
1225

    
1226
  lsa_compare_ospf3 = !ospf2;
1227
  qsort(hea, j1, sizeof(struct top_hash_entry *), lsa_compare_for_state);
1228
  qsort(hex, jx, sizeof(struct top_hash_entry *), ext_compare_for_state);
1229

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

    
1249
  ix = 0;
1250
  for (i = 0; i < j1; i++)
1251
  {
1252
    he = hea[i];
1253

    
1254
    /* If there is no opened node, we open the LSA (if appropriate) or skip to the next one */
1255
    if (!cnode)
1256
    {
1257
      if (((he->lsa_type == LSA_T_RT) || (he->lsa_type == LSA_T_NET))
1258
          && ((he->color == INSPF) || !reachable))
1259
      {
1260
        cnode = he;
1261

    
1262
        if (he->domain != last_area)
1263
        {
1264
          cli_msg(-1016, "");
1265
          cli_msg(-1016, "area %R", he->domain);
1266
          last_area = he->domain;
1267
          ix = 0;
1268
        }
1269
      }
1270
      else
1271
        continue;
1272
    }
1273

    
1274
    ASSERT(cnode && (he->domain == last_area) && (he->lsa.rt == cnode->lsa.rt));
1275

    
1276
    switch (he->lsa_type)
1277
    {
1278
    case LSA_T_RT:
1279
      if (he->lsa.id == cnode->lsa.id)
1280
        show_lsa_router(p, he, verbose);
1281
      break;
1282

    
1283
    case LSA_T_NET:
1284
      show_lsa_network(he, ospf2);
1285
      break;
1286

    
1287
    case LSA_T_SUM_NET:
1288
      if (cnode->lsa_type == LSA_T_RT)
1289
        show_lsa_sum_net(he, ospf2, af);
1290
      break;
1291

    
1292
    case LSA_T_SUM_RT:
1293
      if (cnode->lsa_type == LSA_T_RT)
1294
        show_lsa_sum_rt(he, ospf2);
1295
      break;
1296

    
1297
    case LSA_T_EXT:
1298
    case LSA_T_NSSA:
1299
      show_lsa_external(he, ospf2, af);
1300
      break;
1301

    
1302
    case LSA_T_PREFIX:
1303
      show_lsa_prefix(he, cnode, af);
1304
      break;
1305
    }
1306

    
1307
    /* In these cases, we close the current node */
1308
    if ((i+1 == j1)
1309
        || (hea[i+1]->domain != last_area)
1310
        || (hea[i+1]->lsa.rt != cnode->lsa.rt)
1311
        || (hea[i+1]->lsa_type == LSA_T_NET))
1312
    {
1313
      while ((ix < jx) && (hex[ix]->lsa.rt < cnode->lsa.rt))
1314
        ix++;
1315

    
1316
      while ((ix < jx) && (hex[ix]->lsa.rt == cnode->lsa.rt))
1317
        show_lsa_external(hex[ix++], ospf2, af);
1318

    
1319
      cnode = NULL;
1320
    }
1321
  }
1322

    
1323
  int hdr = 0;
1324
  u32 last_rt = 0xFFFFFFFF;
1325
  for (ix = 0; ix < jx; ix++)
1326
  {
1327
    he = hex[ix];
1328

    
1329
    /* If it is still marked, we show it now. */
1330
    if (he->domain)
1331
    {
1332
      he->domain = 0;
1333

    
1334
      if ((he->color != INSPF) && reachable)
1335
        continue;
1336

    
1337
      if (!hdr)
1338
      {
1339
        cli_msg(-1016, "");
1340
        cli_msg(-1016, "other ASBRs");
1341
        hdr = 1;
1342
      }
1343

    
1344
      if (he->lsa.rt != last_rt)
1345
      {
1346
        cli_msg(-1016, "");
1347
        cli_msg(-1016, "\trouter %R", he->lsa.rt);
1348
        last_rt = he->lsa.rt;
1349
      }
1350

    
1351
      show_lsa_external(he, ospf2, af);
1352
    }
1353
  }
1354

    
1355
  cli_msg(0, "");
1356
}
1357

    
1358

    
1359
static int
1360
lsa_compare_for_lsadb(const void *p1, const void *p2)
1361
{
1362
  struct top_hash_entry * he1 = * (struct top_hash_entry **) p1;
1363
  struct top_hash_entry * he2 = * (struct top_hash_entry **) p2;
1364
  struct ospf_lsa_header *lsa1 = &(he1->lsa);
1365
  struct ospf_lsa_header *lsa2 = &(he2->lsa);
1366
  int sc1 = LSA_SCOPE(he1->lsa_type);
1367
  int sc2 = LSA_SCOPE(he2->lsa_type);
1368

    
1369
  if (sc1 != sc2)
1370
    return sc2 - sc1;
1371

    
1372
  if (he1->domain != he2->domain)
1373
    return he1->domain - he2->domain;
1374

    
1375
  if (lsa1->rt != lsa2->rt)
1376
    return lsa1->rt - lsa2->rt;
1377

    
1378
  if (lsa1->id != lsa2->id)
1379
    return lsa1->id - lsa2->id;
1380

    
1381
  if (he1->lsa_type != he2->lsa_type)
1382
    return he1->lsa_type - he2->lsa_type;
1383

    
1384
  return lsa1->sn - lsa2->sn;
1385
}
1386

    
1387
void
1388
ospf_sh_lsadb(struct lsadb_show_data *ld)
1389
{
1390
  struct ospf_proto *p = (struct ospf_proto *) proto_get_named(ld->name, &proto_ospf);
1391
  uint num = p->gr->hash_entries;
1392
  uint i, j;
1393
  int last_dscope = -1;
1394
  u32 last_domain = 0;
1395
  u16 type_mask = ospf_is_v2(p) ?  0x00ff : 0xffff;        /* see lsa_etype() */
1396

    
1397
  if (p->p.proto_state != PS_UP)
1398
  {
1399
    cli_msg(-1017, "%s: is not up", p->p.name);
1400
    cli_msg(0, "");
1401
    return;
1402
  }
1403

    
1404
  if (ld->router == SH_ROUTER_SELF)
1405
    ld->router = p->router_id;
1406

    
1407
  struct top_hash_entry *hea[num];
1408
  struct top_hash_entry *he;
1409

    
1410
  j = 0;
1411
  WALK_SLIST(he, p->lsal)
1412
    if (he->lsa_body)
1413
      hea[j++] = he;
1414

    
1415
  ASSERT(j <= num);
1416

    
1417
  qsort(hea, j, sizeof(struct top_hash_entry *), lsa_compare_for_lsadb);
1418

    
1419
  for (i = 0; i < j; i++)
1420
  {
1421
    struct ospf_lsa_header *lsa = &(hea[i]->lsa);
1422
    u16 lsa_type = lsa->type_raw & type_mask;
1423
    u16 dscope = LSA_SCOPE(hea[i]->lsa_type);
1424

    
1425
    /* Hack: 1 is used for LSA_SCOPE_LINK, fixed by & 0xf000 */
1426
    if (ld->scope && (dscope != (ld->scope & 0xf000)))
1427
      continue;
1428

    
1429
    if ((ld->scope == LSA_SCOPE_AREA) && (hea[i]->domain != ld->area))
1430
      continue;
1431

    
1432
    /* For user convenience ignore high nibble */
1433
    if (ld->type && ((lsa_type & 0x0fff) != (ld->type & 0x0fff)))
1434
      continue;
1435

    
1436
    if (ld->lsid && (lsa->id != ld->lsid))
1437
      continue;
1438

    
1439
    if (ld->router && (lsa->rt != ld->router))
1440
      continue;
1441

    
1442
    if ((dscope != last_dscope) || (hea[i]->domain != last_domain))
1443
    {
1444
      cli_msg(-1017, "");
1445
      switch (dscope)
1446
      {
1447
      case LSA_SCOPE_AS:
1448
        cli_msg(-1017, "Global");
1449
        break;
1450

    
1451
      case LSA_SCOPE_AREA:
1452
        cli_msg(-1017, "Area %R", hea[i]->domain);
1453
        break;
1454

    
1455
      case LSA_SCOPE_LINK:
1456
        {
1457
          struct iface *ifa = if_find_by_index(hea[i]->domain);
1458
          cli_msg(-1017, "Link %s", (ifa != NULL) ? ifa->name : "?");
1459
        }
1460
        break;
1461
      }
1462
      cli_msg(-1017, "");
1463
      cli_msg(-1017," Type   LS ID           Router          Sequence   Age  Checksum");
1464

    
1465
      last_dscope = dscope;
1466
      last_domain = hea[i]->domain;
1467
    }
1468

    
1469
    cli_msg(-1017," %04x  %-15R %-15R  %08x %5u    %04x",
1470
            lsa_type, lsa->id, lsa->rt, lsa->sn, lsa->age, lsa->checksum);
1471
  }
1472
  cli_msg(0, "");
1473
}
1474

    
1475

    
1476
struct protocol proto_ospf = {
1477
  .name =                "OSPF",
1478
  .template =                "ospf%d",
1479
  .attr_class =                EAP_OSPF,
1480
  .preference =                DEF_PREF_OSPF,
1481
  .channel_mask =        NB_IP,
1482
  .proto_size =                sizeof(struct ospf_proto),
1483
  .config_size =        sizeof(struct ospf_config),
1484
  .init =                ospf_init,
1485
  .dump =                ospf_dump,
1486
  .start =                ospf_start,
1487
  .shutdown =                ospf_shutdown,
1488
  .reconfigure =        ospf_reconfigure,
1489
  .get_status =                ospf_get_status,
1490
  .get_attr =                ospf_get_attr,
1491
  .get_route_info =        ospf_get_route_info
1492
};