Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (38.6 KB)

1
/*
2
 *        BIRD -- OSPF
3
 *
4
 *        (c) 1999--2004 Ondrej Filip <feela@network.cz>
5
 *        (c) 2009--2014 Ondrej Zajicek <santiago@crfreenet.org>
6
 *        (c) 2009--2014 CZ.NIC z.s.p.o.
7
 *
8
 *        Can be freely distributed and used under the terms of the GNU GPL.
9
 */
10

    
11
/**
12
 * DOC: Open Shortest Path First (OSPF)
13
 *
14
 * The OSPF protocol is quite complicated and its complex implemenation is split
15
 * to many files. In |ospf.c|, you will find mainly the interface for
16
 * communication with the core (e.g., reconfiguration hooks, shutdown and
17
 * initialisation and so on). File |iface.c| contains the interface state
18
 * machine and functions for allocation and deallocation of OSPF's interface
19
 * data structures. Source |neighbor.c| includes the neighbor state machine and
20
 * functions for election of Designated Router and Backup Designated router. In
21
 * |packet.c|, you will find various functions for sending and receiving generic
22
 * OSPF packets. There are also routines for authentication and checksumming.
23
 * In |hello.c|, there are routines for sending and receiving of hello packets
24
 * as well as functions for maintaining wait times and the inactivity timer.
25
 * Files |lsreq.c|, |lsack.c|, |dbdes.c| contain functions for sending and
26
 * receiving of link-state requests, link-state acknowledgements and database
27
 * descriptions respectively.  In |lsupd.c|, there are functions for sending and
28
 * receiving of link-state updates and also the flooding algorithm. Source
29
 * |topology.c| is a place where routines for searching LSAs in the link-state
30
 * database, adding and deleting them reside, there also are functions for
31
 * originating of various types of LSAs (router LSA, net LSA, external LSA).
32
 * File |rt.c| contains routines for calculating the routing table. |lsalib.c|
33
 * is a set of various functions for working with the LSAs (endianity
34
 * conversions, calculation of checksum etc.).
35
 *
36
 * One instance of the protocol is able to hold LSA databases for multiple OSPF
37
 * areas, to exchange routing information between multiple neighbors and to
38
 * calculate the routing tables. The core structure is &ospf_proto to which
39
 * multiple &ospf_area and &ospf_iface structures are connected. &ospf_proto is
40
 * also connected to &top_hash_graph which is a dynamic hashing structure that
41
 * describes the link-state database. It allows fast search, addition and
42
 * deletion. Each LSA is kept in two pieces: header and body. Both of them are
43
 * kept in the endianity of the CPU.
44
 *
45
 * In OSPFv2 specification, it is implied that there is one IP prefix for each
46
 * physical network/interface (unless it is an ptp link). But in modern systems,
47
 * there might be more independent IP prefixes associated with an interface.  To
48
 * handle this situation, we have one &ospf_iface for each active IP prefix
49
 * (instead for each active iface); This behaves like virtual interface for the
50
 * purpose of OSPF.  If we receive packet, we associate it with a proper virtual
51
 * interface mainly according to its source address.
52
 *
53
 * OSPF keeps one socket per &ospf_iface. This allows us (compared to one socket
54
 * approach) to evade problems with a limit of multicast groups per socket and
55
 * with sending multicast packets to appropriate interface in a portable way.
56
 * The socket is associated with underlying physical iface and should not
57
 * receive packets received on other ifaces (unfortunately, this is not true on
58
 * BSD). Generally, one packet can be received by more sockets (for example, if
59
 * there are more &ospf_iface on one physical iface), therefore we explicitly
60
 * filter received packets according to src/dst IP address and received iface.
61
 *
62
 * Vlinks are implemented using particularly degenerate form of &ospf_iface,
63
 * which has several exceptions: it does not have its iface or socket (it copies
64
 * these from 'parent' &ospf_iface) and it is present in iface list even when
65
 * down (it is not freed in ospf_iface_down()).
66
 *
67
 * The heart beat of ospf is ospf_disp(). It is called at regular intervals
68
 * (&ospf_proto->tick). It is responsible for aging and flushing of LSAs in the
69
 * database, updating topology information in LSAs and for routing table
70
 * calculation.
71
 *
72
 * To every &ospf_iface, we connect one or more &ospf_neighbor's -- a structure
73
 * containing many timers and queues for building adjacency and for exchange of
74
 * routing messages.
75
 *
76
 * BIRD's OSPF implementation respects RFC2328 in every detail, but some of
77
 * internal algorithms do differ. The RFC recommends making a snapshot of the
78
 * link-state database when a new adjacency is forming and sending the database
79
 * description packets based on the information in this snapshot. The database
80
 * can be quite large in some networks, so rather we walk through a &slist
81
 * structure which allows us to continue even if the actual LSA we were working
82
 * with is deleted. New LSAs are added at the tail of this &slist.
83
 *
84
 * We also do not keep a separate OSPF routing table, because the core helps us
85
 * by being able to recognize when a route is updated to an identical one and it
86
 * suppresses the update automatically. Due to this, we can flush all the routes
87
 * we have recalculated and also those we have deleted to the core's routing
88
 * table and the core will take care of the rest. This simplifies the process
89
 * and conserves memory.
90
 *
91
 * Supported standards:
92
 * - RFC 2328 - main OSPFv2 standard
93
 * - RFC 5340 - main OSPFv3 standard
94
 * - RFC 3101 - OSPFv2 NSSA areas
95
 * - RFC 6549 - OSPFv2 multi-instance extensions
96
 * - RFC 6987 - OSPF stub router advertisement
97
 */
98

    
99
#include <stdlib.h>
100
#include "ospf.h"
101

    
102
static int ospf_import_control(struct proto *P, rte **new, ea_list **attrs, struct linpool *pool);
103
static struct ea_list *ospf_make_tmp_attrs(struct rte *rt, struct linpool *pool);
104
static void ospf_store_tmp_attrs(struct rte *rt, struct ea_list *attrs);
105
static 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
  net_addr net;
118

    
119
  fib_init(&oa->net_fib,  p->p.pool, ospf_is_v2(p) ? NET_IP4 : NET_IP6,
120
           sizeof(struct area_net), OFFSETOF(struct area_net, fn), 0, NULL);
121
  fib_init(&oa->enet_fib, p->p.pool, ospf_is_v2(p) ? NET_IP4 : NET_IP6,
122
           sizeof(struct area_net), OFFSETOF(struct area_net, fn), 0, NULL);
123

    
124
  WALK_LIST(anc, ac->net_list)
125
  {
126
    /* XXXX we should dispatch by ospf version, not by px.addr */
127
    net_fill_ipa(&net, anc->px.addr, anc->px.len);
128
    an = fib_get(&oa->net_fib, &net);
129
    an->hidden = anc->hidden;
130
  }
131

    
132
  WALK_LIST(anc, ac->enet_list)
133
  {
134
    /* XXXX ditto */
135
    net_fill_ipa(&net, anc->px.addr, anc->px.len);
136
    an = fib_get(&oa->enet_fib, &net);
137
    an->hidden = anc->hidden;
138
    an->tag = anc->tag;
139
  }
140
}
141

    
142
static void
143
ospf_area_add(struct ospf_proto *p, struct ospf_area_config *ac)
144
{
145
  struct ospf_area *oa;
146

    
147
  OSPF_TRACE(D_EVENTS, "Adding area %R", ac->areaid);
148

    
149
  oa = mb_allocz(p->p.pool, sizeof(struct ospf_area));
150
  add_tail(&p->area_list, NODE oa);
151
  p->areano++;
152

    
153
  oa->ac = ac;
154
  oa->areaid = ac->areaid;
155
  oa->rt = NULL;
156
  oa->po = p;
157
  fib_init(&oa->rtr, p->p.pool, NET_IP4, sizeof(ort), OFFSETOF(ort, fn), 0, NULL);
158
  add_area_nets(oa, ac);
159

    
160
  if (oa->areaid == 0)
161
    p->backbone = oa;
162

    
163
  if (ospf_is_v2(p))
164
    oa->options = ac->type;
165
  else
166
    oa->options = ac->type | OPT_V6 | (p->stub_router ? 0 : OPT_R);
167

    
168
  ospf_notify_rt_lsa(oa);
169
}
170

    
171
static void
172
ospf_flush_area(struct ospf_proto *p, u32 areaid)
173
{
174
  struct top_hash_entry *en;
175

    
176
  WALK_SLIST(en, p->lsal)
177
    if ((LSA_SCOPE(en->lsa_type) == LSA_SCOPE_AREA) && (en->domain == areaid))
178
      ospf_flush_lsa(p, en);
179
}
180

    
181
static void
182
ospf_area_remove(struct ospf_area *oa)
183
{
184
  struct ospf_proto *p = oa->po;
185
  OSPF_TRACE(D_EVENTS, "Removing area %R", oa->areaid);
186

    
187
  /* We suppose that interfaces are already removed */
188
  ospf_flush_area(p, oa->areaid);
189

    
190
  fib_free(&oa->rtr);
191
  fib_free(&oa->net_fib);
192
  fib_free(&oa->enet_fib);
193

    
194
  if (oa->translator_timer)
195
    rfree(oa->translator_timer);
196

    
197
  p->areano--;
198
  rem_node(NODE oa);
199
  mb_free(oa);
200
}
201

    
202

    
203
struct ospf_area *
204
ospf_find_area(struct ospf_proto *p, u32 aid)
205
{
206
  struct ospf_area *oa;
207
  WALK_LIST(oa, p->area_list)
208
    if (((struct ospf_area *) oa)->areaid == aid)
209
      return oa;
210
  return NULL;
211
}
212

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

    
223
static int
224
ospf_start(struct proto *P)
225
{
226
  struct ospf_proto *p = (struct ospf_proto *) P;
227
  struct ospf_config *c = (struct ospf_config *) (P->cf);
228
  struct ospf_area_config *ac;
229

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

    
252
  p->flood_event = ev_new(P->pool);
253
  p->flood_event->hook = ospf_flood_event;
254
  p->flood_event->data = p;
255

    
256
  p->log_pkt_tbf = (struct tbf){ .rate = 1, .burst = 5 };
257
  p->log_lsa_tbf = (struct tbf){ .rate = 4, .burst = 20 };
258

    
259
  WALK_LIST(ac, c->area_list)
260
    ospf_area_add(p, ac);
261

    
262
  if (c->abr)
263
    ospf_open_vlink_sk(p);
264

    
265
  /* Add all virtual links */
266
  struct ospf_iface_patt *ic;
267
  WALK_LIST(ic, c->vlink_list)
268
    ospf_iface_new_vlink(p, ic);
269

    
270
  return PS_UP;
271
}
272

    
273
static void
274
ospf_dump(struct proto *P)
275
{
276
  struct ospf_proto *p = (struct ospf_proto *) P;
277
  struct ospf_iface *ifa;
278
  struct ospf_neighbor *n;
279

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

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

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

    
302
static struct proto *
303
ospf_init(struct proto_config *c)
304
{
305
  struct ospf_config *oc = (struct ospf_config *) c;
306
  struct proto *P = proto_new(c, sizeof(struct ospf_proto));
307

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

    
319
  return P;
320
}
321

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

    
329
  if(new->attrs->source < old->attrs->source) return 1;
330
  if(new->attrs->source > old->attrs->source) return 0;
331

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

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

    
341
  return 0;                        /* Old is shorter or same */
342
}
343

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

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

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

    
384

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

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

    
395
static int
396
ospf_reload_routes(struct proto *P)
397
{
398
  struct ospf_proto *p = (struct ospf_proto *) P;
399

    
400
  if (p->calcrt != 2)
401
    OSPF_TRACE(D_EVENTS, "Scheduling routing table calculation with route reload");
402

    
403
  p->calcrt = 2;
404

    
405
  return 1;
406
}
407

    
408

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

    
419
  /* Originate or flush local topology LSAs */
420
  ospf_update_topology(p);
421

    
422
  /* Process LSA DB */
423
  ospf_update_lsadb(p);
424

    
425
  /* Calculate routing table */
426
  if (p->calcrt)
427
    ospf_rt_spf(p);
428
}
429

    
430

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

    
448
  if (e->attrs->src->proto == P)
449
    return -1;                        /* Reject our own routes */
450

    
451
  if (oa_is_stub(oa))
452
    return -1;                        /* Do not export routes to stub areas */
453

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

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

    
468
  *attrs = ospf_build_attrs(*attrs, pool, m1, m2, tag, 0);
469
  return 0;                        /* Leave decision to the filters */
470
}
471

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

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

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

    
503
  OSPF_TRACE(D_EVENTS, "Shutdown requested");
504

    
505
  /* And send to all my neighbors 1WAY */
506
  WALK_LIST(ifa, p->iface_list)
507
    ospf_iface_shutdown(ifa);
508

    
509
  /* Cleanup locked rta entries */
510
  FIB_WALK(&p->rtf, nftmp)
511
  {
512
    rta_free(((ort *) nftmp)->old_rta);
513
  }
514
  FIB_WALK_END;
515

    
516
  return PS_DOWN;
517
}
518

    
519
static void
520
ospf_get_status(struct proto *P, byte * buf)
521
{
522
  struct ospf_proto *p = (struct ospf_proto *) P;
523

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

    
532
    WALK_LIST(ifa, p->iface_list)
533
      WALK_LIST(n, ifa->neigh_list) if (n->state == NEIGHBOR_FULL)
534
      adj = 1;
535

    
536
    if (adj == 0)
537
      strcpy(buf, "Alone");
538
    else
539
      strcpy(buf, "Running");
540
  }
541
}
542

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

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

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

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

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

    
606
  oa->ac = nac;
607

    
608
  if (ospf_is_v2(p))
609
    oa->options = nac->type;
610
  else
611
    oa->options = nac->type | OPT_V6 | (p->stub_router ? 0 : OPT_R);
612

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

    
621
  /* Handle net_list */
622
  fib_free(&oa->net_fib);
623
  fib_free(&oa->enet_fib);
624
  add_area_nets(oa, nac);
625

    
626
  /* No need to handle stubnet_list */
627

    
628
  oa->marked = 0;
629
  ospf_notify_rt_lsa(oa);
630
}
631

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

    
653
  if (proto_get_router_id(c) != p->router_id)
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
  struct area_net *anet;
751

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

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

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

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

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

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

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

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

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

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

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

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

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

    
873
  return dst;
874
}
875

    
876

    
877
static int lsa_compare_ospf3;
878

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

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

    
895

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

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

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

    
912

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1069
  show_lsa_distance(he);
1070

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

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

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

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

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

    
1097

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1309
      cnode = NULL;
1310
    }
1311
  }
1312

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

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

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

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

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

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

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

    
1348

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

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

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

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

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

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

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

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

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

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

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

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

    
1405
  ASSERT(j <= num);
1406

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

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

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

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

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

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

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

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

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

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

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

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

    
1465

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