Statistics
| Branch: | Revision:

iof-bird-daemon / proto / ospf / ospf.c @ 094d2bdb

History | View | Annotate | Download (40.5 KB)

1
/*
2
 *        BIRD -- OSPF
3
 *
4
 *        (c) 1999--2004 Ondrej Filip <feela@network.cz>
5
 *
6
 *        Can be freely distributed and used under the terms of the GNU GPL.
7
 */
8

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

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

    
105

    
106
static int ospf_reload_routes(struct proto *p);
107
static void ospf_rt_notify(struct proto *p, struct rtable *table UNUSED, net * n, rte * new, rte * old UNUSED, ea_list * attrs);
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
static void
113
ospf_area_initfib(struct fib_node *fn)
114
{
115
  struct area_net *an = (struct area_net *) fn;
116
  an->hidden = 0;
117
  an->active = -1;        /* Force to regenerate summary lsa */
118
        /* ac->oldactive will be rewritten by ospf_rt_spf() */
119
}
120

    
121
static void
122
add_area_nets(struct ospf_area *oa, struct ospf_area_config *ac)
123
{
124
    struct proto_ospf *po = oa->po;
125
    struct area_net_config *anc;
126
    struct area_net *an;
127

    
128
    fib_init(&oa->net_fib, po->proto.pool, sizeof(struct area_net), 0, ospf_area_initfib);
129
    fib_init(&oa->enet_fib, po->proto.pool, sizeof(struct area_net), 0, ospf_area_initfib);
130

    
131
    WALK_LIST(anc, ac->net_list)
132
    {
133
      an = (struct area_net *) fib_get(&oa->net_fib, &anc->px.addr, anc->px.len);
134
      an->hidden = anc->hidden;
135
    }
136

    
137
    WALK_LIST(anc, ac->enet_list)
138
    {
139
      an = (struct area_net *) fib_get(&oa->enet_fib, &anc->px.addr, anc->px.len);
140
      an->hidden = anc->hidden;
141
      an->tag = anc->tag;
142
    }
143
}
144

    
145
static void
146
ospf_area_add(struct proto_ospf *po, struct ospf_area_config *ac, int reconf)
147
{
148
  struct proto *p = &po->proto;
149
  struct ospf_area *oa;
150

    
151
  OSPF_TRACE(D_EVENTS, "Adding area %R", ac->areaid);
152

    
153
  oa = mb_allocz(p->pool, sizeof(struct ospf_area));
154
  add_tail(&po->area_list, NODE oa);
155
  po->areano++;
156

    
157
  oa->ac = ac;
158
  oa->areaid = ac->areaid;
159
  oa->rt = NULL;
160
  oa->po = po;
161
  fib_init(&oa->rtr, p->pool, sizeof(ort), 0, ospf_rt_initort);
162
  add_area_nets(oa, ac);
163

    
164
  if (oa->areaid == 0)
165
    po->backbone = oa;
166

    
167
#ifdef OSPFv2
168
  oa->options = ac->type;
169
#else /* OSPFv3 */
170
  oa->options = OPT_R | ac->type | OPT_V6;
171
#endif
172

    
173
  /*
174
   * Set E-bit for NSSA ABR routers. No need to explicitly call
175
   * schedule_rt_lsa() for other areas, will be done anyway.
176
   * We use cf->abr because po->areano is not yet complete.
177
   */
178
  if (oa_is_nssa(oa) && ((struct ospf_config *) (p->cf))->abr)
179
    po->ebit = 1;
180

    
181
  if (reconf)
182
    ospf_ifaces_reconfigure(oa, ac);
183
}
184

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

    
191
  /* We suppose that interfaces are already removed */
192
  ospf_flush_area(oa->po, 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
  oa->po->areano--;
202
  rem_node(NODE oa);
203
  mb_free(oa);
204
}
205

    
206

    
207
struct ospf_area *
208
ospf_find_area(struct proto_ospf *po, u32 aid)
209
{
210
  struct ospf_area *oa;
211
  WALK_LIST(oa, po->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 proto_ospf *po, u32 voa, u32 vid)
219
{
220
  struct ospf_iface *ifa;
221
  WALK_LIST(ifa, po->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 proto_ospf *po = (struct proto_ospf *) p;
231
  struct ospf_config *c = (struct ospf_config *) (p->cf);
232
  struct ospf_area_config *ac;
233

    
234
  po->router_id = proto_get_router_id(p->cf);
235
  po->rfc1583 = c->rfc1583;
236
  po->ebit = 0;
237
  po->ecmp = c->ecmp;
238
  po->tick = c->tick;
239
  po->disp_timer = tm_new(p->pool);
240
  po->disp_timer->data = po;
241
  po->disp_timer->randomize = 0;
242
  po->disp_timer->hook = ospf_disp;
243
  po->disp_timer->recurrent = po->tick;
244
  tm_start(po->disp_timer, 1);
245
  po->lsab_size = 256;
246
  po->lsab_used = 0;
247
  po->lsab = mb_alloc(p->pool, po->lsab_size);
248
  po->nhpool = lp_new(p->pool, 12*sizeof(struct mpnh));
249
  init_list(&(po->iface_list));
250
  init_list(&(po->area_list));
251
  fib_init(&po->rtf, p->pool, sizeof(ort), 0, ospf_rt_initort);
252
  po->areano = 0;
253
  po->gr = ospf_top_new(p->pool);
254
  s_init_list(&(po->lsal));
255

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

    
259
  /* Add all virtual links */
260
  struct ospf_iface_patt *ic;
261
  WALK_LIST(ic, c->vlink_list)
262
    ospf_iface_new(po->backbone, NULL, ic);
263

    
264
  return PS_UP;
265
}
266

    
267
static void
268
ospf_dump(struct proto *p)
269
{
270
  struct ospf_iface *ifa;
271
  struct ospf_neighbor *n;
272
  struct proto_ospf *po = (struct proto_ospf *) p;
273

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

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

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

    
296
static struct proto *
297
ospf_init(struct proto_config *c)
298
{
299
  struct proto *p = proto_new(c, sizeof(struct proto_ospf));
300

    
301
  p->accept_ra_types = RA_OPTIMAL;
302
  p->rt_notify = ospf_rt_notify;
303
  p->if_notify = ospf_if_notify;
304
  p->ifa_notify = ospf_ifa_notify;
305
  p->import_control = ospf_import_control;
306
  p->reload_routes = ospf_reload_routes;
307
  p->make_tmp_attrs = ospf_make_tmp_attrs;
308
  p->store_tmp_attrs = ospf_store_tmp_attrs;
309
  p->rte_better = ospf_rte_better;
310
  p->rte_same = ospf_rte_same;
311

    
312
  return p;
313
}
314

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

    
322
  if(new->attrs->source < old->attrs->source) return 1;
323
  if(new->attrs->source > old->attrs->source) return 0;
324

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

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

    
334
  return 0;                        /* Old is shorter or same */
335
}
336

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

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

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

    
377
void
378
schedule_net_lsa(struct ospf_iface *ifa)
379
{
380
  struct proto *p = &ifa->oa->po->proto;
381

    
382
  OSPF_TRACE(D_EVENTS, "Scheduling network-LSA origination for iface %s", ifa->iface->name);
383
  ifa->orignet = 1;
384
}
385

    
386
#ifdef OSPFv3
387
void
388
schedule_link_lsa(struct ospf_iface *ifa)
389
{
390
  struct proto *p = &ifa->oa->po->proto;
391

    
392
  OSPF_TRACE(D_EVENTS, "Scheduling link-LSA origination for iface %s", ifa->iface->name);
393
  ifa->origlink = 1;
394
}
395
#endif
396

    
397
void
398
schedule_rt_lsa(struct ospf_area *oa)
399
{
400
  struct proto *p = &oa->po->proto;
401

    
402
  OSPF_TRACE(D_EVENTS, "Scheduling router-LSA origination for area %R", oa->areaid);
403
  oa->origrt = 1;
404
}
405

    
406
void
407
schedule_rtcalc(struct proto_ospf *po)
408
{
409
  struct proto *p = &po->proto;
410

    
411
  if (po->calcrt)
412
    return;
413

    
414
  OSPF_TRACE(D_EVENTS, "Scheduling routing table calculation");
415
  po->calcrt = 1;
416
}
417

    
418
static int
419
ospf_reload_routes(struct proto *p)
420
{
421
  struct proto_ospf *po = (struct proto_ospf *) p;
422

    
423
  if (po->calcrt != 2)
424
    OSPF_TRACE(D_EVENTS, "Scheduling routing table calculation with route reload");
425

    
426
  po->calcrt = 2;
427

    
428
  return 1;
429
}
430

    
431
/**
432
 * area_disp - invokes origination of
433
 * router LSA and routing table cleanup
434
 * @oa: ospf area
435
 *
436
 * It invokes aging and when @ospf_area->origrt is set to 1, start
437
 * function for origination of router, network LSAs.
438
 */
439
void
440
area_disp(struct ospf_area *oa)
441
{
442
  struct proto_ospf *po = oa->po;
443
  struct ospf_iface *ifa;
444

    
445
  /* Now try to originage rt_lsa */
446
  if (oa->origrt)
447
    update_rt_lsa(oa);
448

    
449
  /* Now try to originate network LSA's */
450
  WALK_LIST(ifa, po->iface_list)
451
  {
452
#ifdef OSPFv3
453
    /* Link LSA should be originated before Network LSA */
454
    if (ifa->origlink && (ifa->oa == oa))
455
      update_link_lsa(ifa);
456
#endif
457

    
458
    if (ifa->orignet && (ifa->oa == oa))
459
      update_net_lsa(ifa);
460
  }
461
}
462

    
463
/**
464
 * ospf_disp - invokes routing table calculation, aging and also area_disp()
465
 * @timer: timer usually called every @proto_ospf->tick second, @timer->data
466
 * point to @proto_ospf
467
 */
468
void
469
ospf_disp(timer * timer)
470
{
471
  struct proto_ospf *po = timer->data;
472
  struct ospf_area *oa;
473

    
474
  WALK_LIST(oa, po->area_list)
475
    area_disp(oa);
476

    
477
  /* Age LSA DB */
478
  ospf_age(po);
479

    
480
  /* Calculate routing table */
481
  if (po->calcrt)
482
    ospf_rt_spf(po);
483
}
484

    
485

    
486

    
487
/**
488
 * ospf_import_control - accept or reject new route from nest's routing table
489
 * @p: current instance of protocol
490
 * @new: the new route
491
 * @attrs: list of attributes
492
 * @pool: pool for allocation of attributes
493
 *
494
 * Its quite simple. It does not accept our own routes and leaves the decision on
495
 * import to the filters.
496
 */
497

    
498
int
499
ospf_import_control(struct proto *p, rte ** new, ea_list ** attrs,
500
                    struct linpool *pool)
501
{
502
  struct ospf_area *oa = ospf_main_area((struct proto_ospf *) p);
503
  rte *e = *new;
504

    
505
  if (e->attrs->src->proto == p)
506
    return -1;                        /* Reject our own routes */
507

    
508
  if (oa_is_stub(oa))
509
    return -1;                        /* Do not export routes to stub areas */
510

    
511
  eattr *ea = ea_find(e->attrs->eattrs, EA_GEN_IGP_METRIC);
512
  u32 m1 = (ea && (ea->u.data < LSINFINITY)) ? ea->u.data : LSINFINITY;
513

    
514
  *attrs = ospf_build_attrs(*attrs, pool, m1, 10000, 0, 0);
515
  return 0;                        /* Leave decision to the filters */
516
}
517

    
518
struct ea_list *
519
ospf_make_tmp_attrs(struct rte *rt, struct linpool *pool)
520
{
521
  return ospf_build_attrs(NULL, pool, rt->u.ospf.metric1, rt->u.ospf.metric2,
522
                          rt->u.ospf.tag, rt->u.ospf.router_id);
523
}
524

    
525
void
526
ospf_store_tmp_attrs(struct rte *rt, struct ea_list *attrs)
527
{
528
  rt->u.ospf.metric1 = ea_get_int(attrs, EA_OSPF_METRIC1, LSINFINITY);
529
  rt->u.ospf.metric2 = ea_get_int(attrs, EA_OSPF_METRIC2, 10000);
530
  rt->u.ospf.tag = ea_get_int(attrs, EA_OSPF_TAG, 0);
531
  rt->u.ospf.router_id = ea_get_int(attrs, EA_OSPF_ROUTER_ID, 0);
532
}
533

    
534
/**
535
 * ospf_shutdown - Finish of OSPF instance
536
 * @p: current instance of protocol
537
 *
538
 * RFC does not define any action that should be taken before router
539
 * shutdown. To make my neighbors react as fast as possible, I send
540
 * them hello packet with empty neighbor list. They should start
541
 * their neighbor state machine with event %NEIGHBOR_1WAY.
542
 */
543

    
544
static int
545
ospf_shutdown(struct proto *p)
546
{
547
  struct proto_ospf *po = (struct proto_ospf *) p;
548
  struct ospf_iface *ifa;
549
  OSPF_TRACE(D_EVENTS, "Shutdown requested");
550

    
551
  /* And send to all my neighbors 1WAY */
552
  WALK_LIST(ifa, po->iface_list)
553
    ospf_iface_shutdown(ifa);
554

    
555
  /* Cleanup locked rta entries */
556
  FIB_WALK(&po->rtf, nftmp)
557
  {
558
    rta_free(((ort *) nftmp)->old_rta);
559
  }
560
  FIB_WALK_END;
561

    
562
  return PS_DOWN;
563
}
564

    
565
static void
566
ospf_rt_notify(struct proto *p, rtable *tbl UNUSED, net * n, rte * new, rte * old UNUSED, ea_list * attrs)
567
{
568
  struct proto_ospf *po = (struct proto_ospf *) p;
569
  struct ospf_area *oa = ospf_main_area(po);
570
  ort *nf = (ort *) fib_get(&po->rtf, &n->n.prefix, n->n.pxlen);
571
  struct fib_node *fn = &nf->fn;
572

    
573
  if (!new)
574
  {
575
    if (fn->x1 != EXT_EXPORT)
576
      return;
577

    
578
    flush_ext_lsa(oa, fn, oa_is_nssa(oa));
579

    
580
    /* Old external route might blocked some NSSA translation */
581
    if (po->areano > 1)
582
      schedule_rtcalc(po);
583

    
584
    return;
585
  }
586

    
587
  /* Get route attributes */
588
  u32 m1 = ea_get_int(attrs, EA_OSPF_METRIC1, LSINFINITY);
589
  u32 m2 = ea_get_int(attrs, EA_OSPF_METRIC2, 10000);
590
  u32 metric = (m1 != LSINFINITY) ? m1 : (m2 | LSA_EXT_EBIT);
591
  u32 tag = ea_get_int(attrs, EA_OSPF_TAG, 0);
592
  ip_addr gw = IPA_NONE;
593
  // FIXME check for gw should be per ifa, not per iface
594
  if ((new->attrs->dest == RTD_ROUTER) &&
595
      ipa_nonzero(new->attrs->gw) &&
596
      !ipa_has_link_scope(new->attrs->gw) &&
597
      (ospf_iface_find((struct proto_ospf *) p, new->attrs->iface) != NULL))
598
    gw = new->attrs->gw;
599

    
600
  originate_ext_lsa(oa, fn, EXT_EXPORT, metric, gw, tag, 1);
601
}
602

    
603
static void
604
ospf_get_status(struct proto *p, byte * buf)
605
{
606
  struct proto_ospf *po = (struct proto_ospf *) p;
607

    
608
  if (p->proto_state == PS_DOWN)
609
    buf[0] = 0;
610
  else
611
  {
612
    struct ospf_iface *ifa;
613
    struct ospf_neighbor *n;
614
    int adj = 0;
615

    
616
    WALK_LIST(ifa, po->iface_list)
617
      WALK_LIST(n, ifa->neigh_list) if (n->state == NEIGHBOR_FULL)
618
      adj = 1;
619

    
620
    if (adj == 0)
621
      strcpy(buf, "Alone");
622
    else
623
      strcpy(buf, "Running");
624
  }
625
}
626

    
627
static void
628
ospf_get_route_info(rte * rte, byte * buf, ea_list * attrs UNUSED)
629
{
630
  char *type = "<bug>";
631

    
632
  switch(rte->attrs->source)
633
  {
634
    case RTS_OSPF:
635
      type = "I";
636
      break;
637
    case RTS_OSPF_IA:
638
      type = "IA";
639
      break;
640
    case RTS_OSPF_EXT1:
641
      type = "E1";
642
      break;
643
    case RTS_OSPF_EXT2:
644
      type = "E2";
645
      break;
646
  }
647

    
648
  buf += bsprintf(buf, " %s", type);
649
  buf += bsprintf(buf, " (%d/%d", rte->pref, rte->u.ospf.metric1);
650
  if (rte->attrs->source == RTS_OSPF_EXT2)
651
    buf += bsprintf(buf, "/%d", rte->u.ospf.metric2);
652
  buf += bsprintf(buf, ")");
653
  if ((rte->attrs->source == RTS_OSPF_EXT1 || rte->attrs->source == RTS_OSPF_EXT2) && rte->u.ospf.tag)
654
  {
655
    buf += bsprintf(buf, " [%x]", rte->u.ospf.tag);
656
  }
657
  if (rte->u.ospf.router_id)
658
    buf += bsprintf(buf, " [%R]", rte->u.ospf.router_id);
659
}
660

    
661
static int
662
ospf_get_attr(eattr * a, byte * buf, int buflen UNUSED)
663
{
664
  switch (a->id)
665
  {
666
  case EA_OSPF_METRIC1:
667
    bsprintf(buf, "metric1");
668
    return GA_NAME;
669
  case EA_OSPF_METRIC2:
670
    bsprintf(buf, "metric2");
671
    return GA_NAME;
672
  case EA_OSPF_TAG:
673
    bsprintf(buf, "tag: 0x%08x", a->u.data);
674
    return GA_FULL;
675
  case EA_OSPF_ROUTER_ID:
676
    bsprintf(buf, "router_id");
677
    return GA_NAME;
678
  default:
679
    return GA_UNKNOWN;
680
  }
681
}
682

    
683
static void
684
ospf_area_reconfigure(struct ospf_area *oa, struct ospf_area_config *nac)
685
{
686
  oa->ac = nac;
687

    
688
  // FIXME better area type reconfiguration
689
#ifdef OSPFv2
690
  oa->options = nac->type;
691
#else /* OSPFv3 */
692
  oa->options = OPT_R | nac->type | OPT_V6;
693
#endif
694
  if (oa_is_nssa(oa) && (oa->po->areano > 1))
695
    oa->po->ebit = 1;
696

    
697
  ospf_ifaces_reconfigure(oa, nac);
698

    
699
  /* Handle net_list */
700
  fib_free(&oa->net_fib);
701
  fib_free(&oa->enet_fib);
702
  add_area_nets(oa, nac);
703

    
704
  /* No need to handle stubnet_list */
705

    
706
  oa->marked = 0;
707
  schedule_rt_lsa(oa);
708
}
709

    
710
/**
711
 * ospf_reconfigure - reconfiguration hook
712
 * @p: current instance of protocol (with old configuration)
713
 * @c: new configuration requested by user
714
 *
715
 * This hook tries to be a little bit intelligent. Instance of OSPF
716
 * will survive change of many constants like hello interval,
717
 * password change, addition or deletion of some neighbor on
718
 * nonbroadcast network, cost of interface, etc.
719
 */
720
static int
721
ospf_reconfigure(struct proto *p, struct proto_config *c)
722
{
723
  struct proto_ospf *po = (struct proto_ospf *) p;
724
  struct ospf_config *old = (struct ospf_config *) (p->cf);
725
  struct ospf_config *new = (struct ospf_config *) c;
726
  struct ospf_area_config *nac;
727
  struct ospf_area *oa, *oax;
728
  struct ospf_iface *ifa, *ifx;
729
  struct ospf_iface_patt *ip;
730

    
731
  if (po->rfc1583 != new->rfc1583)
732
    return 0;
733

    
734
  if (old->abr != new->abr)
735
    return 0;
736

    
737
  po->ecmp = new->ecmp;
738
  po->tick = new->tick;
739
  po->disp_timer->recurrent = po->tick;
740
  tm_start(po->disp_timer, 1);
741

    
742
  /* Mark all areas and ifaces */
743
  WALK_LIST(oa, po->area_list)
744
    oa->marked = 1;
745

    
746
  WALK_LIST(ifa, po->iface_list)
747
    ifa->marked = 1;
748

    
749
  /* Add and update areas */
750
  WALK_LIST(nac, new->area_list)
751
  {
752
    oa = ospf_find_area(po, nac->areaid);
753
    if (oa)
754
      ospf_area_reconfigure(oa, nac);
755
    else
756
      ospf_area_add(po, nac, 1);
757
  }
758

    
759
  /* Add and update vlinks */
760
  WALK_LIST(ip, new->vlink_list)
761
  {
762
    ifa = ospf_find_vlink(po, ip->voa, ip->vid);
763
    if (ifa)
764
      ospf_iface_reconfigure(ifa, ip);
765
    else
766
      ospf_iface_new(po->backbone, NULL, ip);
767
  }
768

    
769
  /* Delete remaining ifaces and areas */
770
  WALK_LIST_DELSAFE(ifa, ifx, po->iface_list)
771
    if (ifa->marked)
772
    {
773
      ospf_iface_shutdown(ifa);
774
      ospf_iface_remove(ifa);
775
    }
776

    
777
  WALK_LIST_DELSAFE(oa, oax, po->area_list)
778
    if (oa->marked)
779
      ospf_area_remove(oa);
780

    
781
  schedule_rtcalc(po);
782
  
783
  return 1;
784
}
785

    
786

    
787
void
788
ospf_sh_neigh(struct proto *p, char *iff)
789
{
790
  struct ospf_iface *ifa = NULL;
791
  struct ospf_neighbor *n;
792
  struct proto_ospf *po = (struct proto_ospf *) p;
793

    
794
  if (p->proto_state != PS_UP)
795
  {
796
    cli_msg(-1013, "%s: is not up", p->name);
797
    cli_msg(0, "");
798
    return;
799
  }
800

    
801
  cli_msg(-1013, "%s:", p->name);
802
  cli_msg(-1013, "%-12s\t%3s\t%-15s\t%-5s\t%-10s %-12s", "Router ID", "Pri",
803
          "     State", "DTime", "Interface", "Router IP");
804
  WALK_LIST(ifa, po->iface_list)
805
    if ((iff == NULL) || patmatch(iff, ifa->iface->name))
806
      WALK_LIST(n, ifa->neigh_list)
807
        ospf_sh_neigh_info(n);
808
  cli_msg(0, "");
809
}
810

    
811
void
812
ospf_sh(struct proto *p)
813
{
814
  struct ospf_area *oa;
815
  struct proto_ospf *po = (struct proto_ospf *) p;
816
  struct ospf_iface *ifa;
817
  struct ospf_neighbor *n;
818
  int ifano, nno, adjno, firstfib;
819
  struct area_net *anet;
820

    
821
  if (p->proto_state != PS_UP)
822
  {
823
    cli_msg(-1014, "%s: is not up", p->name);
824
    cli_msg(0, "");
825
    return;
826
  }
827

    
828
  cli_msg(-1014, "%s:", p->name);
829
  cli_msg(-1014, "RFC1583 compatibility: %s", (po->rfc1583 ? "enable" : "disabled"));
830
  cli_msg(-1014, "RT scheduler tick: %d", po->tick);
831
  cli_msg(-1014, "Number of areas: %u", po->areano);
832
  cli_msg(-1014, "Number of LSAs in DB:\t%u", po->gr->hash_entries);
833

    
834
  WALK_LIST(oa, po->area_list)
835
  {
836
    cli_msg(-1014, "\tArea: %R (%u) %s", oa->areaid, oa->areaid,
837
            oa->areaid == 0 ? "[BACKBONE]" : "");
838
    ifano = 0;
839
    nno = 0;
840
    adjno = 0;
841
    WALK_LIST(ifa, po->iface_list)
842
    {
843
      if (oa == ifa->oa)
844
      {
845
        ifano++;
846
        WALK_LIST(n, ifa->neigh_list)
847
        {
848
          nno++;
849
          if (n->state == NEIGHBOR_FULL)
850
            adjno++;
851
        }
852
      }
853
    }
854

    
855
    cli_msg(-1014, "\t\tStub:\t%s", oa_is_stub(oa) ? "Yes" : "No");
856
    cli_msg(-1014, "\t\tNSSA:\t%s", oa_is_nssa(oa) ? "Yes" : "No");
857
    cli_msg(-1014, "\t\tTransit:\t%s", oa->trcap ? "Yes" : "No");
858

    
859
    if (oa_is_nssa(oa))
860
      cli_msg(-1014, "\t\tNSSA translation:\t%s%s", oa->translate ? "Yes" : "No",
861
              oa->translate == TRANS_WAIT ? " (run down)" : "");
862
    cli_msg(-1014, "\t\tNumber of interfaces:\t%u", ifano);
863
    cli_msg(-1014, "\t\tNumber of neighbors:\t%u", nno);
864
    cli_msg(-1014, "\t\tNumber of adjacent neighbors:\t%u", adjno);
865

    
866
    firstfib = 1;
867
    FIB_WALK(&oa->net_fib, nftmp)
868
    {
869
      anet = (struct area_net *) nftmp;
870
      if(firstfib)
871
      {
872
        cli_msg(-1014, "\t\tArea networks:");
873
        firstfib = 0;
874
      }
875
      cli_msg(-1014, "\t\t\t%1I/%u\t%s\t%s", anet->fn.prefix, anet->fn.pxlen,
876
                anet->hidden ? "Hidden" : "Advertise", anet->active ? "Active" : "");
877
    }
878
    FIB_WALK_END;
879

    
880
    firstfib = 1;
881
    FIB_WALK(&oa->enet_fib, nftmp)
882
    {
883
      anet = (struct area_net *) nftmp;
884
      if(firstfib)
885
      {
886
        cli_msg(-1014, "\t\tArea external networks:");
887
        firstfib = 0;
888
      }
889
      cli_msg(-1014, "\t\t\t%1I/%u\t%s\t%s", anet->fn.prefix, anet->fn.pxlen,
890
                anet->hidden ? "Hidden" : "Advertise", anet->active ? "Active" : "");
891
    }
892
    FIB_WALK_END;
893

    
894
  }
895
  cli_msg(0, "");
896
}
897

    
898
void
899
ospf_sh_iface(struct proto *p, char *iff)
900
{
901
  struct proto_ospf *po = (struct proto_ospf *) p;
902
  struct ospf_iface *ifa = NULL;
903

    
904
  if (p->proto_state != PS_UP)
905
  {
906
    cli_msg(-1015, "%s: is not up", p->name);
907
    cli_msg(0, "");
908
    return;
909
  }
910

    
911
  cli_msg(-1015, "%s:", p->name);
912
  WALK_LIST(ifa, po->iface_list)
913
    if ((iff == NULL) || patmatch(iff, ifa->iface->name))
914
      ospf_iface_info(ifa);
915
  cli_msg(0, "");
916
}
917

    
918
/* lsa_compare_for_state() - Compare function for 'show ospf state'
919
 *
920
 * First we want to separate network-LSAs and other LSAs (because network-LSAs
921
 * will be presented as network nodes and other LSAs together as router nodes)
922
 * Network-LSAs are sorted according to network prefix, other LSAs are sorted
923
 * according to originating router id (to get all LSA needed to represent one
924
 * router node together). Then, according to LSA type, ID and age.
925
 *
926
 * For OSPFv3, we have to handle also Prefix-LSAs. We would like to put each
927
 * immediately after the referenced LSA. We will make faked LSA based on ref_
928
 * values
929
 */
930

    
931
#ifdef OSPFv3
932

    
933
static struct ospf_lsa_header *
934
fake_lsa_from_prefix_lsa(struct ospf_lsa_header *dst, struct ospf_lsa_header *src,
935
                         struct ospf_lsa_prefix *px)
936
{
937
  dst->age = src->age;
938
  dst->type = px->ref_type;
939
  dst->id = px->ref_id;
940
  dst->rt = px->ref_rt;
941
  dst->sn = src->sn;
942

    
943
  return dst;
944
}
945

    
946
#endif
947

    
948
static int
949
lsa_compare_for_state(const void *p1, const void *p2)
950
{
951
  struct top_hash_entry * he1 = * (struct top_hash_entry **) p1;
952
  struct top_hash_entry * he2 = * (struct top_hash_entry **) p2;
953
  struct ospf_lsa_header *lsa1 = &(he1->lsa);
954
  struct ospf_lsa_header *lsa2 = &(he2->lsa);
955

    
956
  if (he1->domain != he2->domain)
957
    return he1->domain - he2->domain;
958

    
959
#ifdef OSPFv3
960
  struct ospf_lsa_header lsatmp1, lsatmp2;
961

    
962
  int px1 = (lsa1->type == LSA_T_PREFIX);
963
  int px2 = (lsa2->type == LSA_T_PREFIX);
964

    
965
  if (px1)
966
    lsa1 = fake_lsa_from_prefix_lsa(&lsatmp1, lsa1, he1->lsa_body);
967

    
968
  if (px2)
969
    lsa2 = fake_lsa_from_prefix_lsa(&lsatmp2, lsa2, he2->lsa_body);
970
#endif
971

    
972
  int nt1 = (lsa1->type == LSA_T_NET);
973
  int nt2 = (lsa2->type == LSA_T_NET);
974

    
975
  if (nt1 != nt2)
976
    return nt1 - nt2;
977

    
978
  if (nt1)
979
  {
980
#ifdef OSPFv3
981
    /* In OSPFv3, neworks are named base on ID of DR */
982
    if (lsa1->rt != lsa2->rt)
983
      return lsa1->rt - lsa2->rt;
984
#endif
985

    
986
    /* For OSPFv2, this is IP of the network,
987
       for OSPFv3, this is interface ID */
988
    if (lsa1->id != lsa2->id)
989
      return lsa1->id - lsa2->id;
990

    
991
#ifdef OSPFv3
992
    if (px1 != px2)
993
      return px1 - px2;
994
#endif
995

    
996
    return lsa1->sn - lsa2->sn;
997
  }
998
  else 
999
  {
1000
    if (lsa1->rt != lsa2->rt)
1001
      return lsa1->rt - lsa2->rt;
1002

    
1003
    if (lsa1->type != lsa2->type)
1004
      return lsa1->type - lsa2->type;
1005
  
1006
    if (lsa1->id != lsa2->id)
1007
      return lsa1->id - lsa2->id;
1008

    
1009
#ifdef OSPFv3
1010
    if (px1 != px2)
1011
      return px1 - px2;
1012
#endif
1013
  
1014
    return lsa1->sn - lsa2->sn;
1015
  }
1016
}
1017

    
1018
static int
1019
ext_compare_for_state(const void *p1, const void *p2)
1020
{
1021
  struct top_hash_entry * he1 = * (struct top_hash_entry **) p1;
1022
  struct top_hash_entry * he2 = * (struct top_hash_entry **) p2;
1023
  struct ospf_lsa_header *lsa1 = &(he1->lsa);
1024
  struct ospf_lsa_header *lsa2 = &(he2->lsa);
1025

    
1026
  if (lsa1->rt != lsa2->rt)
1027
    return lsa1->rt - lsa2->rt;
1028

    
1029
  if (lsa1->id != lsa2->id)
1030
    return lsa1->id - lsa2->id;
1031
 
1032
  return lsa1->sn - lsa2->sn;
1033
}
1034

    
1035
static inline void
1036
show_lsa_distance(struct top_hash_entry *he)
1037
{
1038
  if (he->color == INSPF)
1039
    cli_msg(-1016, "\t\tdistance %u", he->dist);
1040
  else
1041
    cli_msg(-1016, "\t\tunreachable");
1042
}
1043

    
1044
static inline void
1045
show_lsa_router(struct proto_ospf *po, struct top_hash_entry *he, int first, int verbose)
1046
{
1047
  struct ospf_lsa_header *lsa = &(he->lsa);
1048
  struct ospf_lsa_rt *rt = he->lsa_body;
1049
  struct ospf_lsa_rt_link *rr = (struct ospf_lsa_rt_link *) (rt + 1);
1050
  int max = lsa_rt_count(lsa);
1051
  int i;
1052

    
1053
  if (first)
1054
  {
1055
    cli_msg(-1016, "");
1056
    cli_msg(-1016, "\trouter %R", he->lsa.rt);
1057
    show_lsa_distance(he);
1058
  }
1059

    
1060

    
1061
  for (i = 0; i < max; i++)
1062
    if (rr[i].type == LSART_VLNK)
1063
      cli_msg(-1016, "\t\tvlink %R metric %u", rr[i].id, rr[i].metric);
1064

    
1065
  for (i = 0; i < max; i++)
1066
    if (rr[i].type == LSART_PTP)
1067
      cli_msg(-1016, "\t\trouter %R metric %u", rr[i].id, rr[i].metric);
1068

    
1069
  for (i = 0; i < max; i++)
1070
    if (rr[i].type == LSART_NET)
1071
    {
1072
#ifdef OSPFv2
1073
      struct top_hash_entry *net_he = ospf_hash_find_net(po->gr, he->domain, rr[i].id);
1074

    
1075
      if (net_he)
1076
      {
1077
        struct ospf_lsa_header *net_lsa = &(net_he->lsa);
1078
        struct ospf_lsa_net *net_ln = net_he->lsa_body;
1079

    
1080
        cli_msg(-1016, "\t\tnetwork %I/%d metric %u", 
1081
                ipa_and(ipa_from_u32(net_lsa->id), net_ln->netmask),
1082
                ipa_mklen(net_ln->netmask), rr[i].metric);
1083
      }
1084
      else
1085
        cli_msg(-1016, "\t\tnetwork [%R] metric %u", rr[i].id, rr[i].metric);
1086

    
1087
#else /* OSPFv3 */
1088
      cli_msg(-1016, "\t\tnetwork [%R-%u] metric %u", rr[i].id, rr[i].nif, rr[i].metric);
1089
#endif
1090
    }
1091

    
1092
#ifdef OSPFv2
1093
  if (!verbose)
1094
    return;
1095

    
1096
  for (i = 0; i < max; i++)
1097
    if (rr[i].type == LSART_STUB)
1098
      cli_msg(-1016, "\t\tstubnet %I/%d metric %u", ipa_from_u32(rr[i].id),
1099
              ipa_mklen(ipa_from_u32(rr[i].data)), rr[i].metric);
1100
#endif
1101
}
1102

    
1103
static inline void
1104
show_lsa_network(struct top_hash_entry *he)
1105
{
1106
  struct ospf_lsa_header *lsa = &(he->lsa);
1107
  struct ospf_lsa_net *ln = he->lsa_body;
1108
  u32 i;
1109

    
1110
#ifdef OSPFv2
1111
  cli_msg(-1016, "");
1112
  cli_msg(-1016, "\tnetwork %I/%d", ipa_and(ipa_from_u32(lsa->id), ln->netmask), ipa_mklen(ln->netmask));
1113
  cli_msg(-1016, "\t\tdr %R", lsa->rt);
1114
#else /* OSPFv3 */
1115
  cli_msg(-1016, "");
1116
  cli_msg(-1016, "\tnetwork [%R-%u]", lsa->rt, lsa->id);
1117
#endif
1118

    
1119
  show_lsa_distance(he);
1120

    
1121
  for (i = 0; i < lsa_net_count(lsa); i++)
1122
    cli_msg(-1016, "\t\trouter %R", ln->routers[i]);
1123
}
1124

    
1125
static inline void
1126
show_lsa_sum_net(struct top_hash_entry *he)
1127
{
1128
  ip_addr ip;
1129
  int pxlen;
1130

    
1131
#ifdef OSPFv2
1132
  struct ospf_lsa_sum *ls = he->lsa_body;
1133
  pxlen = ipa_mklen(ls->netmask);
1134
  ip = ipa_and(ipa_from_u32(he->lsa.id), ls->netmask);
1135
#else /* OSPFv3 */
1136
  u8 pxopts;
1137
  u16 rest;
1138
  struct ospf_lsa_sum_net *ls = he->lsa_body;
1139
  lsa_get_ipv6_prefix(ls->prefix, &ip, &pxlen, &pxopts, &rest);
1140
#endif
1141

    
1142
  cli_msg(-1016, "\t\txnetwork %I/%d metric %u", ip, pxlen, ls->metric);
1143
}
1144

    
1145
static inline void
1146
show_lsa_sum_rt(struct top_hash_entry *he)
1147
{
1148
  u32 dst_rid;
1149

    
1150
#ifdef OSPFv2
1151
  struct ospf_lsa_sum *ls = he->lsa_body;
1152
  dst_rid = he->lsa.id;
1153
  // options = 0;
1154
#else /* OSPFv3 */
1155
  struct ospf_lsa_sum_rt *ls = he->lsa_body;
1156
  dst_rid = ls->drid; 
1157
  // options = ls->options & OPTIONS_MASK;
1158
#endif
1159

    
1160
  cli_msg(-1016, "\t\txrouter %R metric %u", dst_rid, ls->metric);
1161
}
1162

    
1163

    
1164
static inline void
1165
show_lsa_external(struct top_hash_entry *he)
1166
{
1167
  struct ospf_lsa_ext *ext = he->lsa_body;
1168
  char str_via[STD_ADDRESS_P_LENGTH + 8] = "";
1169
  char str_tag[16] = "";
1170
  ip_addr ip, rt_fwaddr;
1171
  int pxlen, ebit, rt_fwaddr_valid;
1172
  u32 rt_tag, rt_metric;
1173

    
1174
  if (he->lsa.type == LSA_T_EXT)
1175
    he->domain = 0; /* Unmark the LSA */
1176

    
1177
  rt_metric = ext->metric & METRIC_MASK;
1178
  ebit = ext->metric & LSA_EXT_EBIT;
1179
#ifdef OSPFv2
1180
  ip = ipa_and(ipa_from_u32(he->lsa.id), ext->netmask);
1181
  pxlen = ipa_mklen(ext->netmask);
1182
  rt_fwaddr = ext->fwaddr;
1183
  rt_fwaddr_valid = !ipa_equal(rt_fwaddr, IPA_NONE);
1184
  rt_tag = ext->tag;
1185
#else /* OSPFv3 */
1186
  u8 pxopts;
1187
  u16 rest;
1188
  u32 *buf = ext->rest;
1189
  buf = lsa_get_ipv6_prefix(buf, &ip, &pxlen, &pxopts, &rest);
1190

    
1191
  rt_fwaddr_valid = ext->metric & LSA_EXT_FBIT;
1192
  if (rt_fwaddr_valid)
1193
    buf = lsa_get_ipv6_addr(buf, &rt_fwaddr);
1194
  else 
1195
    rt_fwaddr = IPA_NONE;
1196

    
1197
  if (ext->metric & LSA_EXT_TBIT)
1198
    rt_tag = *buf++;
1199
  else
1200
    rt_tag = 0;
1201
#endif
1202
  
1203
  if (rt_fwaddr_valid)
1204
    bsprintf(str_via, " via %I", rt_fwaddr);
1205

    
1206
  if (rt_tag)
1207
    bsprintf(str_tag, " tag %08x", rt_tag);
1208

    
1209
  cli_msg(-1016, "\t\t%s %I/%d metric%s %u%s%s",
1210
          (he->lsa.type == LSA_T_NSSA) ? "nssa-ext" : "external",
1211
          ip, pxlen, ebit ? "2" : "", rt_metric, str_via, str_tag);
1212
}
1213

    
1214
#ifdef OSPFv3
1215
static inline void
1216
show_lsa_prefix(struct top_hash_entry *he, struct ospf_lsa_header *cnode)
1217
{
1218
  struct ospf_lsa_prefix *px = he->lsa_body;
1219
  ip_addr pxa;
1220
  int pxlen;
1221
  u8 pxopts;
1222
  u16 metric;
1223
  u32 *buf;
1224
  int i;
1225

    
1226
  /* We check whether given prefix-LSA is related to the current node */
1227
  if ((px->ref_type != cnode->type) || (px->ref_rt != cnode->rt))
1228
    return;
1229

    
1230
  if ((px->ref_type == LSA_T_RT) && (px->ref_id != 0))
1231
    return;
1232

    
1233
  if ((px->ref_type == LSA_T_NET) && (px->ref_id != cnode->id))
1234
    return;
1235

    
1236
  buf = px->rest;
1237
  for (i = 0; i < px->pxcount; i++)
1238
    {
1239
      buf = lsa_get_ipv6_prefix(buf, &pxa, &pxlen, &pxopts, &metric);
1240

    
1241
      if (px->ref_type == LSA_T_RT)
1242
        cli_msg(-1016, "\t\tstubnet %I/%d metric %u", pxa, pxlen, metric);
1243
      else
1244
        cli_msg(-1016, "\t\taddress %I/%d", pxa, pxlen);
1245
    }
1246
}
1247
#endif
1248

    
1249
void
1250
ospf_sh_state(struct proto *p, int verbose, int reachable)
1251
{
1252
  struct proto_ospf *po = (struct proto_ospf *) p;
1253
  struct ospf_lsa_header *cnode = NULL;
1254
  int num = po->gr->hash_entries;
1255
  unsigned int i, ix, j1, j2, jx;
1256
  u32 last_area = 0xFFFFFFFF;
1257

    
1258
  if (p->proto_state != PS_UP)
1259
  {
1260
    cli_msg(-1016, "%s: is not up", p->name);
1261
    cli_msg(0, "");
1262
    return;
1263
  }
1264

    
1265
  /* We store interesting area-scoped LSAs in array hea and 
1266
     global-scoped (LSA_T_EXT) LSAs in array hex */
1267

    
1268
  struct top_hash_entry *hea[num];
1269
  struct top_hash_entry *hex[verbose ? num : 0];
1270
  struct top_hash_entry *he;
1271

    
1272
  j1 = j2 = jx = 0;
1273
  WALK_SLIST(he, po->lsal)
1274
  {
1275
    int accept;
1276

    
1277
    switch (he->lsa.type)
1278
      {
1279
      case LSA_T_RT:
1280
      case LSA_T_NET:
1281
        accept = 1;
1282
        break;
1283

    
1284
      case LSA_T_SUM_NET:
1285
      case LSA_T_SUM_RT:
1286
      case LSA_T_NSSA:
1287
#ifdef OSPFv3
1288
      case LSA_T_PREFIX:
1289
#endif
1290
        accept = verbose;
1291
        break;
1292

    
1293
      case LSA_T_EXT:
1294
        if (verbose)
1295
        {
1296
          he->domain = 1; /* Abuse domain field to mark the LSA */
1297
          hex[jx++] = he;
1298
        }
1299
      default:
1300
        accept = 0;
1301
      }
1302

    
1303
    if (accept)
1304
      hea[j1++] = he;
1305
    else
1306
      j2++;
1307
  }
1308

    
1309
  if ((j1 + j2) != num)
1310
    die("Fatal mismatch");
1311

    
1312
  qsort(hea, j1, sizeof(struct top_hash_entry *), lsa_compare_for_state);
1313
  qsort(hex, jx, sizeof(struct top_hash_entry *), ext_compare_for_state);
1314

    
1315
  /*
1316
   * This code is a bit tricky, we have a primary LSAs (router and
1317
   * network) that are presented as a node, and secondary LSAs that
1318
   * are presented as a part of a primary node. cnode represents an
1319
   * currently opened node (whose header was presented). The LSAs are
1320
   * sorted to get secondary LSAs just after related primary LSA (if
1321
   * available). We present secondary LSAs only when related primary
1322
   * LSA is opened.
1323
   *
1324
   * AS-external LSAs are stored separately as they might be presented
1325
   * several times (for each area when related ASBR is opened). When
1326
   * the node is closed, related external routes are presented. We
1327
   * also have to take into account that in OSPFv3, there might be
1328
   * more router-LSAs and only the first should be considered as a
1329
   * primary. This is handled by not closing old router-LSA when next
1330
   * one is processed (which is not opened because there is already
1331
   * one opened).
1332
   */
1333

    
1334
  ix = 0;
1335
  for (i = 0; i < j1; i++)
1336
  {
1337
    he = hea[i];
1338

    
1339
    /* If there is no opened node, we open the LSA (if appropriate) or skip to the next one */
1340
    if (!cnode)
1341
    {
1342
      if (((he->lsa.type == LSA_T_RT) || (he->lsa.type == LSA_T_NET))
1343
          && ((he->color == INSPF) || !reachable))
1344
      {
1345
        cnode = &(he->lsa);
1346

    
1347
        if (he->domain != last_area)
1348
        {
1349
          cli_msg(-1016, "");
1350
          cli_msg(-1016, "area %R", he->domain);
1351
          last_area = he->domain;
1352
          ix = 0;
1353
        }
1354
      }
1355
      else
1356
        continue;
1357
    }
1358

    
1359
    ASSERT(cnode && (he->domain == last_area) && (he->lsa.rt == cnode->rt));
1360

    
1361
    switch (he->lsa.type)
1362
    {
1363
      case LSA_T_RT:
1364
        show_lsa_router(po, he, he->lsa.id == cnode->id, verbose);
1365
        break;
1366

    
1367
      case LSA_T_NET:
1368
        show_lsa_network(he);
1369
        break;
1370

    
1371
      case LSA_T_SUM_NET:
1372
        if (cnode->type == LSA_T_RT)
1373
          show_lsa_sum_net(he);
1374
        break;
1375

    
1376
      case LSA_T_SUM_RT:
1377
        if (cnode->type == LSA_T_RT)
1378
          show_lsa_sum_rt(he);
1379
        break;
1380

    
1381
#ifdef OSPFv3
1382
      case LSA_T_PREFIX:
1383
        show_lsa_prefix(he, cnode);
1384
        break;
1385
#endif
1386

    
1387
      case LSA_T_EXT:
1388
      case LSA_T_NSSA:
1389
        show_lsa_external(he);
1390
        break;
1391
    }
1392

    
1393
    /* In these cases, we close the current node */
1394
    if ((i+1 == j1)
1395
        || (hea[i+1]->domain != last_area)
1396
        || (hea[i+1]->lsa.rt != cnode->rt)
1397
        || (hea[i+1]->lsa.type == LSA_T_NET))
1398
    {
1399
      while ((ix < jx) && (hex[ix]->lsa.rt < cnode->rt))
1400
        ix++;
1401

    
1402
      while ((ix < jx) && (hex[ix]->lsa.rt == cnode->rt))
1403
        show_lsa_external(hex[ix++]);
1404

    
1405
      cnode = NULL;
1406
    }
1407
  }
1408

    
1409
  int hdr = 0;
1410
  u32 last_rt = 0xFFFFFFFF;
1411
  for (ix = 0; ix < jx; ix++)
1412
  {
1413
    he = hex[ix];
1414

    
1415
    /* If it is still marked, we show it now. */
1416
    if (he->domain)
1417
    {
1418
      he->domain = 0;
1419

    
1420
      if ((he->color != INSPF) && reachable)
1421
        continue;
1422

    
1423
      if (!hdr)
1424
      {
1425
        cli_msg(-1016, "");
1426
        cli_msg(-1016, "other ASBRs");
1427
        hdr = 1;
1428
      }
1429

    
1430
      if (he->lsa.rt != last_rt)
1431
      {
1432
        cli_msg(-1016, "");
1433
        cli_msg(-1016, "\trouter %R", he->lsa.rt);
1434
        last_rt = he->lsa.rt;
1435
      }
1436

    
1437
      show_lsa_external(he);
1438
    }
1439
  }
1440

    
1441
  cli_msg(0, "");
1442
}
1443

    
1444

    
1445
static int
1446
lsa_compare_for_lsadb(const void *p1, const void *p2)
1447
{
1448
  struct top_hash_entry * he1 = * (struct top_hash_entry **) p1;
1449
  struct top_hash_entry * he2 = * (struct top_hash_entry **) p2;
1450
  struct ospf_lsa_header *lsa1 = &(he1->lsa);
1451
  struct ospf_lsa_header *lsa2 = &(he2->lsa);
1452
  int sc1 = LSA_SCOPE(lsa1);
1453
  int sc2 = LSA_SCOPE(lsa2);
1454

    
1455
  if (sc1 != sc2)
1456
    return sc2 - sc1;
1457

    
1458
  if (he1->domain != he2->domain)
1459
    return he1->domain - he2->domain;
1460

    
1461
  if (lsa1->rt != lsa2->rt)
1462
    return lsa1->rt - lsa2->rt;
1463
  
1464
  if (lsa1->id != lsa2->id)
1465
    return lsa1->id - lsa2->id;
1466

    
1467
  if (lsa1->type != lsa2->type)
1468
    return lsa1->type - lsa2->type;
1469

    
1470
  return lsa1->sn - lsa2->sn;
1471
}
1472

    
1473
void
1474
ospf_sh_lsadb(struct lsadb_show_data *ld)
1475
{
1476
  struct proto *p = proto_get_named(ld->name, &proto_ospf);
1477
  struct proto_ospf *po = (struct proto_ospf *) p;
1478
  int num = po->gr->hash_entries;
1479
  unsigned int i, j;
1480
  int last_dscope = -1;
1481
  u32 last_domain = 0;
1482

    
1483
  if (p->proto_state != PS_UP)
1484
  {
1485
    cli_msg(-1017, "%s: is not up", p->name);
1486
    cli_msg(0, "");
1487
    return;
1488
  }
1489

    
1490
  if (ld->router == SH_ROUTER_SELF)
1491
    ld->router = po->router_id;
1492

    
1493
  struct top_hash_entry *hea[num];
1494
  struct top_hash_entry *he;
1495

    
1496
  j = 0;
1497
  WALK_SLIST(he, po->lsal)
1498
    hea[j++] = he;
1499

    
1500
  if (j != num)
1501
    die("Fatal mismatch");
1502

    
1503
  qsort(hea, j, sizeof(struct top_hash_entry *), lsa_compare_for_lsadb);
1504

    
1505
  for (i = 0; i < j; i++)
1506
  {
1507
    struct ospf_lsa_header *lsa = &(hea[i]->lsa);
1508
    int dscope = LSA_SCOPE(lsa);
1509

    
1510
    if (ld->scope && (dscope != (ld->scope & 0xf000)))
1511
      continue;
1512

    
1513
    if ((ld->scope == LSA_SCOPE_AREA) && (hea[i]->domain != ld->area))
1514
      continue;
1515

    
1516
    /* Ignore high nibble */
1517
    if (ld->type && ((lsa->type & 0x0fff) != (ld->type & 0x0fff)))
1518
      continue;
1519

    
1520
    if (ld->lsid && (lsa->id != ld->lsid))
1521
      continue;
1522

    
1523
    if (ld->router && (lsa->rt != ld->router))
1524
      continue;
1525
    
1526
    if ((dscope != last_dscope) || (hea[i]->domain != last_domain))
1527
    {
1528
      cli_msg(-1017, "");
1529
      switch (dscope)
1530
      {
1531
        case LSA_SCOPE_AS:
1532
          cli_msg(-1017, "Global");
1533
          break;
1534
        case LSA_SCOPE_AREA:
1535
          cli_msg(-1017, "Area %R", hea[i]->domain);
1536
          break;
1537
#ifdef OSPFv3
1538
        case LSA_SCOPE_LINK:
1539
          {
1540
            struct iface *ifa = if_find_by_index(hea[i]->domain);
1541
            cli_msg(-1017, "Link %s", (ifa != NULL) ? ifa->name : "?");
1542
          }
1543
          break;
1544
#endif
1545
      }
1546
      cli_msg(-1017, "");
1547
      cli_msg(-1017," Type   LS ID           Router           Age  Sequence  Checksum");
1548

    
1549
      last_dscope = dscope;
1550
      last_domain = hea[i]->domain;
1551
    }
1552

    
1553

    
1554
    cli_msg(-1017," %04x  %-15R %-15R %5u  %08x    %04x",
1555
            lsa->type, lsa->id, lsa->rt, lsa->age, lsa->sn, lsa->checksum);
1556
  }
1557
  cli_msg(0, "");
1558
}
1559

    
1560

    
1561
struct protocol proto_ospf = {
1562
  name:                        "OSPF",
1563
  template:                "ospf%d",
1564
  attr_class:                EAP_OSPF,
1565
  preference:                DEF_PREF_OSPF,
1566
  init:                        ospf_init,
1567
  dump:                        ospf_dump,
1568
  start:                ospf_start,
1569
  shutdown:                ospf_shutdown,
1570
  reconfigure:                ospf_reconfigure,
1571
  get_status:                ospf_get_status,
1572
  get_attr:                ospf_get_attr,
1573
  get_route_info:        ospf_get_route_info
1574
  // show_proto_info:        ospf_sh
1575
};