Statistics
| Branch: | Revision:

iof-bird-daemon / proto / ospf / ospf.c @ 145368f5

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->merge_external = c->merge_external;
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
  if (c->abr)
262
    ospf_open_vlink_sk(po);
263

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

    
269
  return PS_UP;
270
}
271

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

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

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

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

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

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

    
317
  return p;
318
}
319

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
431
  po->calcrt = 2;
432

    
433
  return 1;
434
}
435

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

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

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

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

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

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

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

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

    
490

    
491

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

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

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

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

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

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

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

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

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

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

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

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

    
567
  return PS_DOWN;
568
}
569

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

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

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

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

    
589
    return;
590
  }
591

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

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

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

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

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

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

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

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

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

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

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

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

    
702
  ospf_ifaces_reconfigure(oa, nac);
703

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

    
709
  /* No need to handle stubnet_list */
710

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

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

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

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

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

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

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

    
756
  WALK_LIST(ifa, po->iface_list)
757
    ifa->marked = 1;
758

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

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

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

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

    
791
  schedule_rtcalc(po);
792
  
793
  return 1;
794
}
795

    
796

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

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

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

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

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

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

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

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

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

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

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

    
905
  }
906
  cli_msg(0, "");
907
}
908

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

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

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

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

    
942
#ifdef OSPFv3
943

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

    
954
  return dst;
955
}
956

    
957
#endif
958

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

    
967
  if (he1->domain < he2->domain)
968
    return -1;
969
  if (he1->domain > he2->domain)
970
    return 1;
971

    
972
#ifdef OSPFv3
973
  struct ospf_lsa_header lsatmp1, lsatmp2;
974

    
975
  int px1 = (lsa1->type == LSA_T_PREFIX);
976
  int px2 = (lsa2->type == LSA_T_PREFIX);
977

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

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

    
985
  int nt1 = (lsa1->type == LSA_T_NET);
986
  int nt2 = (lsa2->type == LSA_T_NET);
987

    
988
  if (nt1 != nt2)
989
    return nt1 - nt2;
990

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

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

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

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

    
1022
    if (lsa1->type < lsa2->type)
1023
      return -1;
1024
    if (lsa1->type > lsa2->type)
1025
      return 1;
1026

    
1027
    if (lsa1->id < lsa2->id)
1028
      return -1;
1029
    if (lsa1->id > lsa2->id)
1030
      return 1;
1031

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

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

    
1049
  if (lsa1->rt < lsa2->rt)
1050
    return -1;
1051
  if (lsa1->rt > lsa2->rt)
1052
    return 1;
1053

    
1054
  if (lsa1->id < lsa2->id)
1055
    return -1;
1056
  if (lsa1->id > lsa2->id)
1057
    return 1;
1058

    
1059
  return lsa1->sn - lsa2->sn;
1060
}
1061

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

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

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

    
1087

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

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

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

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

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

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

    
1119
#ifdef OSPFv2
1120
  if (!verbose)
1121
    return;
1122

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

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

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

    
1146
  show_lsa_distance(he);
1147

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

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

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

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

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

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

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

    
1190

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

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

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

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

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

    
1233
  if (rt_tag)
1234
    bsprintf(str_tag, " tag %08x", rt_tag);
1235

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

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

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

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

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

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

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

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

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

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

    
1294
  int num = po->gr->hash_entries;
1295
  struct top_hash_entry *hea[num];
1296
  struct top_hash_entry *hex[verbose ? num : 0];
1297
  struct top_hash_entry *he;
1298

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

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

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

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

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

    
1336
  if ((j1 + j2) != num)
1337
    die("Fatal mismatch");
1338

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

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

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

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

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

    
1386
    ASSERT(cnode && (he->domain == last_area) && (he->lsa.rt == cnode->rt));
1387

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

    
1394
      case LSA_T_NET:
1395
        show_lsa_network(he);
1396
        break;
1397

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

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

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

    
1414
      case LSA_T_EXT:
1415
      case LSA_T_NSSA:
1416
        show_lsa_external(he);
1417
        break;
1418
    }
1419

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

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

    
1432
      cnode = NULL;
1433
    }
1434
  }
1435

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

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

    
1447
      if ((he->color != INSPF) && reachable)
1448
        continue;
1449

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

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

    
1464
      show_lsa_external(he);
1465
    }
1466
  }
1467

    
1468
  cli_msg(0, "");
1469
}
1470

    
1471

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

    
1482
  if (sc1 != sc2)
1483
    return sc2 - sc1;
1484

    
1485
  if (he1->domain != he2->domain)
1486
    return he1->domain - he2->domain;
1487

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

    
1494
  if (lsa1->type != lsa2->type)
1495
    return lsa1->type - lsa2->type;
1496

    
1497
  return lsa1->sn - lsa2->sn;
1498
}
1499

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

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

    
1517
  if (ld->router == SH_ROUTER_SELF)
1518
    ld->router = po->router_id;
1519

    
1520
  struct top_hash_entry *hea[num];
1521
  struct top_hash_entry *he;
1522

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

    
1527
  if (j != num)
1528
    die("Fatal mismatch");
1529

    
1530
  qsort(hea, j, sizeof(struct top_hash_entry *), lsa_compare_for_lsadb);
1531

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

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

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

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

    
1547
    if (ld->lsid && (lsa->id != ld->lsid))
1548
      continue;
1549

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

    
1576
      last_dscope = dscope;
1577
      last_domain = hea[i]->domain;
1578
    }
1579

    
1580

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

    
1587

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