Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (41 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 = ac->type | OPT_V6 | (po->stub_router ? 0 : OPT_R);
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->last_vlink_id = 0x80000000;
236
  po->rfc1583 = c->rfc1583;
237
  po->stub_router = c->stub_router;
238
  po->ebit = 0;
239
  po->ecmp = c->ecmp;
240
  po->tick = c->tick;
241
  po->disp_timer = tm_new(p->pool);
242
  po->disp_timer->data = po;
243
  po->disp_timer->randomize = 0;
244
  po->disp_timer->hook = ospf_disp;
245
  po->disp_timer->recurrent = po->tick;
246
  tm_start(po->disp_timer, 1);
247
  po->lsab_size = 256;
248
  po->lsab_used = 0;
249
  po->lsab = mb_alloc(p->pool, po->lsab_size);
250
  po->nhpool = lp_new(p->pool, 12*sizeof(struct mpnh));
251
  init_list(&(po->iface_list));
252
  init_list(&(po->area_list));
253
  fib_init(&po->rtf, p->pool, sizeof(ort), 0, ospf_rt_initort);
254
  po->areano = 0;
255
  po->gr = ospf_top_new(p->pool);
256
  s_init_list(&(po->lsal));
257

    
258
  WALK_LIST(ac, c->area_list)
259
    ospf_area_add(po, ac, 0);
260

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

    
266
  return PS_UP;
267
}
268

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

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

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

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

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

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

    
314
  return p;
315
}
316

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

    
324
  if(new->attrs->source < old->attrs->source) return 1;
325
  if(new->attrs->source > old->attrs->source) return 0;
326

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

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

    
336
  return 0;                        /* Old is shorter or same */
337
}
338

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

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

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

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

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

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

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

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

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

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

    
413
  if (po->calcrt)
414
    return;
415

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

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

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

    
428
  po->calcrt = 2;
429

    
430
  return 1;
431
}
432

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

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

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

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

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

    
476
  WALK_LIST(oa, po->area_list)
477
    area_disp(oa);
478

    
479
  /* Age LSA DB */
480
  ospf_age(po);
481

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

    
487

    
488

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

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

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

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

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

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

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

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

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

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

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

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

    
564
  return PS_DOWN;
565
}
566

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

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

    
580
    flush_ext_lsa(oa, fn, oa_is_nssa(oa));
581

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

    
586
    return;
587
  }
588

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

    
602
  originate_ext_lsa(oa, fn, EXT_EXPORT, metric, gw, tag, 1);
603
}
604

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

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

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

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

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

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

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

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

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

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

    
699
  ospf_ifaces_reconfigure(oa, nac);
700

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

    
706
  /* No need to handle stubnet_list */
707

    
708
  oa->marked = 0;
709
  schedule_rt_lsa(oa);
710
}
711

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

    
733
  if (proto_get_router_id(c) != po->router_id)
734
    return 0;
735

    
736
  if (po->rfc1583 != new->rfc1583)
737
    return 0;
738

    
739
  if (old->abr != new->abr)
740
    return 0;
741

    
742
  po->stub_router = new->stub_router;
743
  po->ecmp = new->ecmp;
744
  po->tick = new->tick;
745
  po->disp_timer->recurrent = po->tick;
746
  tm_start(po->disp_timer, 1);
747

    
748
  /* Mark all areas and ifaces */
749
  WALK_LIST(oa, po->area_list)
750
    oa->marked = 1;
751

    
752
  WALK_LIST(ifa, po->iface_list)
753
    ifa->marked = 1;
754

    
755
  /* Add and update areas */
756
  WALK_LIST(nac, new->area_list)
757
  {
758
    oa = ospf_find_area(po, nac->areaid);
759
    if (oa)
760
      ospf_area_reconfigure(oa, nac);
761
    else
762
      ospf_area_add(po, nac, 1);
763
  }
764

    
765
  /* Add and update vlinks */
766
  WALK_LIST(ip, new->vlink_list)
767
  {
768
    ifa = ospf_find_vlink(po, ip->voa, ip->vid);
769
    if (ifa)
770
      ospf_iface_reconfigure(ifa, ip);
771
    else
772
      ospf_iface_new(po->backbone, NULL, ip);
773
  }
774

    
775
  /* Delete remaining ifaces and areas */
776
  WALK_LIST_DELSAFE(ifa, ifx, po->iface_list)
777
    if (ifa->marked)
778
    {
779
      ospf_iface_shutdown(ifa);
780
      ospf_iface_remove(ifa);
781
    }
782

    
783
  WALK_LIST_DELSAFE(oa, oax, po->area_list)
784
    if (oa->marked)
785
      ospf_area_remove(oa);
786

    
787
  schedule_rtcalc(po);
788
  
789
  return 1;
790
}
791

    
792

    
793
void
794
ospf_sh_neigh(struct proto *p, char *iff)
795
{
796
  struct ospf_iface *ifa = NULL;
797
  struct ospf_neighbor *n;
798
  struct proto_ospf *po = (struct proto_ospf *) p;
799

    
800
  if (p->proto_state != PS_UP)
801
  {
802
    cli_msg(-1013, "%s: is not up", p->name);
803
    cli_msg(0, "");
804
    return;
805
  }
806

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

    
817
void
818
ospf_sh(struct proto *p)
819
{
820
  struct ospf_area *oa;
821
  struct proto_ospf *po = (struct proto_ospf *) p;
822
  struct ospf_iface *ifa;
823
  struct ospf_neighbor *n;
824
  int ifano, nno, adjno, firstfib;
825
  struct area_net *anet;
826

    
827
  if (p->proto_state != PS_UP)
828
  {
829
    cli_msg(-1014, "%s: is not up", p->name);
830
    cli_msg(0, "");
831
    return;
832
  }
833

    
834
  cli_msg(-1014, "%s:", p->name);
835
  cli_msg(-1014, "RFC1583 compatibility: %s", (po->rfc1583 ? "enable" : "disabled"));
836
  cli_msg(-1014, "Stub router: %s", (po->stub_router ? "Yes" : "No"));
837
  cli_msg(-1014, "RT scheduler tick: %d", po->tick);
838
  cli_msg(-1014, "Number of areas: %u", po->areano);
839
  cli_msg(-1014, "Number of LSAs in DB:\t%u", po->gr->hash_entries);
840

    
841
  WALK_LIST(oa, po->area_list)
842
  {
843
    cli_msg(-1014, "\tArea: %R (%u) %s", oa->areaid, oa->areaid,
844
            oa->areaid == 0 ? "[BACKBONE]" : "");
845
    ifano = 0;
846
    nno = 0;
847
    adjno = 0;
848
    WALK_LIST(ifa, po->iface_list)
849
    {
850
      if (oa == ifa->oa)
851
      {
852
        ifano++;
853
        WALK_LIST(n, ifa->neigh_list)
854
        {
855
          nno++;
856
          if (n->state == NEIGHBOR_FULL)
857
            adjno++;
858
        }
859
      }
860
    }
861

    
862
    cli_msg(-1014, "\t\tStub:\t%s", oa_is_stub(oa) ? "Yes" : "No");
863
    cli_msg(-1014, "\t\tNSSA:\t%s", oa_is_nssa(oa) ? "Yes" : "No");
864
    cli_msg(-1014, "\t\tTransit:\t%s", oa->trcap ? "Yes" : "No");
865

    
866
    if (oa_is_nssa(oa))
867
      cli_msg(-1014, "\t\tNSSA translation:\t%s%s", oa->translate ? "Yes" : "No",
868
              oa->translate == TRANS_WAIT ? " (run down)" : "");
869
    cli_msg(-1014, "\t\tNumber of interfaces:\t%u", ifano);
870
    cli_msg(-1014, "\t\tNumber of neighbors:\t%u", nno);
871
    cli_msg(-1014, "\t\tNumber of adjacent neighbors:\t%u", adjno);
872

    
873
    firstfib = 1;
874
    FIB_WALK(&oa->net_fib, nftmp)
875
    {
876
      anet = (struct area_net *) nftmp;
877
      if(firstfib)
878
      {
879
        cli_msg(-1014, "\t\tArea networks:");
880
        firstfib = 0;
881
      }
882
      cli_msg(-1014, "\t\t\t%1I/%u\t%s\t%s", anet->fn.prefix, anet->fn.pxlen,
883
                anet->hidden ? "Hidden" : "Advertise", anet->active ? "Active" : "");
884
    }
885
    FIB_WALK_END;
886

    
887
    firstfib = 1;
888
    FIB_WALK(&oa->enet_fib, nftmp)
889
    {
890
      anet = (struct area_net *) nftmp;
891
      if(firstfib)
892
      {
893
        cli_msg(-1014, "\t\tArea external networks:");
894
        firstfib = 0;
895
      }
896
      cli_msg(-1014, "\t\t\t%1I/%u\t%s\t%s", anet->fn.prefix, anet->fn.pxlen,
897
                anet->hidden ? "Hidden" : "Advertise", anet->active ? "Active" : "");
898
    }
899
    FIB_WALK_END;
900

    
901
  }
902
  cli_msg(0, "");
903
}
904

    
905
void
906
ospf_sh_iface(struct proto *p, char *iff)
907
{
908
  struct proto_ospf *po = (struct proto_ospf *) p;
909
  struct ospf_iface *ifa = NULL;
910

    
911
  if (p->proto_state != PS_UP)
912
  {
913
    cli_msg(-1015, "%s: is not up", p->name);
914
    cli_msg(0, "");
915
    return;
916
  }
917

    
918
  cli_msg(-1015, "%s:", p->name);
919
  WALK_LIST(ifa, po->iface_list)
920
    if ((iff == NULL) || patmatch(iff, ifa->iface->name))
921
      ospf_iface_info(ifa);
922
  cli_msg(0, "");
923
}
924

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

    
938
#ifdef OSPFv3
939

    
940
static struct ospf_lsa_header *
941
fake_lsa_from_prefix_lsa(struct ospf_lsa_header *dst, struct ospf_lsa_header *src,
942
                         struct ospf_lsa_prefix *px)
943
{
944
  dst->age = src->age;
945
  dst->type = px->ref_type;
946
  dst->id = px->ref_id;
947
  dst->rt = px->ref_rt;
948
  dst->sn = src->sn;
949

    
950
  return dst;
951
}
952

    
953
#endif
954

    
955
static int
956
lsa_compare_for_state(const void *p1, const void *p2)
957
{
958
  struct top_hash_entry * he1 = * (struct top_hash_entry **) p1;
959
  struct top_hash_entry * he2 = * (struct top_hash_entry **) p2;
960
  struct ospf_lsa_header *lsa1 = &(he1->lsa);
961
  struct ospf_lsa_header *lsa2 = &(he2->lsa);
962

    
963
  if (he1->domain < he2->domain)
964
    return -1;
965
  if (he1->domain > he2->domain)
966
    return 1;
967

    
968
#ifdef OSPFv3
969
  struct ospf_lsa_header lsatmp1, lsatmp2;
970

    
971
  int px1 = (lsa1->type == LSA_T_PREFIX);
972
  int px2 = (lsa2->type == LSA_T_PREFIX);
973

    
974
  if (px1)
975
    lsa1 = fake_lsa_from_prefix_lsa(&lsatmp1, lsa1, he1->lsa_body);
976

    
977
  if (px2)
978
    lsa2 = fake_lsa_from_prefix_lsa(&lsatmp2, lsa2, he2->lsa_body);
979
#endif
980

    
981
  int nt1 = (lsa1->type == LSA_T_NET);
982
  int nt2 = (lsa2->type == LSA_T_NET);
983

    
984
  if (nt1 != nt2)
985
    return nt1 - nt2;
986

    
987
  if (nt1)
988
  {
989
#ifdef OSPFv3
990
    /* In OSPFv3, neworks are named base on ID of DR */
991
    if (lsa1->rt < lsa2->rt)
992
      return -1;
993
    if (lsa1->rt > lsa2->rt)
994
      return 1;
995
#endif
996

    
997
    /* For OSPFv2, this is IP of the network,
998
       for OSPFv3, this is interface ID */
999
    if (lsa1->id < lsa2->id)
1000
      return -1;
1001
    if (lsa1->id > lsa2->id)
1002
      return 1;
1003

    
1004
#ifdef OSPFv3
1005
    if (px1 != px2)
1006
      return px1 - px2;
1007
#endif
1008

    
1009
    return lsa1->sn - lsa2->sn;
1010
  }
1011
  else 
1012
  {
1013
    if (lsa1->rt < lsa2->rt)
1014
      return -1;
1015
    if (lsa1->rt > lsa2->rt)
1016
      return 1;
1017

    
1018
    if (lsa1->type < lsa2->type)
1019
      return -1;
1020
    if (lsa1->type > lsa2->type)
1021
      return 1;
1022

    
1023
    if (lsa1->id < lsa2->id)
1024
      return -1;
1025
    if (lsa1->id > lsa2->id)
1026
      return 1;
1027

    
1028
#ifdef OSPFv3
1029
    if (px1 != px2)
1030
      return px1 - px2;
1031
#endif
1032
  
1033
    return lsa1->sn - lsa2->sn;
1034
  }
1035
}
1036

    
1037
static int
1038
ext_compare_for_state(const void *p1, const void *p2)
1039
{
1040
  struct top_hash_entry * he1 = * (struct top_hash_entry **) p1;
1041
  struct top_hash_entry * he2 = * (struct top_hash_entry **) p2;
1042
  struct ospf_lsa_header *lsa1 = &(he1->lsa);
1043
  struct ospf_lsa_header *lsa2 = &(he2->lsa);
1044

    
1045
  if (lsa1->rt < lsa2->rt)
1046
    return -1;
1047
  if (lsa1->rt > lsa2->rt)
1048
    return 1;
1049

    
1050
  if (lsa1->id < lsa2->id)
1051
    return -1;
1052
  if (lsa1->id > lsa2->id)
1053
    return 1;
1054

    
1055
  return lsa1->sn - lsa2->sn;
1056
}
1057

    
1058
static inline void
1059
show_lsa_distance(struct top_hash_entry *he)
1060
{
1061
  if (he->color == INSPF)
1062
    cli_msg(-1016, "\t\tdistance %u", he->dist);
1063
  else
1064
    cli_msg(-1016, "\t\tunreachable");
1065
}
1066

    
1067
static inline void
1068
show_lsa_router(struct proto_ospf *po, struct top_hash_entry *he, int first, int verbose)
1069
{
1070
  struct ospf_lsa_header *lsa = &(he->lsa);
1071
  struct ospf_lsa_rt *rt = he->lsa_body;
1072
  struct ospf_lsa_rt_link *rr = (struct ospf_lsa_rt_link *) (rt + 1);
1073
  int max = lsa_rt_count(lsa);
1074
  int i;
1075

    
1076
  if (first)
1077
  {
1078
    cli_msg(-1016, "");
1079
    cli_msg(-1016, "\trouter %R", he->lsa.rt);
1080
    show_lsa_distance(he);
1081
  }
1082

    
1083

    
1084
  for (i = 0; i < max; i++)
1085
    if (rr[i].type == LSART_VLNK)
1086
      cli_msg(-1016, "\t\tvlink %R metric %u", rr[i].id, rr[i].metric);
1087

    
1088
  for (i = 0; i < max; i++)
1089
    if (rr[i].type == LSART_PTP)
1090
      cli_msg(-1016, "\t\trouter %R metric %u", rr[i].id, rr[i].metric);
1091

    
1092
  for (i = 0; i < max; i++)
1093
    if (rr[i].type == LSART_NET)
1094
    {
1095
#ifdef OSPFv2
1096
      struct top_hash_entry *net_he = ospf_hash_find_net(po->gr, he->domain, rr[i].id);
1097

    
1098
      if (net_he)
1099
      {
1100
        struct ospf_lsa_header *net_lsa = &(net_he->lsa);
1101
        struct ospf_lsa_net *net_ln = net_he->lsa_body;
1102

    
1103
        cli_msg(-1016, "\t\tnetwork %I/%d metric %u", 
1104
                ipa_and(ipa_from_u32(net_lsa->id), net_ln->netmask),
1105
                ipa_mklen(net_ln->netmask), rr[i].metric);
1106
      }
1107
      else
1108
        cli_msg(-1016, "\t\tnetwork [%R] metric %u", rr[i].id, rr[i].metric);
1109

    
1110
#else /* OSPFv3 */
1111
      cli_msg(-1016, "\t\tnetwork [%R-%u] metric %u", rr[i].id, rr[i].nif, rr[i].metric);
1112
#endif
1113
    }
1114

    
1115
#ifdef OSPFv2
1116
  if (!verbose)
1117
    return;
1118

    
1119
  for (i = 0; i < max; i++)
1120
    if (rr[i].type == LSART_STUB)
1121
      cli_msg(-1016, "\t\tstubnet %I/%d metric %u", ipa_from_u32(rr[i].id),
1122
              ipa_mklen(ipa_from_u32(rr[i].data)), rr[i].metric);
1123
#endif
1124
}
1125

    
1126
static inline void
1127
show_lsa_network(struct top_hash_entry *he)
1128
{
1129
  struct ospf_lsa_header *lsa = &(he->lsa);
1130
  struct ospf_lsa_net *ln = he->lsa_body;
1131
  u32 i;
1132

    
1133
#ifdef OSPFv2
1134
  cli_msg(-1016, "");
1135
  cli_msg(-1016, "\tnetwork %I/%d", ipa_and(ipa_from_u32(lsa->id), ln->netmask), ipa_mklen(ln->netmask));
1136
  cli_msg(-1016, "\t\tdr %R", lsa->rt);
1137
#else /* OSPFv3 */
1138
  cli_msg(-1016, "");
1139
  cli_msg(-1016, "\tnetwork [%R-%u]", lsa->rt, lsa->id);
1140
#endif
1141

    
1142
  show_lsa_distance(he);
1143

    
1144
  for (i = 0; i < lsa_net_count(lsa); i++)
1145
    cli_msg(-1016, "\t\trouter %R", ln->routers[i]);
1146
}
1147

    
1148
static inline void
1149
show_lsa_sum_net(struct top_hash_entry *he)
1150
{
1151
  ip_addr ip;
1152
  int pxlen;
1153

    
1154
#ifdef OSPFv2
1155
  struct ospf_lsa_sum *ls = he->lsa_body;
1156
  pxlen = ipa_mklen(ls->netmask);
1157
  ip = ipa_and(ipa_from_u32(he->lsa.id), ls->netmask);
1158
#else /* OSPFv3 */
1159
  u8 pxopts;
1160
  u16 rest;
1161
  struct ospf_lsa_sum_net *ls = he->lsa_body;
1162
  lsa_get_ipv6_prefix(ls->prefix, &ip, &pxlen, &pxopts, &rest);
1163
#endif
1164

    
1165
  cli_msg(-1016, "\t\txnetwork %I/%d metric %u", ip, pxlen, ls->metric);
1166
}
1167

    
1168
static inline void
1169
show_lsa_sum_rt(struct top_hash_entry *he)
1170
{
1171
  u32 dst_rid;
1172

    
1173
#ifdef OSPFv2
1174
  struct ospf_lsa_sum *ls = he->lsa_body;
1175
  dst_rid = he->lsa.id;
1176
  // options = 0;
1177
#else /* OSPFv3 */
1178
  struct ospf_lsa_sum_rt *ls = he->lsa_body;
1179
  dst_rid = ls->drid; 
1180
  // options = ls->options & OPTIONS_MASK;
1181
#endif
1182

    
1183
  cli_msg(-1016, "\t\txrouter %R metric %u", dst_rid, ls->metric);
1184
}
1185

    
1186

    
1187
static inline void
1188
show_lsa_external(struct top_hash_entry *he)
1189
{
1190
  struct ospf_lsa_ext *ext = he->lsa_body;
1191
  char str_via[STD_ADDRESS_P_LENGTH + 8] = "";
1192
  char str_tag[16] = "";
1193
  ip_addr ip, rt_fwaddr;
1194
  int pxlen, ebit, rt_fwaddr_valid;
1195
  u32 rt_tag, rt_metric;
1196

    
1197
  if (he->lsa.type == LSA_T_EXT)
1198
    he->domain = 0; /* Unmark the LSA */
1199

    
1200
  rt_metric = ext->metric & METRIC_MASK;
1201
  ebit = ext->metric & LSA_EXT_EBIT;
1202
#ifdef OSPFv2
1203
  ip = ipa_and(ipa_from_u32(he->lsa.id), ext->netmask);
1204
  pxlen = ipa_mklen(ext->netmask);
1205
  rt_fwaddr = ext->fwaddr;
1206
  rt_fwaddr_valid = !ipa_equal(rt_fwaddr, IPA_NONE);
1207
  rt_tag = ext->tag;
1208
#else /* OSPFv3 */
1209
  u8 pxopts;
1210
  u16 rest;
1211
  u32 *buf = ext->rest;
1212
  buf = lsa_get_ipv6_prefix(buf, &ip, &pxlen, &pxopts, &rest);
1213

    
1214
  rt_fwaddr_valid = ext->metric & LSA_EXT_FBIT;
1215
  if (rt_fwaddr_valid)
1216
    buf = lsa_get_ipv6_addr(buf, &rt_fwaddr);
1217
  else 
1218
    rt_fwaddr = IPA_NONE;
1219

    
1220
  if (ext->metric & LSA_EXT_TBIT)
1221
    rt_tag = *buf++;
1222
  else
1223
    rt_tag = 0;
1224
#endif
1225
  
1226
  if (rt_fwaddr_valid)
1227
    bsprintf(str_via, " via %I", rt_fwaddr);
1228

    
1229
  if (rt_tag)
1230
    bsprintf(str_tag, " tag %08x", rt_tag);
1231

    
1232
  cli_msg(-1016, "\t\t%s %I/%d metric%s %u%s%s",
1233
          (he->lsa.type == LSA_T_NSSA) ? "nssa-ext" : "external",
1234
          ip, pxlen, ebit ? "2" : "", rt_metric, str_via, str_tag);
1235
}
1236

    
1237
#ifdef OSPFv3
1238
static inline void
1239
show_lsa_prefix(struct top_hash_entry *he, struct ospf_lsa_header *cnode)
1240
{
1241
  struct ospf_lsa_prefix *px = he->lsa_body;
1242
  ip_addr pxa;
1243
  int pxlen;
1244
  u8 pxopts;
1245
  u16 metric;
1246
  u32 *buf;
1247
  int i;
1248

    
1249
  /* We check whether given prefix-LSA is related to the current node */
1250
  if ((px->ref_type != cnode->type) || (px->ref_rt != cnode->rt))
1251
    return;
1252

    
1253
  if ((px->ref_type == LSA_T_RT) && (px->ref_id != 0))
1254
    return;
1255

    
1256
  if ((px->ref_type == LSA_T_NET) && (px->ref_id != cnode->id))
1257
    return;
1258

    
1259
  buf = px->rest;
1260
  for (i = 0; i < px->pxcount; i++)
1261
    {
1262
      buf = lsa_get_ipv6_prefix(buf, &pxa, &pxlen, &pxopts, &metric);
1263

    
1264
      if (px->ref_type == LSA_T_RT)
1265
        cli_msg(-1016, "\t\tstubnet %I/%d metric %u", pxa, pxlen, metric);
1266
      else
1267
        cli_msg(-1016, "\t\taddress %I/%d", pxa, pxlen);
1268
    }
1269
}
1270
#endif
1271

    
1272
void
1273
ospf_sh_state(struct proto *p, int verbose, int reachable)
1274
{
1275
  struct proto_ospf *po = (struct proto_ospf *) p;
1276
  struct ospf_lsa_header *cnode = NULL;
1277
  int num = po->gr->hash_entries;
1278
  unsigned int i, ix, j1, j2, jx;
1279
  u32 last_area = 0xFFFFFFFF;
1280

    
1281
  if (p->proto_state != PS_UP)
1282
  {
1283
    cli_msg(-1016, "%s: is not up", p->name);
1284
    cli_msg(0, "");
1285
    return;
1286
  }
1287

    
1288
  /* We store interesting area-scoped LSAs in array hea and 
1289
     global-scoped (LSA_T_EXT) LSAs in array hex */
1290

    
1291
  struct top_hash_entry *hea[num];
1292
  struct top_hash_entry *hex[verbose ? num : 0];
1293
  struct top_hash_entry *he;
1294

    
1295
  j1 = j2 = jx = 0;
1296
  WALK_SLIST(he, po->lsal)
1297
  {
1298
    int accept;
1299

    
1300
    switch (he->lsa.type)
1301
      {
1302
      case LSA_T_RT:
1303
      case LSA_T_NET:
1304
        accept = 1;
1305
        break;
1306

    
1307
      case LSA_T_SUM_NET:
1308
      case LSA_T_SUM_RT:
1309
      case LSA_T_NSSA:
1310
#ifdef OSPFv3
1311
      case LSA_T_PREFIX:
1312
#endif
1313
        accept = verbose;
1314
        break;
1315

    
1316
      case LSA_T_EXT:
1317
        if (verbose)
1318
        {
1319
          he->domain = 1; /* Abuse domain field to mark the LSA */
1320
          hex[jx++] = he;
1321
        }
1322
      default:
1323
        accept = 0;
1324
      }
1325

    
1326
    if (accept)
1327
      hea[j1++] = he;
1328
    else
1329
      j2++;
1330
  }
1331

    
1332
  if ((j1 + j2) != num)
1333
    die("Fatal mismatch");
1334

    
1335
  qsort(hea, j1, sizeof(struct top_hash_entry *), lsa_compare_for_state);
1336
  qsort(hex, jx, sizeof(struct top_hash_entry *), ext_compare_for_state);
1337

    
1338
  /*
1339
   * This code is a bit tricky, we have a primary LSAs (router and
1340
   * network) that are presented as a node, and secondary LSAs that
1341
   * are presented as a part of a primary node. cnode represents an
1342
   * currently opened node (whose header was presented). The LSAs are
1343
   * sorted to get secondary LSAs just after related primary LSA (if
1344
   * available). We present secondary LSAs only when related primary
1345
   * LSA is opened.
1346
   *
1347
   * AS-external LSAs are stored separately as they might be presented
1348
   * several times (for each area when related ASBR is opened). When
1349
   * the node is closed, related external routes are presented. We
1350
   * also have to take into account that in OSPFv3, there might be
1351
   * more router-LSAs and only the first should be considered as a
1352
   * primary. This is handled by not closing old router-LSA when next
1353
   * one is processed (which is not opened because there is already
1354
   * one opened).
1355
   */
1356

    
1357
  ix = 0;
1358
  for (i = 0; i < j1; i++)
1359
  {
1360
    he = hea[i];
1361

    
1362
    /* If there is no opened node, we open the LSA (if appropriate) or skip to the next one */
1363
    if (!cnode)
1364
    {
1365
      if (((he->lsa.type == LSA_T_RT) || (he->lsa.type == LSA_T_NET))
1366
          && ((he->color == INSPF) || !reachable))
1367
      {
1368
        cnode = &(he->lsa);
1369

    
1370
        if (he->domain != last_area)
1371
        {
1372
          cli_msg(-1016, "");
1373
          cli_msg(-1016, "area %R", he->domain);
1374
          last_area = he->domain;
1375
          ix = 0;
1376
        }
1377
      }
1378
      else
1379
        continue;
1380
    }
1381

    
1382
    ASSERT(cnode && (he->domain == last_area) && (he->lsa.rt == cnode->rt));
1383

    
1384
    switch (he->lsa.type)
1385
    {
1386
      case LSA_T_RT:
1387
        show_lsa_router(po, he, he->lsa.id == cnode->id, verbose);
1388
        break;
1389

    
1390
      case LSA_T_NET:
1391
        show_lsa_network(he);
1392
        break;
1393

    
1394
      case LSA_T_SUM_NET:
1395
        if (cnode->type == LSA_T_RT)
1396
          show_lsa_sum_net(he);
1397
        break;
1398

    
1399
      case LSA_T_SUM_RT:
1400
        if (cnode->type == LSA_T_RT)
1401
          show_lsa_sum_rt(he);
1402
        break;
1403

    
1404
#ifdef OSPFv3
1405
      case LSA_T_PREFIX:
1406
        show_lsa_prefix(he, cnode);
1407
        break;
1408
#endif
1409

    
1410
      case LSA_T_EXT:
1411
      case LSA_T_NSSA:
1412
        show_lsa_external(he);
1413
        break;
1414
    }
1415

    
1416
    /* In these cases, we close the current node */
1417
    if ((i+1 == j1)
1418
        || (hea[i+1]->domain != last_area)
1419
        || (hea[i+1]->lsa.rt != cnode->rt)
1420
        || (hea[i+1]->lsa.type == LSA_T_NET))
1421
    {
1422
      while ((ix < jx) && (hex[ix]->lsa.rt < cnode->rt))
1423
        ix++;
1424

    
1425
      while ((ix < jx) && (hex[ix]->lsa.rt == cnode->rt))
1426
        show_lsa_external(hex[ix++]);
1427

    
1428
      cnode = NULL;
1429
    }
1430
  }
1431

    
1432
  int hdr = 0;
1433
  u32 last_rt = 0xFFFFFFFF;
1434
  for (ix = 0; ix < jx; ix++)
1435
  {
1436
    he = hex[ix];
1437

    
1438
    /* If it is still marked, we show it now. */
1439
    if (he->domain)
1440
    {
1441
      he->domain = 0;
1442

    
1443
      if ((he->color != INSPF) && reachable)
1444
        continue;
1445

    
1446
      if (!hdr)
1447
      {
1448
        cli_msg(-1016, "");
1449
        cli_msg(-1016, "other ASBRs");
1450
        hdr = 1;
1451
      }
1452

    
1453
      if (he->lsa.rt != last_rt)
1454
      {
1455
        cli_msg(-1016, "");
1456
        cli_msg(-1016, "\trouter %R", he->lsa.rt);
1457
        last_rt = he->lsa.rt;
1458
      }
1459

    
1460
      show_lsa_external(he);
1461
    }
1462
  }
1463

    
1464
  cli_msg(0, "");
1465
}
1466

    
1467

    
1468
static int
1469
lsa_compare_for_lsadb(const void *p1, const void *p2)
1470
{
1471
  struct top_hash_entry * he1 = * (struct top_hash_entry **) p1;
1472
  struct top_hash_entry * he2 = * (struct top_hash_entry **) p2;
1473
  struct ospf_lsa_header *lsa1 = &(he1->lsa);
1474
  struct ospf_lsa_header *lsa2 = &(he2->lsa);
1475
  int sc1 = LSA_SCOPE(lsa1);
1476
  int sc2 = LSA_SCOPE(lsa2);
1477

    
1478
  if (sc1 != sc2)
1479
    return sc2 - sc1;
1480

    
1481
  if (he1->domain != he2->domain)
1482
    return he1->domain - he2->domain;
1483

    
1484
  if (lsa1->rt != lsa2->rt)
1485
    return lsa1->rt - lsa2->rt;
1486
  
1487
  if (lsa1->id != lsa2->id)
1488
    return lsa1->id - lsa2->id;
1489

    
1490
  if (lsa1->type != lsa2->type)
1491
    return lsa1->type - lsa2->type;
1492

    
1493
  return lsa1->sn - lsa2->sn;
1494
}
1495

    
1496
void
1497
ospf_sh_lsadb(struct lsadb_show_data *ld)
1498
{
1499
  struct proto *p = proto_get_named(ld->name, &proto_ospf);
1500
  struct proto_ospf *po = (struct proto_ospf *) p;
1501
  int num = po->gr->hash_entries;
1502
  unsigned int i, j;
1503
  int last_dscope = -1;
1504
  u32 last_domain = 0;
1505

    
1506
  if (p->proto_state != PS_UP)
1507
  {
1508
    cli_msg(-1017, "%s: is not up", p->name);
1509
    cli_msg(0, "");
1510
    return;
1511
  }
1512

    
1513
  if (ld->router == SH_ROUTER_SELF)
1514
    ld->router = po->router_id;
1515

    
1516
  struct top_hash_entry *hea[num];
1517
  struct top_hash_entry *he;
1518

    
1519
  j = 0;
1520
  WALK_SLIST(he, po->lsal)
1521
    hea[j++] = he;
1522

    
1523
  if (j != num)
1524
    die("Fatal mismatch");
1525

    
1526
  qsort(hea, j, sizeof(struct top_hash_entry *), lsa_compare_for_lsadb);
1527

    
1528
  for (i = 0; i < j; i++)
1529
  {
1530
    struct ospf_lsa_header *lsa = &(hea[i]->lsa);
1531
    int dscope = LSA_SCOPE(lsa);
1532

    
1533
    if (ld->scope && (dscope != (ld->scope & 0xf000)))
1534
      continue;
1535

    
1536
    if ((ld->scope == LSA_SCOPE_AREA) && (hea[i]->domain != ld->area))
1537
      continue;
1538

    
1539
    /* Ignore high nibble */
1540
    if (ld->type && ((lsa->type & 0x0fff) != (ld->type & 0x0fff)))
1541
      continue;
1542

    
1543
    if (ld->lsid && (lsa->id != ld->lsid))
1544
      continue;
1545

    
1546
    if (ld->router && (lsa->rt != ld->router))
1547
      continue;
1548
    
1549
    if ((dscope != last_dscope) || (hea[i]->domain != last_domain))
1550
    {
1551
      cli_msg(-1017, "");
1552
      switch (dscope)
1553
      {
1554
        case LSA_SCOPE_AS:
1555
          cli_msg(-1017, "Global");
1556
          break;
1557
        case LSA_SCOPE_AREA:
1558
          cli_msg(-1017, "Area %R", hea[i]->domain);
1559
          break;
1560
#ifdef OSPFv3
1561
        case LSA_SCOPE_LINK:
1562
          {
1563
            struct iface *ifa = if_find_by_index(hea[i]->domain);
1564
            cli_msg(-1017, "Link %s", (ifa != NULL) ? ifa->name : "?");
1565
          }
1566
          break;
1567
#endif
1568
      }
1569
      cli_msg(-1017, "");
1570
      cli_msg(-1017," Type   LS ID           Router           Age  Sequence  Checksum");
1571

    
1572
      last_dscope = dscope;
1573
      last_domain = hea[i]->domain;
1574
    }
1575

    
1576

    
1577
    cli_msg(-1017," %04x  %-15R %-15R %5u  %08x    %04x",
1578
            lsa->type, lsa->id, lsa->rt, lsa->age, lsa->sn, lsa->checksum);
1579
  }
1580
  cli_msg(0, "");
1581
}
1582

    
1583

    
1584
struct protocol proto_ospf = {
1585
  name:                        "OSPF",
1586
  template:                "ospf%d",
1587
  attr_class:                EAP_OSPF,
1588
  preference:                DEF_PREF_OSPF,
1589
  init:                        ospf_init,
1590
  dump:                        ospf_dump,
1591
  start:                ospf_start,
1592
  shutdown:                ospf_shutdown,
1593
  reconfigure:                ospf_reconfigure,
1594
  get_status:                ospf_get_status,
1595
  get_attr:                ospf_get_attr,
1596
  get_route_info:        ospf_get_route_info
1597
  // show_proto_info:        ospf_sh
1598
};