Statistics
| Branch: | Revision:

iof-bird-daemon / proto / ospf / ospf.c @ 48e5f32d

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

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

    
260
  if (c->abr)
261
    ospf_open_vlink_sk(po);
262

    
263
  /* Add all virtual links */
264
  struct ospf_iface_patt *ic;
265
  WALK_LIST(ic, c->vlink_list)
266
    ospf_iface_new_vlink(po, ic);
267

    
268
  return PS_UP;
269
}
270

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

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

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

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

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

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

    
316
  return p;
317
}
318

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
415
  if (po->calcrt)
416
    return;
417

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

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

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

    
430
  po->calcrt = 2;
431

    
432
  return 1;
433
}
434

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

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

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

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

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

    
478
  WALK_LIST(oa, po->area_list)
479
    area_disp(oa);
480

    
481
  /* Age LSA DB */
482
  ospf_age(po);
483

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

    
489

    
490

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

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

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

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

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

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

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

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

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

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

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

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

    
566
  return PS_DOWN;
567
}
568

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

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

    
582
    flush_ext_lsa(oa, fn, oa_is_nssa(oa));
583

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

    
588
    return;
589
  }
590

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

    
604
  originate_ext_lsa(oa, fn, EXT_EXPORT, metric, gw, tag, 1);
605
}
606

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

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

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

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

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

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

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

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

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

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

    
701
  ospf_ifaces_reconfigure(oa, nac);
702

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

    
708
  /* No need to handle stubnet_list */
709

    
710
  oa->marked = 0;
711
  schedule_rt_lsa(oa);
712
}
713

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

    
735
  if (proto_get_router_id(c) != po->router_id)
736
    return 0;
737

    
738
  if (po->rfc1583 != new->rfc1583)
739
    return 0;
740

    
741
  if (old->abr != new->abr)
742
    return 0;
743

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

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

    
754
  WALK_LIST(ifa, po->iface_list)
755
    ifa->marked = 1;
756

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

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

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

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

    
789
  schedule_rtcalc(po);
790
  
791
  return 1;
792
}
793

    
794

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

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

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

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

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

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

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

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

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

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

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

    
903
  }
904
  cli_msg(0, "");
905
}
906

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

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

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

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

    
940
#ifdef OSPFv3
941

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

    
952
  return dst;
953
}
954

    
955
#endif
956

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

    
965
  if (he1->domain < he2->domain)
966
    return -1;
967
  if (he1->domain > he2->domain)
968
    return 1;
969

    
970
#ifdef OSPFv3
971
  struct ospf_lsa_header lsatmp1, lsatmp2;
972

    
973
  int px1 = (lsa1->type == LSA_T_PREFIX);
974
  int px2 = (lsa2->type == LSA_T_PREFIX);
975

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

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

    
983
  int nt1 = (lsa1->type == LSA_T_NET);
984
  int nt2 = (lsa2->type == LSA_T_NET);
985

    
986
  if (nt1 != nt2)
987
    return nt1 - nt2;
988

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

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

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

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

    
1020
    if (lsa1->type < lsa2->type)
1021
      return -1;
1022
    if (lsa1->type > lsa2->type)
1023
      return 1;
1024

    
1025
    if (lsa1->id < lsa2->id)
1026
      return -1;
1027
    if (lsa1->id > lsa2->id)
1028
      return 1;
1029

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

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

    
1047
  if (lsa1->rt < lsa2->rt)
1048
    return -1;
1049
  if (lsa1->rt > lsa2->rt)
1050
    return 1;
1051

    
1052
  if (lsa1->id < lsa2->id)
1053
    return -1;
1054
  if (lsa1->id > lsa2->id)
1055
    return 1;
1056

    
1057
  return lsa1->sn - lsa2->sn;
1058
}
1059

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

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

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

    
1085

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

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

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

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

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

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

    
1117
#ifdef OSPFv2
1118
  if (!verbose)
1119
    return;
1120

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

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

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

    
1144
  show_lsa_distance(he);
1145

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

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

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

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

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

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

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

    
1188

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

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

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

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

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

    
1231
  if (rt_tag)
1232
    bsprintf(str_tag, " tag %08x", rt_tag);
1233

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1334
  if ((j1 + j2) != num)
1335
    die("Fatal mismatch");
1336

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

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

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

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

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

    
1384
    ASSERT(cnode && (he->domain == last_area) && (he->lsa.rt == cnode->rt));
1385

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

    
1392
      case LSA_T_NET:
1393
        show_lsa_network(he);
1394
        break;
1395

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

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

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

    
1412
      case LSA_T_EXT:
1413
      case LSA_T_NSSA:
1414
        show_lsa_external(he);
1415
        break;
1416
    }
1417

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

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

    
1430
      cnode = NULL;
1431
    }
1432
  }
1433

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

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

    
1445
      if ((he->color != INSPF) && reachable)
1446
        continue;
1447

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

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

    
1462
      show_lsa_external(he);
1463
    }
1464
  }
1465

    
1466
  cli_msg(0, "");
1467
}
1468

    
1469

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

    
1480
  if (sc1 != sc2)
1481
    return sc2 - sc1;
1482

    
1483
  if (he1->domain != he2->domain)
1484
    return he1->domain - he2->domain;
1485

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

    
1492
  if (lsa1->type != lsa2->type)
1493
    return lsa1->type - lsa2->type;
1494

    
1495
  return lsa1->sn - lsa2->sn;
1496
}
1497

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

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

    
1515
  if (ld->router == SH_ROUTER_SELF)
1516
    ld->router = po->router_id;
1517

    
1518
  struct top_hash_entry *hea[num];
1519
  struct top_hash_entry *he;
1520

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

    
1525
  if (j != num)
1526
    die("Fatal mismatch");
1527

    
1528
  qsort(hea, j, sizeof(struct top_hash_entry *), lsa_compare_for_lsadb);
1529

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

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

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

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

    
1545
    if (ld->lsid && (lsa->id != ld->lsid))
1546
      continue;
1547

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

    
1574
      last_dscope = dscope;
1575
      last_domain = hea[i]->domain;
1576
    }
1577

    
1578

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

    
1585

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