Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (39.4 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
 * The heart beat of ospf is ospf_disp(). It is called at regular intervals
46
 * (&proto_ospf->tick). It is responsible for aging and flushing of LSAs in
47
 * the database, for routing table calculaction and it call area_disp() of every
48
 * ospf_area.
49
 * 
50
 * The function area_disp() is
51
 * responsible for late originating of router LSA and network LSA
52
 * and for cleanup after routing table calculation process in
53
 * the area.
54
 * To every &ospf_iface, we connect one or more
55
 * &ospf_neighbor's -- a structure containing many timers and queues
56
 * for building adjacency and for exchange of routing messages.
57
 *
58
 * BIRD's OSPF implementation respects RFC2328 in every detail, but
59
 * some of internal algorithms do differ. The RFC recommends making a snapshot
60
 * of the link-state database when a new adjacency is forming and sending
61
 * the database description packets based on the information in this 
62
 * snapshot. The database can be quite large in some networks, so
63
 * rather we walk through a &slist structure which allows us to
64
 * continue even if the actual LSA we were working with is deleted. New
65
 * LSAs are added at the tail of this &slist.
66
 *
67
 * We also don't keep a separate OSPF routing table, because the core
68
 * helps us by being able to recognize when a route is updated
69
 * to an identical one and it suppresses the update automatically.
70
 * Due to this, we can flush all the routes we've recalculated and
71
 * also those we've deleted to the core's routing table and the
72
 * core will take care of the rest. This simplifies the process
73
 * and conserves memory.
74
 */
75

    
76
#include <stdlib.h>
77
#include "ospf.h"
78

    
79

    
80
static int ospf_reload_routes(struct proto *p);
81
static void ospf_rt_notify(struct proto *p, struct rtable *table UNUSED, net * n, rte * new, rte * old UNUSED, ea_list * attrs);
82
static void ospf_ifa_notify(struct proto *p, unsigned flags, struct ifa *a);
83
static int ospf_rte_better(struct rte *new, struct rte *old);
84
static int ospf_rte_same(struct rte *new, struct rte *old);
85
static void ospf_disp(timer *timer);
86

    
87
static void
88
ospf_area_initfib(struct fib_node *fn)
89
{
90
  struct area_net *an = (struct area_net *) fn;
91
  an->hidden = 0;
92
  an->active = -1;        /* Force to regenerate summary lsa */
93
        /* ac->oldactive will be rewritten by ospf_rt_spf() */
94
}
95

    
96
static void
97
add_area_nets(struct ospf_area *oa, struct ospf_area_config *ac)
98
{
99
    struct proto_ospf *po = oa->po;
100
    struct proto *p = &po->proto;
101
    struct area_net_config *anet;
102
    struct area_net *antmp;
103

    
104
    fib_init(&oa->net_fib, p->pool, sizeof(struct area_net), 16, ospf_area_initfib);
105

    
106
    WALK_LIST(anet, ac->net_list)
107
    {
108
      antmp = (struct area_net *) fib_get(&oa->net_fib, &anet->px.addr, anet->px.len);
109
      antmp->hidden = anet->hidden;
110
    }
111
}
112

    
113
static int
114
ospf_start(struct proto *p)
115
{
116
  struct proto_ospf *po = (struct proto_ospf *) p;
117
  struct ospf_config *c = (struct ospf_config *) (p->cf);
118
  struct ospf_area_config *ac;
119
  struct ospf_area *oa;
120

    
121
  po->router_id = proto_get_router_id(p->cf);
122
  po->rfc1583 = c->rfc1583;
123
  po->ebit = 0;
124
  po->tick = c->tick;
125
  po->disp_timer = tm_new(p->pool);
126
  po->disp_timer->data = po;
127
  po->disp_timer->randomize = 0;
128
  po->disp_timer->hook = ospf_disp;
129
  po->disp_timer->recurrent = po->tick;
130
  tm_start(po->disp_timer, 1);
131
  po->lsab_size = 256;
132
  po->lsab_used = 0;
133
  po->lsab = mb_alloc(p->pool, po->lsab_size);
134
  init_list(&(po->iface_list));
135
  init_list(&(po->area_list));
136
  fib_init(&po->rtf, p->pool, sizeof(ort), 16, ospf_rt_initort);
137
  po->areano = 0;
138
  po->gr = ospf_top_new(p->pool);
139
  po->cleanup = 1;
140
  s_init_list(&(po->lsal));
141
  if (EMPTY_LIST(c->area_list))
142
  {
143
    log(L_ERR "Cannot start, no OSPF areas configured!");
144
    return PS_DOWN;
145
  }
146

    
147
  WALK_LIST(ac, c->area_list)
148
  {
149
    oa = mb_allocz(p->pool, sizeof(struct ospf_area));
150
    add_tail(&po->area_list, NODE oa);
151
    po->areano++;
152
    oa->ac = ac;
153
    oa->stub = ac->stub;
154
    oa->areaid = ac->areaid;
155
    oa->rt = NULL;
156
    oa->po = po;
157
    add_area_nets(oa, ac);
158
    fib_init(&oa->rtr, p->pool, sizeof(ort), 16, ospf_rt_initort);
159

    
160
    if (oa->areaid == 0)
161
    {
162
      po->backbone = oa;
163
      if (oa->stub) log(L_ERR "Backbone cannot be stub. Ignoring!");
164
      oa->stub = 0;
165
    }
166

    
167
#ifdef OSPFv2
168
    oa->options = (oa->stub ? 0 : OPT_E);
169
#else /* OSPFv3 */
170
    oa->options = OPT_R | (oa->stub ? 0 : OPT_E) | OPT_V6;
171
#endif
172
  }
173

    
174
  /* Add all virtual links as interfaces */
175
  {
176
    struct ospf_iface_patt *ipatt;
177
    WALK_LIST(ac, c->area_list)
178
    {
179
      WALK_LIST(ipatt, ac->vlink_list)
180
      {
181
        if(!po->backbone)
182
        {
183
          oa = mb_allocz(p->pool, sizeof(struct ospf_area));
184
          add_tail(&po->area_list, NODE oa);
185
          po->areano++;
186
          oa->stub = 0;
187
          oa->areaid = 0;
188
          oa->rt = NULL;
189
          oa->po = po;
190
          fib_init(&oa->net_fib, p->pool, sizeof(struct area_net), 16, ospf_area_initfib);
191
          fib_init(&oa->rtr, p->pool, sizeof(ort), 16, ospf_rt_initort);
192
          po->backbone = oa;
193
#ifdef OSPFv2
194
          oa->options = OPT_E;
195
#else /* OSPFv3 */
196
          oa->options = OPT_R | OPT_E | OPT_V6;
197
#endif
198
        }
199
        ospf_iface_new(po, NULL, ac, ipatt);
200
      }
201
    }
202
  }
203
  return PS_UP;
204
}
205

    
206
static void
207
ospf_dump(struct proto *p)
208
{
209
  struct ospf_iface *ifa;
210
  struct ospf_neighbor *n;
211
  struct proto_ospf *po = (struct proto_ospf *) p;
212

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

    
215
  WALK_LIST(ifa, po->iface_list)
216
  {
217
    OSPF_TRACE(D_EVENTS, "Interface: %s", (ifa->iface ? ifa->iface->name : "(null)"));
218
    OSPF_TRACE(D_EVENTS, "state: %u", ifa->state);
219
    OSPF_TRACE(D_EVENTS, "DR:  %R", ifa->drid);
220
    OSPF_TRACE(D_EVENTS, "BDR: %R", ifa->bdrid);
221
    WALK_LIST(n, ifa->neigh_list)
222
    {
223
      OSPF_TRACE(D_EVENTS, "  neighbor %R in state %u", n->rid, n->state);
224
    }
225
  }
226

    
227
  /*
228
  OSPF_TRACE(D_EVENTS, "LSA graph dump start:");
229
  ospf_top_dump(po->gr, p);
230
  OSPF_TRACE(D_EVENTS, "LSA graph dump finished");
231
  */
232
  neigh_dump_all();
233
}
234

    
235
static struct proto *
236
ospf_init(struct proto_config *c)
237
{
238
  struct proto *p = proto_new(c, sizeof(struct proto_ospf));
239

    
240
  p->make_tmp_attrs = ospf_make_tmp_attrs;
241
  p->store_tmp_attrs = ospf_store_tmp_attrs;
242
  p->import_control = ospf_import_control;
243
  p->reload_routes = ospf_reload_routes;
244
  p->accept_ra_types = RA_OPTIMAL;
245
  p->rt_notify = ospf_rt_notify;
246
  p->if_notify = ospf_iface_notify;
247
  p->ifa_notify = ospf_ifa_notify;
248
  p->rte_better = ospf_rte_better;
249
  p->rte_same = ospf_rte_same;
250

    
251
  return p;
252
}
253

    
254
/* If new is better return 1 */
255
static int
256
ospf_rte_better(struct rte *new, struct rte *old)
257
{
258
  if (new->u.ospf.metric1 == LSINFINITY)
259
    return 0;
260

    
261
  if(new->attrs->source < old->attrs->source) return 1;
262
  if(new->attrs->source > old->attrs->source) return 0;
263

    
264
  if(new->attrs->source == RTS_OSPF_EXT2)
265
  {
266
    if(new->u.ospf.metric2 < old->u.ospf.metric2) return 1;
267
    if(new->u.ospf.metric2 > old->u.ospf.metric2) return 0;
268
  }
269

    
270
  if (new->u.ospf.metric1 < old->u.ospf.metric1)
271
    return 1;
272

    
273
  return 0;                        /* Old is shorter or same */
274
}
275

    
276
static int
277
ospf_rte_same(struct rte *new, struct rte *old)
278
{
279
  /* new->attrs == old->attrs always */
280
  return
281
    new->u.ospf.metric1 == old->u.ospf.metric1 &&
282
    new->u.ospf.metric2 == old->u.ospf.metric2 &&
283
    new->u.ospf.tag == old->u.ospf.tag &&
284
    new->u.ospf.router_id == old->u.ospf.router_id;
285
}
286

    
287
static ea_list *
288
ospf_build_attrs(ea_list * next, struct linpool *pool, u32 m1, u32 m2,
289
                 u32 tag, u32 rid)
290
{
291
  struct ea_list *l =
292
    lp_alloc(pool, sizeof(struct ea_list) + 4 * sizeof(eattr));
293

    
294
  l->next = next;
295
  l->flags = EALF_SORTED;
296
  l->count = 4;
297
  l->attrs[0].id = EA_OSPF_METRIC1;
298
  l->attrs[0].flags = 0;
299
  l->attrs[0].type = EAF_TYPE_INT | EAF_TEMP;
300
  l->attrs[0].u.data = m1;
301
  l->attrs[1].id = EA_OSPF_METRIC2;
302
  l->attrs[1].flags = 0;
303
  l->attrs[1].type = EAF_TYPE_INT | EAF_TEMP;
304
  l->attrs[1].u.data = m2;
305
  l->attrs[2].id = EA_OSPF_TAG;
306
  l->attrs[2].flags = 0;
307
  l->attrs[2].type = EAF_TYPE_INT | EAF_TEMP;
308
  l->attrs[2].u.data = tag;
309
  l->attrs[3].id = EA_OSPF_ROUTER_ID;
310
  l->attrs[3].flags = 0;
311
  l->attrs[3].type = EAF_TYPE_INT | EAF_TEMP;
312
  l->attrs[3].u.data = rid;
313
  return l;
314
}
315

    
316
void
317
schedule_net_lsa(struct ospf_iface *ifa)
318
{
319
  struct proto *p = &ifa->oa->po->proto;
320

    
321
  OSPF_TRACE(D_EVENTS, "Scheduling network-LSA origination for iface %s", ifa->iface->name);
322
  ifa->orignet = 1;
323
}
324

    
325
#ifdef OSPFv3
326
void
327
schedule_link_lsa(struct ospf_iface *ifa)
328
{
329
  struct proto *p = &ifa->oa->po->proto;
330

    
331
  OSPF_TRACE(D_EVENTS, "Scheduling link-LSA origination for iface %s", ifa->iface->name);
332
  ifa->origlink = 1;
333
}
334
#endif
335

    
336
void
337
schedule_rt_lsa(struct ospf_area *oa)
338
{
339
  struct proto *p = &oa->po->proto;
340

    
341
  OSPF_TRACE(D_EVENTS, "Scheduling router-LSA origination for area %R", oa->areaid);
342
  oa->origrt = 1;
343
}
344

    
345
void
346
schedule_rtcalc(struct proto_ospf *po)
347
{
348
  struct proto *p = &po->proto;
349

    
350
  if (po->calcrt)
351
    return;
352

    
353
  OSPF_TRACE(D_EVENTS, "Scheduling routing table calculation");
354
  po->calcrt = 1;
355
}
356

    
357
static int
358
ospf_reload_routes(struct proto *p)
359
{
360
  struct proto_ospf *po = (struct proto_ospf *) p;
361

    
362
  if (po->calcrt != 2)
363
    OSPF_TRACE(D_EVENTS, "Scheduling routing table calculation with route reload");
364

    
365
  po->calcrt = 2;
366

    
367
  return 1;
368
}
369

    
370
/**
371
 * area_disp - invokes origination of
372
 * router LSA and routing table cleanup
373
 * @oa: ospf area
374
 *
375
 * It invokes aging and when @ospf_area->origrt is set to 1, start
376
 * function for origination of router, network LSAs.
377
 */
378
void
379
area_disp(struct ospf_area *oa)
380
{
381
  struct proto_ospf *po = oa->po;
382
  struct ospf_iface *ifa;
383

    
384
  /* Now try to originage rt_lsa */
385
  if (oa->origrt)
386
    update_rt_lsa(oa);
387

    
388
  /* Now try to originate network LSA's */
389
  WALK_LIST(ifa, po->iface_list)
390
  {
391
#ifdef OSPFv3
392
    /* Link LSA should be originated before Network LSA */
393
    if (ifa->origlink && (ifa->oa == oa))
394
      update_link_lsa(ifa);
395
#endif
396

    
397
    if (ifa->orignet && (ifa->oa == oa))
398
      update_net_lsa(ifa);
399
  }
400
}
401

    
402
/**
403
 * ospf_disp - invokes routing table calctulation, aging and also area_disp()
404
 * @timer: timer usually called every @proto_ospf->tick second, @timer->data
405
 * point to @proto_ospf
406
 */
407
void
408
ospf_disp(timer * timer)
409
{
410
  struct proto_ospf *po = timer->data;
411
  struct ospf_area *oa;
412

    
413
  WALK_LIST(oa, po->area_list)
414
    area_disp(oa);
415

    
416
  /* Age LSA DB */
417
  ospf_age(po);
418

    
419
  /* Calculate routing table */
420
  if (po->calcrt)
421
    ospf_rt_spf(po);
422
}
423

    
424

    
425

    
426
/**
427
 * ospf_import_control - accept or reject new route from nest's routing table
428
 * @p: current instance of protocol
429
 * @new: the new route
430
 * @attrs: list of attributes
431
 * @pool: pool for allocation of attributes
432
 *
433
 * Its quite simple. It does not accept our own routes and leaves the decision on
434
 * import to the filters.
435
 */
436

    
437
int
438
ospf_import_control(struct proto *p, rte ** new, ea_list ** attrs,
439
                    struct linpool *pool)
440
{
441
  rte *e = *new;
442

    
443
  if (p == e->attrs->proto)
444
    return -1;                        /* Reject our own routes */
445
  *attrs = ospf_build_attrs(*attrs, pool, LSINFINITY, 10000, 0, 0);
446
  return 0;                        /* Leave decision to the filters */
447
}
448

    
449
struct ea_list *
450
ospf_make_tmp_attrs(struct rte *rt, struct linpool *pool)
451
{
452
  return ospf_build_attrs(NULL, pool, rt->u.ospf.metric1, rt->u.ospf.metric2,
453
                          rt->u.ospf.tag, rt->u.ospf.router_id);
454
}
455

    
456
void
457
ospf_store_tmp_attrs(struct rte *rt, struct ea_list *attrs)
458
{
459
  rt->u.ospf.metric1 = ea_get_int(attrs, EA_OSPF_METRIC1, LSINFINITY);
460
  rt->u.ospf.metric2 = ea_get_int(attrs, EA_OSPF_METRIC2, 10000);
461
  rt->u.ospf.tag = ea_get_int(attrs, EA_OSPF_TAG, 0);
462
  rt->u.ospf.router_id = ea_get_int(attrs, EA_OSPF_ROUTER_ID, 0);
463
}
464

    
465
/**
466
 * ospf_shutdown - Finish of OSPF instance
467
 * @p: current instance of protocol
468
 *
469
 * RFC does not define any action that should be taken before router
470
 * shutdown. To make my neighbors react as fast as possible, I send
471
 * them hello packet with empty neighbor list. They should start
472
 * their neighbor state machine with event %NEIGHBOR_1WAY.
473
 */
474

    
475
static int
476
ospf_shutdown(struct proto *p)
477
{
478
  struct proto_ospf *po = (struct proto_ospf *) p;
479
  struct ospf_iface *ifa;
480
  OSPF_TRACE(D_EVENTS, "Shutdown requested");
481

    
482
  /* And send to all my neighbors 1WAY */
483
  WALK_LIST(ifa, po->iface_list) ospf_iface_shutdown(ifa);
484

    
485
  return PS_DOWN;
486
}
487

    
488
static void
489
ospf_rt_notify(struct proto *p, rtable *tbl UNUSED, net * n, rte * new, rte * old UNUSED, ea_list * attrs)
490
{
491
  struct proto_ospf *po = (struct proto_ospf *) p;
492

    
493
/* Temporarily down write anything
494
  OSPF_TRACE(D_EVENTS, "Got route %I/%d %s", p->name, n->n.prefix,
495
    n->n.pxlen, new ? "up" : "down");
496
*/
497

    
498
  if (new)                        /* Got some new route */
499
    originate_ext_lsa(n, new, po, attrs);
500
  else
501
    flush_ext_lsa(n, po);
502
}
503

    
504
static void
505
ospf_ifa_notify(struct proto *p, unsigned flags UNUSED, struct ifa *a)
506
{
507
  struct proto_ospf *po = (struct proto_ospf *) p;
508
  struct ospf_iface *ifa;
509
  
510
  if ((a->flags & IA_SECONDARY) || (a->flags & IA_UNNUMBERED))
511
    return;
512

    
513
  WALK_LIST(ifa, po->iface_list)
514
    {
515
      if (ifa->iface == a->iface)
516
        {
517
          schedule_rt_lsa(ifa->oa);
518
          /* Event 5 from RFC5340 4.4.3. */
519
          schedule_link_lsa(ifa);
520
          return;
521
        }
522
    }
523
}
524

    
525
static void
526
ospf_get_status(struct proto *p, byte * buf)
527
{
528
  struct proto_ospf *po = (struct proto_ospf *) p;
529

    
530
  if (p->proto_state == PS_DOWN)
531
    buf[0] = 0;
532
  else
533
  {
534
    struct ospf_iface *ifa;
535
    struct ospf_neighbor *n;
536
    int adj = 0;
537

    
538
    WALK_LIST(ifa, po->iface_list)
539
      WALK_LIST(n, ifa->neigh_list) if (n->state == NEIGHBOR_FULL)
540
      adj = 1;
541

    
542
    if (adj == 0)
543
      strcpy(buf, "Alone");
544
    else
545
      strcpy(buf, "Running");
546
  }
547
}
548

    
549
static void
550
ospf_get_route_info(rte * rte, byte * buf, ea_list * attrs UNUSED)
551
{
552
  char *type = "<bug>";
553

    
554
  switch(rte->attrs->source)
555
  {
556
    case RTS_OSPF:
557
      type = "I";
558
      break;
559
    case RTS_OSPF_IA:
560
      type = "IA";
561
      break;
562
    case RTS_OSPF_EXT1:
563
      type = "E1";
564
      break;
565
    case RTS_OSPF_EXT2:
566
      type = "E2";
567
      break;
568
  }
569

    
570
  buf += bsprintf(buf, " %s", type);
571
  buf += bsprintf(buf, " (%d/%d", rte->pref, rte->u.ospf.metric1);
572
  if (rte->attrs->source == RTS_OSPF_EXT2)
573
    buf += bsprintf(buf, "/%d", rte->u.ospf.metric2);
574
  buf += bsprintf(buf, ")");
575
  if ((rte->attrs->source == RTS_OSPF_EXT2 || rte->attrs->source == RTS_OSPF_EXT1) && rte->u.ospf.tag)
576
  {
577
    buf += bsprintf(buf, " [%x]", rte->u.ospf.tag);
578
  }
579
  if (rte->u.ospf.router_id)
580
    buf += bsprintf(buf, " [%R]", rte->u.ospf.router_id);
581
}
582

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

    
605
static int
606
ospf_patt_compare(struct ospf_iface_patt *a, struct ospf_iface_patt *b)
607
{
608
  return (a->type == b->type);
609
}
610

    
611
/**
612
 * ospf_reconfigure - reconfiguration hook
613
 * @p: current instance of protocol (with old configuration)
614
 * @c: new configuration requested by user
615
 *
616
 * This hook tries to be a little bit intelligent. Instance of OSPF
617
 * will survive change of many constants like hello interval,
618
 * password change, addition or deletion of some neighbor on
619
 * nonbroadcast network, cost of interface, etc.
620
 */
621
static int
622
ospf_reconfigure(struct proto *p, struct proto_config *c)
623
{
624
  struct ospf_config *old = (struct ospf_config *) (p->cf);
625
  struct ospf_config *new = (struct ospf_config *) c;
626
  struct ospf_area_config *oldac, *newac;
627
  struct proto_ospf *po = (struct proto_ospf *) p;
628
  struct ospf_iface_patt *oldip, *newip;
629
  struct ospf_iface *ifa;
630
  struct nbma_node *nb1, *nb2, *nbnx;
631
  struct ospf_area *oa = NULL;
632
  int found, olddead, newdead;
633
  struct area_net_config *anc;
634
  struct area_net *an;
635

    
636
  if (po->rfc1583 != new->rfc1583)
637
    return 0;
638

    
639
  schedule_rtcalc(po);
640

    
641
  po->tick = new->tick;
642
  po->disp_timer->recurrent = po->tick;
643
  tm_start(po->disp_timer, 1);
644

    
645
  oldac = HEAD(old->area_list);
646
  newac = HEAD(new->area_list);
647

    
648
  /* I should get it in the same order */
649

    
650
  while (((NODE(oldac))->next != NULL) && ((NODE(newac))->next != NULL))
651
  {
652
    if (oldac->areaid != newac->areaid)
653
      return 0;
654

    
655
    WALK_LIST(oa, po->area_list)
656
      if (oa->areaid == newac->areaid)
657
        break;
658

    
659
    if (!oa)
660
      return 0;
661

    
662
    oa->ac = newac;
663
    oa->stub = newac->stub;
664
    if (newac->stub && (oa->areaid == 0)) oa->stub = 0;
665

    
666
    /* Check stubnet_list */
667
    struct ospf_stubnet_config *oldsn = HEAD(oldac->stubnet_list);
668
    struct ospf_stubnet_config *newsn = HEAD(newac->stubnet_list);
669

    
670
    while (((NODE(oldsn))->next != NULL) && ((NODE(newsn))->next != NULL))
671
      {
672
        if (!ipa_equal(oldsn->px.addr, newsn->px.addr) ||
673
            (oldsn->px.len != newsn->px.len) ||
674
            (oldsn->hidden != newsn->hidden) ||
675
            (oldsn->summary != newsn->summary) ||
676
            (oldsn->cost != newsn->cost))
677
          break;
678

    
679
        oldsn = (struct ospf_stubnet_config *)(NODE(oldsn))->next;
680
        newsn = (struct ospf_stubnet_config *)(NODE(newsn))->next;
681
      }
682

    
683
    /* If there is no change, both pointers should be NULL */
684
    if (((NODE(oldsn))->next) != ((NODE(newsn))->next))
685
      schedule_rt_lsa(oa);
686

    
687
    /* Change net_list */
688
    FIB_WALK(&oa->net_fib, nf)        /* First check if some networks are deleted */
689
    {
690
      found = 0;
691
      WALK_LIST(anc, newac->net_list)
692
      {
693
        if (ipa_equal(anc->px.addr, nf->prefix) && (anc->px.len == nf->pxlen))
694
        {
695
          found = 1;
696
          break;
697
        }
698
        if (!found) flush_sum_lsa(oa, nf, ORT_NET);        /* And flush them */
699
      }
700
    }
701
    FIB_WALK_END;
702

    
703
    WALK_LIST(anc, newac->net_list)        /* Second add new networks */
704
    {
705
      an = fib_get(&oa->net_fib, &anc->px.addr, anc->px.len);
706
      an->hidden = anc->hidden;
707
    }
708

    
709
    if (!iface_patts_equal(&oldac->patt_list, &newac->patt_list,
710
                           (void *) ospf_patt_compare))
711
      return 0;
712

    
713
    WALK_LIST(ifa, po->iface_list)
714
    {
715
      if (oldip = (struct ospf_iface_patt *)
716
          iface_patt_find(&oldac->patt_list, ifa->iface))
717
      {
718
        /* Now reconfigure interface */
719
        if (!(newip = (struct ospf_iface_patt *)
720
              iface_patt_find(&newac->patt_list, ifa->iface)))
721
          return 0;
722

    
723
        /* HELLO TIMER */
724
        if (oldip->helloint != newip->helloint)
725
        {
726
          ifa->helloint = newip->helloint;
727
          ifa->hello_timer->recurrent = ifa->helloint;
728
          tm_start(ifa->hello_timer, ifa->helloint);
729
          OSPF_TRACE(D_EVENTS,
730
                     "Changing hello interval on interface %s from %d to %d",
731
                     ifa->iface->name, oldip->helloint, newip->helloint);
732
        }
733

    
734
        /* POLL TIMER */
735
        if (oldip->pollint != newip->pollint)
736
        {
737
          ifa->pollint = newip->helloint;
738
          ifa->poll_timer->recurrent = ifa->pollint;
739
          tm_start(ifa->poll_timer, ifa->pollint);
740
          OSPF_TRACE(D_EVENTS,
741
                     "Changing poll interval on interface %s from %d to %d",
742
                     ifa->iface->name, oldip->pollint, newip->pollint);
743
        }
744

    
745
        /* COST */
746
        if (oldip->cost != newip->cost)
747
        {
748
          ifa->cost = newip->cost;
749
          OSPF_TRACE(D_EVENTS,
750
                     "Changing cost interface %s from %d to %d",
751
                     ifa->iface->name, oldip->cost, newip->cost);
752
          schedule_rt_lsa(ifa->oa);
753
        }
754

    
755
        /* RX BUFF */
756
        if (oldip->rxbuf != newip->rxbuf)
757
        {
758
          ifa->rxbuf = newip->rxbuf;
759
          OSPF_TRACE(D_EVENTS,
760
                     "Changing rxbuf interface %s from %d to %d",
761
                     ifa->iface->name, oldip->rxbuf, newip->rxbuf);
762
          ospf_iface_change_mtu(po, ifa);
763
        }
764

    
765
        /* strict nbma */
766
        if ((oldip->strictnbma == 0) && (newip->strictnbma != 0))
767
        {
768
          ifa->strictnbma = newip->strictnbma;
769
          OSPF_TRACE(D_EVENTS,
770
                     "Interface %s is now strict NBMA.", ifa->iface->name);
771
        }
772
        if ((oldip->strictnbma != 0) && (newip->strictnbma == 0))
773
        {
774
          ifa->strictnbma = newip->strictnbma;
775
          OSPF_TRACE(D_EVENTS,
776
                     "Interface %s is no longer strict NBMA.",
777
                     ifa->iface->name);
778
        }
779

    
780
        /* stub */
781
        if ((oldip->stub == 0) && (newip->stub != 0))
782
        {
783
          ifa->stub = newip->stub;
784
          OSPF_TRACE(D_EVENTS, "Interface %s is now stub.", ifa->iface->name);
785
        }
786
        if ((oldip->stub != 0) && (newip->stub == 0) &&
787
            ((ifa->ioprob & OSPF_I_IP) == 0) &&
788
            (((ifa->ioprob & OSPF_I_MC) == 0) || (ifa->type == OSPF_IT_NBMA)))
789
        {
790
          ifa->stub = newip->stub;
791
          OSPF_TRACE(D_EVENTS,
792
                     "Interface %s is no longer stub.", ifa->iface->name);
793
        }
794

    
795
#ifdef OSPFv2        
796
        /* AUTHENTICATION */
797
        if (oldip->autype != newip->autype)
798
        {
799
          ifa->autype = newip->autype;
800
          OSPF_TRACE(D_EVENTS,
801
                     "Changing authentication type on interface %s",
802
                     ifa->iface->name);
803
        }
804
        /* Add *passwords */
805
        ifa->passwords = newip->passwords;
806
#endif
807

    
808
        /* priority */
809
        if (oldip->priority != newip->priority)
810
        {
811
          ifa->priority = newip->priority;
812
          OSPF_TRACE(D_EVENTS,
813
                     "Changing priority on interface %s from %d to %d",
814
                     ifa->iface->name, oldip->priority, newip->priority);
815
        }
816

    
817
        /* RXMT */
818
        if (oldip->rxmtint != newip->rxmtint)
819
        {
820
          ifa->rxmtint = newip->rxmtint;
821
          OSPF_TRACE(D_EVENTS,
822
                     "Changing retransmit interval on interface %s from %d to %d",
823
                     ifa->iface->name, oldip->rxmtint, newip->rxmtint);
824
        }
825

    
826
        /* WAIT */
827
        if (oldip->waitint != newip->waitint)
828
        {
829
          ifa->waitint = newip->waitint;
830
          if (ifa->wait_timer->expires != 0)
831
            tm_start(ifa->wait_timer, ifa->waitint);
832
          OSPF_TRACE(D_EVENTS,
833
                     "Changing wait interval on interface %s from %d to %d",
834
                     ifa->iface->name, oldip->waitint, newip->waitint);
835
        }
836

    
837
        /* INFTRANS */
838
        if (oldip->inftransdelay != newip->inftransdelay)
839
        {
840
          ifa->inftransdelay = newip->inftransdelay;
841
          OSPF_TRACE(D_EVENTS,
842
                     "Changing transmit delay on interface %s from %d to %d",
843
                     ifa->iface->name, oldip->inftransdelay,
844
                     newip->inftransdelay);
845
        }
846

    
847
        /* DEAD */
848
        olddead = (oldip->dead == 0) ? oldip->deadc * oldip->helloint : oldip->dead;
849
        newdead = (newip->dead == 0) ? newip->deadc * newip->helloint : newip->dead;
850
        if (olddead != newdead)
851
        {
852
          ifa->dead = newdead;
853
          OSPF_TRACE(D_EVENTS,
854
                     "Changing dead interval on interface %s from %d to %d",
855
                     ifa->iface->name, olddead, newdead);
856
        }
857

    
858
        /* NBMA LIST */
859
        /* First remove old */
860
        WALK_LIST_DELSAFE(nb1, nbnx, ifa->nbma_list)
861
        {
862
          found = 0;
863
          WALK_LIST(nb2, newip->nbma_list)
864
            if (ipa_compare(nb1->ip, nb2->ip) == 0)
865
          {
866
            found = 1;
867
            if (nb1->eligible != nb2->eligible)
868
              OSPF_TRACE(D_EVENTS,
869
                         "Changing neighbor eligibility %I on interface %s",
870
                         nb1->ip, ifa->iface->name);
871
            break;
872
          }
873

    
874
          if (!found)
875
          {
876
            OSPF_TRACE(D_EVENTS,
877
                       "Removing NBMA neighbor %I on interface %s",
878
                       nb1->ip, ifa->iface->name);
879
            rem_node(NODE nb1);
880
            mb_free(nb1);
881
          }
882
        }
883
        /* And then add new */
884
        WALK_LIST(nb2, newip->nbma_list)
885
        {
886
          found = 0;
887
          WALK_LIST(nb1, ifa->nbma_list)
888
            if (ipa_compare(nb1->ip, nb2->ip) == 0)
889
          {
890
            found = 1;
891
            break;
892
          }
893
          if (!found)
894
          {
895
            nb1 = mb_alloc(p->pool, sizeof(struct nbma_node));
896
            nb1->ip = nb2->ip;
897
            nb1->eligible = nb2->eligible;
898
            add_tail(&ifa->nbma_list, NODE nb1);
899
            OSPF_TRACE(D_EVENTS,
900
                       "Adding NBMA neighbor %I on interface %s",
901
                       nb1->ip, ifa->iface->name);
902
          }
903
        }
904
      }
905
    }
906

    
907
    oldac = (struct ospf_area_config *)(NODE(oldac))->next;
908
    newac = (struct ospf_area_config *)(NODE(newac))->next;
909
  }
910

    
911
  if (((NODE(oldac))->next) != ((NODE(newac))->next))
912
    return 0;                        /* One is not null */
913

    
914
  return 1;                        /* Everything OK :-) */
915
}
916

    
917
void
918
ospf_sh_neigh(struct proto *p, char *iff)
919
{
920
  struct ospf_iface *ifa = NULL;
921
  struct ospf_neighbor *n;
922
  struct proto_ospf *po = (struct proto_ospf *) p;
923

    
924
  if (p->proto_state != PS_UP)
925
  {
926
    cli_msg(-1013, "%s: is not up", p->name);
927
    cli_msg(0, "");
928
    return;
929
  }
930

    
931
  cli_msg(-1013, "%s:", p->name);
932
  cli_msg(-1013, "%-12s\t%3s\t%-15s\t%-5s\t%-10s %-12s", "Router ID", "Pri",
933
          "     State", "DTime", "Interface", "Router IP");
934
  WALK_LIST(ifa, po->iface_list)
935
    if ((iff == NULL) || patmatch(iff, ifa->iface->name))
936
      WALK_LIST(n, ifa->neigh_list)
937
        ospf_sh_neigh_info(n);
938
  cli_msg(0, "");
939
}
940

    
941
void
942
ospf_sh(struct proto *p)
943
{
944
  struct ospf_area *oa;
945
  struct proto_ospf *po = (struct proto_ospf *) p;
946
  struct ospf_iface *ifa;
947
  struct ospf_neighbor *n;
948
  int ifano, nno, adjno, firstfib;
949
  struct area_net *anet;
950

    
951
  if (p->proto_state != PS_UP)
952
  {
953
    cli_msg(-1014, "%s: is not up", p->name);
954
    cli_msg(0, "");
955
    return;
956
  }
957

    
958
  cli_msg(-1014, "%s:", p->name);
959
  cli_msg(-1014, "RFC1583 compatibility: %s", (po->rfc1583 ? "enable" : "disabled"));
960
  cli_msg(-1014, "RT scheduler tick: %d", po->tick);
961
  cli_msg(-1014, "Number of areas: %u", po->areano);
962
  cli_msg(-1014, "Number of LSAs in DB:\t%u", po->gr->hash_entries);
963

    
964
  WALK_LIST(oa, po->area_list)
965
  {
966
    cli_msg(-1014, "\tArea: %R (%u) %s", oa->areaid, oa->areaid,
967
            oa->areaid == 0 ? "[BACKBONE]" : "");
968
    ifano = 0;
969
    nno = 0;
970
    adjno = 0;
971
    WALK_LIST(ifa, po->iface_list)
972
    {
973
      if (oa == ifa->oa)
974
      {
975
        ifano++;
976
        WALK_LIST(n, ifa->neigh_list)
977
        {
978
          nno++;
979
          if (n->state == NEIGHBOR_FULL)
980
            adjno++;
981
        }
982
      }
983
    }
984
    cli_msg(-1014, "\t\tStub:\t%s", oa->stub ? "Yes" : "No");
985
    cli_msg(-1014, "\t\tTransit:\t%s", oa->trcap ? "Yes" : "No");
986
    cli_msg(-1014, "\t\tNumber of interfaces:\t%u", ifano);
987
    cli_msg(-1014, "\t\tNumber of neighbors:\t%u", nno);
988
    cli_msg(-1014, "\t\tNumber of adjacent neighbors:\t%u", adjno);
989

    
990
    firstfib = 1;
991
    FIB_WALK(&oa->net_fib, nftmp)
992
    {
993
      anet = (struct area_net *) nftmp;
994
      if(firstfib)
995
      {
996
        cli_msg(-1014, "\t\tArea networks:");
997
        firstfib = 0;
998
      }
999
      cli_msg(-1014, "\t\t\t%1I/%u\t%s\t%s", anet->fn.prefix, anet->fn.pxlen,
1000
                anet->hidden ? "Hidden" : "Advertise", anet->active ? "Active" : "");
1001
    }
1002
    FIB_WALK_END;
1003
  }
1004
  cli_msg(0, "");
1005
}
1006

    
1007
void
1008
ospf_sh_iface(struct proto *p, char *iff)
1009
{
1010
  struct proto_ospf *po = (struct proto_ospf *) p;
1011
  struct ospf_iface *ifa = NULL;
1012

    
1013
  if (p->proto_state != PS_UP)
1014
  {
1015
    cli_msg(-1015, "%s: is not up", p->name);
1016
    cli_msg(0, "");
1017
    return;
1018
  }
1019

    
1020
  cli_msg(-1015, "%s:", p->name);
1021
  WALK_LIST(ifa, po->iface_list)
1022
    if ((iff == NULL) || patmatch(iff, ifa->iface->name))
1023
      ospf_iface_info(ifa);
1024
  cli_msg(0, "");
1025
}
1026

    
1027
/* lsa_compare_for_state() - Compare function for 'show ospf state'
1028
 *
1029
 * First we want to separate network-LSAs and other LSAs (because network-LSAs
1030
 * will be presented as network nodes and other LSAs together as router nodes)
1031
 * Network-LSAs are sorted according to network prefix, other LSAs are sorted
1032
 * according to originating router id (to get all LSA needed to represent one
1033
 * router node together). Then, according to LSA type, ID and age.
1034
 *
1035
 * For OSPFv3, we have to handle also Prefix-LSAs. We would like to put each
1036
 * immediately after the referenced LSA. We will make faked LSA based on ref_
1037
 * values
1038
 */
1039

    
1040
#ifdef OSPFv3
1041

    
1042
static struct ospf_lsa_header *
1043
fake_lsa_from_prefix_lsa(struct ospf_lsa_header *dst, struct ospf_lsa_header *src,
1044
                         struct ospf_lsa_prefix *px)
1045
{
1046
  dst->age = src->age;
1047
  dst->type = px->ref_type;
1048
  dst->id = px->ref_id;
1049
  dst->rt = px->ref_rt;
1050
  dst->sn = src->sn;
1051

    
1052
  return dst;
1053
}
1054

    
1055
#endif
1056

    
1057
static int
1058
lsa_compare_for_state(const void *p1, const void *p2)
1059
{
1060
  struct top_hash_entry * he1 = * (struct top_hash_entry **) p1;
1061
  struct top_hash_entry * he2 = * (struct top_hash_entry **) p2;
1062
  struct ospf_lsa_header *lsa1 = &(he1->lsa);
1063
  struct ospf_lsa_header *lsa2 = &(he2->lsa);
1064

    
1065
  if (he1->domain != he2->domain)
1066
    return he1->domain - he2->domain;
1067

    
1068
#ifdef OSPFv3
1069
  struct ospf_lsa_header lsatmp1, lsatmp2;
1070

    
1071
  int px1 = (lsa1->type == LSA_T_PREFIX);
1072
  int px2 = (lsa2->type == LSA_T_PREFIX);
1073

    
1074
  if (px1)
1075
    lsa1 = fake_lsa_from_prefix_lsa(&lsatmp1, lsa1, he1->lsa_body);
1076

    
1077
  if (px2)
1078
    lsa2 = fake_lsa_from_prefix_lsa(&lsatmp2, lsa2, he2->lsa_body);
1079
#endif
1080

    
1081
  int nt1 = (lsa1->type == LSA_T_NET);
1082
  int nt2 = (lsa2->type == LSA_T_NET);
1083

    
1084
  if (nt1 != nt2)
1085
    return nt1 - nt2;
1086

    
1087
  if (nt1)
1088
  {
1089
#ifdef OSPFv3
1090
    /* In OSPFv3, neworks are named base on ID of DR */
1091
    if (lsa1->rt != lsa2->rt)
1092
      return lsa1->rt - lsa2->rt;
1093
#endif
1094

    
1095
    /* For OSPFv2, this is IP of the network,
1096
       for OSPFv3, this is interface ID */
1097
    if (lsa1->id != lsa2->id)
1098
      return lsa1->id - lsa2->id;
1099

    
1100
#ifdef OSPFv3
1101
    if (px1 != px2)
1102
      return px1 - px2;
1103
#endif
1104

    
1105
    return lsa1->sn - lsa2->sn;
1106
  }
1107
  else 
1108
  {
1109
    if (lsa1->rt != lsa2->rt)
1110
      return lsa1->rt - lsa2->rt;
1111

    
1112
    if (lsa1->type != lsa2->type)
1113
      return lsa1->type - lsa2->type;
1114
  
1115
    if (lsa1->id != lsa2->id)
1116
      return lsa1->id - lsa2->id;
1117

    
1118
#ifdef OSPFv3
1119
    if (px1 != px2)
1120
      return px1 - px2;
1121
#endif
1122
  
1123
    return lsa1->sn - lsa2->sn;
1124
  }
1125
}
1126

    
1127
static inline void
1128
show_lsa_router(struct proto_ospf *po, struct top_hash_entry *he)
1129
{
1130
  struct ospf_lsa_header *lsa = &(he->lsa);
1131
  struct ospf_lsa_rt *rt = he->lsa_body;
1132
  struct ospf_lsa_rt_link *rr = (struct ospf_lsa_rt_link *) (rt + 1);
1133
  int max = lsa_rt_count(lsa);
1134
  int i;
1135

    
1136
  for (i = 0; i < max; i++)
1137
    if (rr[i].type == LSART_VLNK)
1138
      cli_msg(-1016, "\t\tvlink %R metric %u", rr[i].id, rr[i].metric);
1139

    
1140
  for (i = 0; i < max; i++)
1141
    if (rr[i].type == LSART_PTP)
1142
      cli_msg(-1016, "\t\trouter %R metric %u", rr[i].id, rr[i].metric);
1143

    
1144
  for (i = 0; i < max; i++)
1145
    if (rr[i].type == LSART_NET)
1146
    {
1147
#ifdef OSPFv2
1148
      struct top_hash_entry *net_he = ospf_hash_find_net(po->gr, he->domain, rr[i].id);
1149

    
1150
      if (net_he)
1151
      {
1152
        struct ospf_lsa_header *net_lsa = &(net_he->lsa);
1153
        struct ospf_lsa_net *net_ln = net_he->lsa_body;
1154

    
1155
        cli_msg(-1016, "\t\tnetwork %I/%d metric %u", 
1156
                ipa_and(ipa_from_u32(net_lsa->id), net_ln->netmask),
1157
                ipa_mklen(net_ln->netmask), rr[i].metric);
1158
      }
1159
      else
1160
        cli_msg(-1016, "\t\tnetwork [%R] metric %u", rr[i].id, rr[i].metric);
1161

    
1162
#else /* OSPFv3 */
1163
      cli_msg(-1016, "\t\tnetwork [%R-%u] metric %u", rr[i].id, rr[i].nif, rr[i].metric);
1164
#endif
1165
    }
1166

    
1167
#ifdef OSPFv2
1168
  for (i = 0; i < max; i++)
1169
    if (rr[i].type == LSART_STUB)
1170
      cli_msg(-1016, "\t\tstubnet %I/%d metric %u", ipa_from_u32(rr[i].id),
1171
              ipa_mklen(ipa_from_u32(rr[i].data)), rr[i].metric);
1172
#endif
1173
}
1174

    
1175
static inline void
1176
show_lsa_network(struct top_hash_entry *he)
1177
{
1178
  struct ospf_lsa_header *lsa = &(he->lsa);
1179
  struct ospf_lsa_net *ln = he->lsa_body;
1180
  u32 i;
1181

    
1182
#ifdef OSPFv2
1183
  cli_msg(-1016, "");
1184
  cli_msg(-1016, "\tnetwork %I/%d", ipa_and(ipa_from_u32(lsa->id), ln->netmask), ipa_mklen(ln->netmask));
1185
  cli_msg(-1016, "\t\tdr %R", lsa->rt);
1186
#else /* OSPFv3 */
1187
  cli_msg(-1016, "");
1188
  cli_msg(-1016, "\tnetwork [%R-%u]", lsa->rt, lsa->id);
1189
#endif
1190

    
1191
  for (i = 0; i < lsa_net_count(lsa); i++)
1192
    cli_msg(-1016, "\t\trouter %R", ln->routers[i]);
1193
}
1194

    
1195
static inline void
1196
show_lsa_sum_net(struct top_hash_entry *he)
1197
{
1198
  ip_addr ip;
1199
  int pxlen;
1200

    
1201
#ifdef OSPFv2
1202
  struct ospf_lsa_sum *ls = he->lsa_body;
1203
  pxlen = ipa_mklen(ls->netmask);
1204
  ip = ipa_and(ipa_from_u32(he->lsa.id), ls->netmask);
1205
#else /* OSPFv3 */
1206
  u8 pxopts;
1207
  u16 rest;
1208
  struct ospf_lsa_sum_net *ls = he->lsa_body;
1209
  lsa_get_ipv6_prefix(ls->prefix, &ip, &pxlen, &pxopts, &rest);
1210
#endif
1211

    
1212
  cli_msg(-1016, "\t\txnetwork %I/%d", ip, pxlen);
1213
}
1214

    
1215
static inline void
1216
show_lsa_sum_rt(struct top_hash_entry *he)
1217
{
1218
  u32 dst_rid, options;
1219

    
1220
#ifdef OSPFv2
1221
  //  struct ospf_lsa_sum *ls = he->lsa_body;
1222
  dst_rid = he->lsa.id;
1223
  options = 0;
1224
#else /* OSPFv3 */
1225
  struct ospf_lsa_sum_rt *ls = he->lsa_body;
1226
  dst_rid = ls->drid; 
1227
  options = ls->options & OPTIONS_MASK;
1228
#endif
1229

    
1230
  cli_msg(-1016, "\t\txrouter %R", dst_rid);
1231
}
1232

    
1233

    
1234
static inline void
1235
show_lsa_external(struct top_hash_entry *he)
1236
{
1237
  struct ospf_lsa_header *lsa = &(he->lsa);
1238
  struct ospf_lsa_ext *ext = he->lsa_body;
1239
  char str_via[STD_ADDRESS_P_LENGTH + 8] = "";
1240
  char str_tag[16] = "";
1241
  ip_addr ip, rt_fwaddr;
1242
  int pxlen, ebit, rt_fwaddr_valid;
1243
  u32 rt_tag, rt_metric;
1244

    
1245
  rt_metric = ext->metric & METRIC_MASK;
1246
  ebit = ext->metric & LSA_EXT_EBIT;
1247
#ifdef OSPFv2
1248
  ip = ipa_and(ipa_from_u32(lsa->id), ext->netmask);
1249
  pxlen = ipa_mklen(ext->netmask);
1250
  rt_fwaddr = ext->fwaddr;
1251
  rt_fwaddr_valid = !ipa_equal(rt_fwaddr, IPA_NONE);
1252
  rt_tag = ext->tag;
1253
#else /* OSPFv3 */
1254
  u8 pxopts;
1255
  u16 rest;
1256
  u32 *buf = ext->rest;
1257
  buf = lsa_get_ipv6_prefix(buf, &ip, &pxlen, &pxopts, &rest);
1258

    
1259
  rt_fwaddr_valid = ext->metric & LSA_EXT_FBIT;
1260
  if (rt_fwaddr_valid)
1261
    buf = lsa_get_ipv6_addr(buf, &rt_fwaddr);
1262
  else 
1263
    rt_fwaddr = IPA_NONE;
1264

    
1265
  if (ext->metric & LSA_EXT_TBIT)
1266
    rt_tag = *buf++;
1267
  else
1268
    rt_tag = 0;
1269
#endif
1270
  
1271
  if (rt_fwaddr_valid)
1272
    bsprintf(str_via, " via %I", rt_fwaddr);
1273

    
1274
  if (rt_tag)
1275
    bsprintf(str_tag, " tag %08x", rt_tag);
1276

    
1277
  cli_msg(-1016, "\t\texternal %I/%d metric%s %u%s%s", ip, pxlen,
1278
          ebit ? "2" : "", rt_metric, str_via, str_tag);
1279
}
1280

    
1281
#ifdef OSPFv3
1282
static inline void
1283
show_lsa_prefix(struct top_hash_entry *he, struct ospf_lsa_header *olsa)
1284
{
1285
  struct ospf_lsa_header *lsa = &(he->lsa);
1286
  struct ospf_lsa_prefix *px = he->lsa_body;
1287
  struct ospf_lsa_ext *ext = he->lsa_body;
1288
  char *msg;
1289
  ip_addr pxa;
1290
  int pxlen;
1291
  u8 pxopts;
1292
  u16 metric;
1293
  u32 *buf;
1294
  int i;
1295

    
1296
  /* We check whether given prefix-LSA is related to the last non-prefix-LSA */
1297
  if ((olsa == NULL) || (olsa->type != px->ref_type) || (olsa->rt != px->ref_rt) ||
1298
      !(((px->ref_type == LSA_T_RT)  && (px->ref_id == 0)) ||
1299
        ((px->ref_type == LSA_T_NET) && (px->ref_id == olsa->id))))
1300
    return;
1301

    
1302
  buf = px->rest;
1303
  for (i = 0; i < px->pxcount; i++)
1304
    {
1305
      buf = lsa_get_ipv6_prefix(buf, &pxa, &pxlen, &pxopts, &metric);
1306

    
1307
      if (px->ref_type == LSA_T_RT)
1308
        cli_msg(-1016, "\t\tstubnet %I/%d metric %u", pxa, pxlen, metric);
1309
      else
1310
        cli_msg(-1016, "\t\taddress %I/%d", pxa, pxlen);
1311
    }
1312
}
1313
#endif
1314

    
1315
void
1316
ospf_sh_state(struct proto *p, int verbose)
1317
{
1318
  struct proto_ospf *po = (struct proto_ospf *) p;
1319
  struct top_graph *f = po->gr;
1320
  unsigned int i, j1, j2;
1321
  u32 last_rt = 0xFFFFFFFF;
1322
  u32 last_area = 0xFFFFFFFF;
1323

    
1324
#ifdef OSPFv3
1325
  struct ospf_lsa_header *olsa = NULL;
1326
#endif
1327

    
1328
  if (p->proto_state != PS_UP)
1329
  {
1330
    cli_msg(-1016, "%s: is not up", p->name);
1331
    cli_msg(0, "");
1332
    return;
1333
  }
1334

    
1335
  struct top_hash_entry *hea[f->hash_entries];
1336
  struct top_hash_entry *he;
1337

    
1338
  j1 = j2 = 0;
1339
  WALK_SLIST(he, po->lsal)
1340
  {
1341
    int accept;
1342

    
1343
    switch (he->lsa.type)
1344
      {
1345
      case LSA_T_RT:
1346
      case LSA_T_NET:
1347
        accept = 1;
1348
        break;
1349

    
1350
      case LSA_T_SUM_NET:
1351
      case LSA_T_SUM_RT:
1352
      case LSA_T_EXT:
1353
#ifdef OSPFv3
1354
      case LSA_T_PREFIX:
1355
#endif
1356
        accept = verbose;
1357
        break;
1358

    
1359
      default:
1360
        accept = 0;
1361
      }
1362

    
1363
    if (accept)
1364
      hea[j1++] = he;
1365
    else
1366
      j2++;
1367
  }
1368

    
1369
  if ((j1 + j2) != f->hash_entries)
1370
    die("Fatal mismatch");
1371

    
1372
  qsort(hea, j1, sizeof(struct top_hash_entry *), lsa_compare_for_state);
1373

    
1374
  for (i = 0; i < j1; i++)
1375
  {
1376
    if (last_area != hea[i]->domain)
1377
    {
1378
      cli_msg(-1016, "");
1379
      cli_msg(-1016, "area %R", hea[i]->domain);
1380
      last_area = hea[i]->domain;
1381
      last_rt = 0xFFFFFFFF;
1382
    }
1383

    
1384
    if ((hea[i]->lsa.rt != last_rt) && (hea[i]->lsa.type != LSA_T_NET)
1385
#ifdef OSPFv3
1386
        && (hea[i]->lsa.type != LSA_T_PREFIX)
1387
#endif
1388
        )
1389
    {
1390
      cli_msg(-1016, "");
1391
      cli_msg(-1016, (hea[i]->lsa.type != LSA_T_EXT) ? "\trouter %R" : "\txrouter %R", hea[i]->lsa.rt);
1392
      last_rt = hea[i]->lsa.rt;
1393
    }
1394

    
1395
    switch (hea[i]->lsa.type)
1396
    {
1397
      case LSA_T_RT:
1398
        show_lsa_router(po, hea[i]);
1399
        break;
1400

    
1401
      case LSA_T_NET:
1402
        show_lsa_network(hea[i]);
1403
        break;
1404

    
1405
      case LSA_T_SUM_NET:
1406
        show_lsa_sum_net(hea[i]);
1407
        break;
1408

    
1409
      case LSA_T_SUM_RT:
1410
        show_lsa_sum_rt(hea[i]);
1411
        break;
1412

    
1413
      case LSA_T_EXT:
1414
        show_lsa_external(hea[i]);
1415
        break;
1416

    
1417
#ifdef OSPFv3
1418
      case LSA_T_PREFIX:
1419
        show_lsa_prefix(hea[i], olsa);
1420
        break;
1421
#endif
1422
    }
1423

    
1424
#ifdef OSPFv3
1425
    if (hea[i]->lsa.type != LSA_T_PREFIX)
1426
      olsa = &(hea[i]->lsa);
1427
#endif
1428
  }
1429
  cli_msg(0, "");
1430
}
1431

    
1432

    
1433
static int
1434
lsa_compare_for_lsadb(const void *p1, const void *p2)
1435
{
1436
  struct top_hash_entry * he1 = * (struct top_hash_entry **) p1;
1437
  struct top_hash_entry * he2 = * (struct top_hash_entry **) p2;
1438
  struct ospf_lsa_header *lsa1 = &(he1->lsa);
1439
  struct ospf_lsa_header *lsa2 = &(he2->lsa);
1440
  int sc1 = LSA_SCOPE(lsa1);
1441
  int sc2 = LSA_SCOPE(lsa2);
1442

    
1443
  if (sc1 != sc2)
1444
    return sc2 - sc1;
1445

    
1446
  if (he1->domain != he2->domain)
1447
    return he1->domain - he2->domain;
1448

    
1449
  if (lsa1->rt != lsa2->rt)
1450
    return lsa1->rt - lsa2->rt;
1451
  
1452
  if (lsa1->id != lsa2->id)
1453
    return lsa1->id - lsa2->id;
1454

    
1455
  if (lsa1->type != lsa2->type)
1456
    return lsa1->type - lsa2->type;
1457

    
1458
  return lsa1->sn - lsa2->sn;
1459
}
1460

    
1461
void
1462
ospf_sh_lsadb(struct proto *p)
1463
{
1464
  struct proto_ospf *po = (struct proto_ospf *) p;
1465
  struct top_graph *f = po->gr;
1466
  unsigned int i, j;
1467
  int last_dscope = -1;
1468
  u32 last_domain = 0;
1469

    
1470
  if (p->proto_state != PS_UP)
1471
  {
1472
    cli_msg(-1017, "%s: is not up", p->name);
1473
    cli_msg(0, "");
1474
    return;
1475
  }
1476

    
1477
  struct top_hash_entry *hea[f->hash_entries];
1478
  struct top_hash_entry *he;
1479

    
1480
  j = 0;
1481
  WALK_SLIST(he, po->lsal)
1482
    hea[j++] = he;
1483

    
1484
  if (j != f->hash_entries)
1485
    die("Fatal mismatch");
1486

    
1487
  qsort(hea, j, sizeof(struct top_hash_entry *), lsa_compare_for_lsadb);
1488

    
1489
  for (i = 0; i < j; i++)
1490
  {
1491
    struct ospf_lsa_header *lsa = &(hea[i]->lsa);
1492
    int dscope = LSA_SCOPE(lsa);
1493
    
1494
    if ((dscope != last_dscope) || (hea[i]->domain != last_domain))
1495
    {
1496
      cli_msg(-1017, "");
1497
      switch (dscope)
1498
      {
1499
        case LSA_SCOPE_AS:
1500
          cli_msg(-1017, "Global");
1501
          break;
1502
        case LSA_SCOPE_AREA:
1503
          cli_msg(-1017, "Area %R", hea[i]->domain);
1504
          break;
1505
#ifdef OSPFv3
1506
        case LSA_SCOPE_LINK:
1507
          struct iface *ifa = if_find_by_index(hea[i]->domain);
1508
          cli_msg(-1017, "Link %s", (ifa != NULL) ? ifa->name : "?");
1509
          break;
1510
#endif
1511
      }
1512
      cli_msg(-1017, "");
1513
      cli_msg(-1017," Type   LS ID           Router           Age  Sequence  Checksum");
1514

    
1515
      last_dscope = dscope;
1516
      last_domain = hea[i]->domain;
1517
    }
1518

    
1519

    
1520
    cli_msg(-1017," %04x  %-15R %-15R %5u  %08x    %04x",
1521
            lsa->type, lsa->id, lsa->rt, lsa->age, lsa->sn, lsa->checksum);
1522
  }
1523
  cli_msg(0, "");
1524
}
1525

    
1526

    
1527
struct protocol proto_ospf = {
1528
  name:"OSPF",
1529
  template:"ospf%d",
1530
  attr_class:EAP_OSPF,
1531
  init:ospf_init,
1532
  dump:ospf_dump,
1533
  start:ospf_start,
1534
  shutdown:ospf_shutdown,
1535
  get_route_info:ospf_get_route_info,
1536
  get_attr:ospf_get_attr,
1537
  get_status:ospf_get_status,
1538
  reconfigure:ospf_reconfigure
1539
};