Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (40.5 KB)

1 c1f8dc91 Ondrej Filip
/*
2
 *        BIRD -- OSPF
3
 *
4 98ac6176 Ondrej Filip
 *        (c) 1999--2004 Ondrej Filip <feela@network.cz>
5 c1f8dc91 Ondrej Filip
 *
6
 *        Can be freely distributed and used under the terms of the GNU GPL.
7
 */
8
9 89755a86 Ondrej Filip
/**
10
 * DOC: Open Shortest Path First (OSPF)
11
 * 
12 725270cb Martin Mares
 * The OSPF protocol is quite complicated and its complex implemenation is
13 baa5dd6c Ondrej Filip
 * split to many files. In |ospf.c|, you will find mainly the interface
14 725270cb Martin Mares
 * for communication with the core (e.g., reconfiguration hooks, shutdown
15 baa5dd6c Ondrej Filip
 * 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 725270cb Martin Mares
 * interface data structures. Source |neighbor.c| includes the neighbor state
20 baa5dd6c Ondrej Filip
 * machine and functions for election of Designated Router and Backup
21
 * Designated router. In |hello.c|, there are routines for sending
22 725270cb Martin Mares
 * 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 baa5dd6c Ondrej Filip
 * link-state acknowledgements and database descriptions respectively.
26 725270cb Martin Mares
 * 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 baa5dd6c Ondrej Filip
 * a place where routines for searching LSAs in the link-state database,
29 725270cb Martin Mares
 * adding and deleting them reside, there also are functions for originating
30 baa5dd6c Ondrej Filip
 * of various types of LSAs (router LSA, net LSA, external LSA). File |rt.c|
31 725270cb Martin Mares
 * contains routines for calculating the routing table. |lsalib.c| is a set
32 baa5dd6c Ondrej Filip
 * of various functions for working with the LSAs (endianity conversions,
33 725270cb Martin Mares
 * calculation of checksum etc.).
34 89755a86 Ondrej Filip
 *
35 725270cb Martin Mares
 * 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 baa5dd6c Ondrej Filip
 * &ospf_iface structures are connected. &ospf_area is also connected to
40 725270cb Martin Mares
 * &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 baa5dd6c Ondrej Filip
 * kept in the endianity of the CPU.
44 86c84d76 Ondrej Filip
 *
45 48cff379 Ondrej Zajicek
 * 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 86c84d76 Ondrej Filip
 * 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 37c3e558 Ondrej Filip
 * 
76 86c84d76 Ondrej Filip
 * The function area_disp() is
77
 * responsible for late originating of router LSA and network LSA
78 0ea8fb4a Ondrej Zajicek
 * and for cleanup before routing table calculation process in
79 86c84d76 Ondrej Filip
 * the area.
80 725270cb Martin Mares
 * 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 89755a86 Ondrej Filip
 *
84 725270cb Martin Mares
 * BIRD's OSPF implementation respects RFC2328 in every detail, but
85 baa5dd6c Ondrej Filip
 * 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 725270cb Martin Mares
 * snapshot. The database can be quite large in some networks, so
89 baa5dd6c Ondrej Filip
 * 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 89755a86 Ondrej Filip
 *
93 725270cb Martin Mares
 * 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 baa5dd6c Ondrej Filip
 * also those we've deleted to the core's routing table and the
98 725270cb Martin Mares
 * core will take care of the rest. This simplifies the process
99
 * and conserves memory.
100 89755a86 Ondrej Filip
 */
101
102 a3b70dc4 Ondrej Zajicek
#include <stdlib.h>
103 c1f8dc91 Ondrej Filip
#include "ospf.h"
104
105 3d15dcdb Ondrej Zajicek
106 f7574707 Ondrej Zajicek
static int ospf_reload_routes(struct proto *p);
107 dca75fd7 Ondrej Zajicek
static void ospf_rt_notify(struct proto *p, struct rtable *table UNUSED, net * n, rte * new, rte * old UNUSED, ea_list * attrs);
108 d83faf8d Martin Mares
static int ospf_rte_better(struct rte *new, struct rte *old);
109
static int ospf_rte_same(struct rte *new, struct rte *old);
110 b8f17cf1 Ondrej Filip
static void ospf_disp(timer *timer);
111
112 98ac6176 Ondrej Filip
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 8e48831a Ondrej Zajicek
    struct area_net_config *anc;
126
    struct area_net *an;
127 98ac6176 Ondrej Filip
128 8e48831a Ondrej Zajicek
    fib_init(&oa->net_fib, po->proto.pool, sizeof(struct area_net), 0, ospf_area_initfib);
129 ed317862 Ondrej Zajicek
    fib_init(&oa->enet_fib, po->proto.pool, sizeof(struct area_net), 0, ospf_area_initfib);
130 98ac6176 Ondrej Filip
131 8e48831a Ondrej Zajicek
    WALK_LIST(anc, ac->net_list)
132 98ac6176 Ondrej Filip
    {
133 8e48831a Ondrej Zajicek
      an = (struct area_net *) fib_get(&oa->net_fib, &anc->px.addr, anc->px.len);
134 ed317862 Ondrej Zajicek
      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 98ac6176 Ondrej Filip
    }
143
}
144 d83faf8d Martin Mares
145 8e48831a Ondrej Zajicek
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 41b612c3 Ondrej Zajicek
  oa->options = ac->type;
169 8e48831a Ondrej Zajicek
#else /* OSPFv3 */
170 41b612c3 Ondrej Zajicek
  oa->options = OPT_R | ac->type | OPT_V6;
171 8e48831a Ondrej Zajicek
#endif
172
173 2918e610 Ondrej Zajicek
  /*
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 8e48831a Ondrej Zajicek
  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 ed317862 Ondrej Zajicek
  fib_free(&oa->enet_fib);
197 8e48831a Ondrej Zajicek
198 4160a9dd Ondrej Zajicek
  if (oa->translator_timer)
199
    rfree(oa->translator_timer);
200
201 8e48831a Ondrej Zajicek
  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 c1f8dc91 Ondrej Filip
static int
228
ospf_start(struct proto *p)
229
{
230 2e10a170 Ondrej Filip
  struct proto_ospf *po = (struct proto_ospf *) p;
231
  struct ospf_config *c = (struct ospf_config *) (p->cf);
232 51cff78b Ondrej Filip
  struct ospf_area_config *ac;
233 f14032ef Ondrej Filip
234 34a877cc Ondrej Zajicek
  po->router_id = proto_get_router_id(p->cf);
235 b8f17cf1 Ondrej Filip
  po->rfc1583 = c->rfc1583;
236
  po->ebit = 0;
237 57c574d8 Ondrej Zajicek
  po->ecmp = c->ecmp;
238 b8f17cf1 Ondrej Filip
  po->tick = c->tick;
239 98ac6176 Ondrej Filip
  po->disp_timer = tm_new(p->pool);
240 b8f17cf1 Ondrej Filip
  po->disp_timer->data = po;
241
  po->disp_timer->randomize = 0;
242
  po->disp_timer->hook = ospf_disp;
243
  po->disp_timer->recurrent = po->tick;
244
  tm_start(po->disp_timer, 1);
245 3d15dcdb Ondrej Zajicek
  po->lsab_size = 256;
246
  po->lsab_used = 0;
247
  po->lsab = mb_alloc(p->pool, po->lsab_size);
248 57c574d8 Ondrej Zajicek
  po->nhpool = lp_new(p->pool, 12*sizeof(struct mpnh));
249 5970fcda Ondrej Filip
  init_list(&(po->iface_list));
250
  init_list(&(po->area_list));
251 824de84d Ondrej Zajicek
  fib_init(&po->rtf, p->pool, sizeof(ort), 0, ospf_rt_initort);
252 2e10a170 Ondrej Filip
  po->areano = 0;
253 86c84d76 Ondrej Filip
  po->gr = ospf_top_new(p->pool);
254
  s_init_list(&(po->lsal));
255 5b1a92e6 Ondrej Filip
256 2e10a170 Ondrej Filip
  WALK_LIST(ac, c->area_list)
257 8e48831a Ondrej Zajicek
    ospf_area_add(po, ac, 0);
258 6384c7d7 Ondrej Zajicek
259 8e48831a Ondrej Zajicek
  /* Add all virtual links */
260
  struct ospf_iface_patt *ic;
261
  WALK_LIST(ic, c->vlink_list)
262
    ospf_iface_new(po->backbone, NULL, ic);
263 6384c7d7 Ondrej Zajicek
264 c1f8dc91 Ondrej Filip
  return PS_UP;
265
}
266
267
static void
268
ospf_dump(struct proto *p)
269
{
270 b332fcdf Ondrej Filip
  struct ospf_iface *ifa;
271
  struct ospf_neighbor *n;
272 2e10a170 Ondrej Filip
  struct proto_ospf *po = (struct proto_ospf *) p;
273 c1f8dc91 Ondrej Filip
274 ee4880c8 Ondrej Filip
  OSPF_TRACE(D_EVENTS, "Area number: %d", po->areano);
275 b332fcdf Ondrej Filip
276 163b2073 Ondrej Filip
  WALK_LIST(ifa, po->iface_list)
277 b332fcdf Ondrej Filip
  {
278 3b16080c Ondrej Filip
    OSPF_TRACE(D_EVENTS, "Interface: %s", (ifa->iface ? ifa->iface->name : "(null)"));
279 f14032ef Ondrej Filip
    OSPF_TRACE(D_EVENTS, "state: %u", ifa->state);
280 3aab39f5 Ondrej Zajicek
    OSPF_TRACE(D_EVENTS, "DR:  %R", ifa->drid);
281
    OSPF_TRACE(D_EVENTS, "BDR: %R", ifa->bdrid);
282 b332fcdf Ondrej Filip
    WALK_LIST(n, ifa->neigh_list)
283
    {
284 3aab39f5 Ondrej Zajicek
      OSPF_TRACE(D_EVENTS, "  neighbor %R in state %u", n->rid, n->state);
285 b332fcdf Ondrej Filip
    }
286
  }
287 163b2073 Ondrej Filip
288 e81b440f Ondrej Zajicek
  /*
289 86c84d76 Ondrej Filip
  OSPF_TRACE(D_EVENTS, "LSA graph dump start:");
290
  ospf_top_dump(po->gr, p);
291
  OSPF_TRACE(D_EVENTS, "LSA graph dump finished");
292 e81b440f Ondrej Zajicek
  */
293 c6c56264 Ondrej Filip
  neigh_dump_all();
294 c1f8dc91 Ondrej Filip
}
295
296
static struct proto *
297
ospf_init(struct proto_config *c)
298
{
299 4c630a6d Ondrej Filip
  struct proto *p = proto_new(c, sizeof(struct proto_ospf));
300 c1f8dc91 Ondrej Filip
301 23ac9e9a Ondrej Zajicek
  p->accept_ra_types = RA_OPTIMAL;
302 e8085aba Ondrej Filip
  p->rt_notify = ospf_rt_notify;
303 d9e7e1b1 Ondrej Zajicek
  p->if_notify = ospf_if_notify;
304 3d15dcdb Ondrej Zajicek
  p->ifa_notify = ospf_ifa_notify;
305 094d2bdb Ondrej Zajicek
  p->import_control = ospf_import_control;
306
  p->reload_routes = ospf_reload_routes;
307
  p->make_tmp_attrs = ospf_make_tmp_attrs;
308
  p->store_tmp_attrs = ospf_store_tmp_attrs;
309 5919c66e Martin Mares
  p->rte_better = ospf_rte_better;
310
  p->rte_same = ospf_rte_same;
311 163b2073 Ondrej Filip
312 c1f8dc91 Ondrej Filip
  return p;
313
}
314
315 bbd76b42 Ondrej Filip
/* If new is better return 1 */
316
static int
317
ospf_rte_better(struct rte *new, struct rte *old)
318
{
319 2e10a170 Ondrej Filip
  if (new->u.ospf.metric1 == LSINFINITY)
320
    return 0;
321 bbd76b42 Ondrej Filip
322 98ac6176 Ondrej Filip
  if(new->attrs->source < old->attrs->source) return 1;
323
  if(new->attrs->source > old->attrs->source) return 0;
324 2e10a170 Ondrej Filip
325 98ac6176 Ondrej Filip
  if(new->attrs->source == RTS_OSPF_EXT2)
326 bbd76b42 Ondrej Filip
  {
327 98ac6176 Ondrej Filip
    if(new->u.ospf.metric2 < old->u.ospf.metric2) return 1;
328
    if(new->u.ospf.metric2 > old->u.ospf.metric2) return 0;
329 bbd76b42 Ondrej Filip
  }
330 d6a7e2fb Ondrej Filip
331 2e10a170 Ondrej Filip
  if (new->u.ospf.metric1 < old->u.ospf.metric1)
332
    return 1;
333 98ac6176 Ondrej Filip
334 2e10a170 Ondrej Filip
  return 0;                        /* Old is shorter or same */
335 bbd76b42 Ondrej Filip
}
336
337 00c1f79a Ondrej Filip
static int
338
ospf_rte_same(struct rte *new, struct rte *old)
339
{
340 5919c66e Martin Mares
  /* new->attrs == old->attrs always */
341
  return
342
    new->u.ospf.metric1 == old->u.ospf.metric1 &&
343
    new->u.ospf.metric2 == old->u.ospf.metric2 &&
344 c27b2449 Ondrej Zajicek
    new->u.ospf.tag == old->u.ospf.tag &&
345
    new->u.ospf.router_id == old->u.ospf.router_id;
346 5919c66e Martin Mares
}
347 00c1f79a Ondrej Filip
348 5919c66e Martin Mares
static ea_list *
349 2e10a170 Ondrej Filip
ospf_build_attrs(ea_list * next, struct linpool *pool, u32 m1, u32 m2,
350 c27b2449 Ondrej Zajicek
                 u32 tag, u32 rid)
351 5919c66e Martin Mares
{
352 2e10a170 Ondrej Filip
  struct ea_list *l =
353 c27b2449 Ondrej Zajicek
    lp_alloc(pool, sizeof(struct ea_list) + 4 * sizeof(eattr));
354 5919c66e Martin Mares
355
  l->next = next;
356
  l->flags = EALF_SORTED;
357 c27b2449 Ondrej Zajicek
  l->count = 4;
358 5919c66e Martin Mares
  l->attrs[0].id = EA_OSPF_METRIC1;
359
  l->attrs[0].flags = 0;
360
  l->attrs[0].type = EAF_TYPE_INT | EAF_TEMP;
361
  l->attrs[0].u.data = m1;
362
  l->attrs[1].id = EA_OSPF_METRIC2;
363
  l->attrs[1].flags = 0;
364
  l->attrs[1].type = EAF_TYPE_INT | EAF_TEMP;
365
  l->attrs[1].u.data = m2;
366
  l->attrs[2].id = EA_OSPF_TAG;
367
  l->attrs[2].flags = 0;
368
  l->attrs[2].type = EAF_TYPE_INT | EAF_TEMP;
369
  l->attrs[2].u.data = tag;
370 c27b2449 Ondrej Zajicek
  l->attrs[3].id = EA_OSPF_ROUTER_ID;
371
  l->attrs[3].flags = 0;
372 126683fe Ondrej Zajicek
  l->attrs[3].type = EAF_TYPE_ROUTER_ID | EAF_TEMP;
373 c27b2449 Ondrej Zajicek
  l->attrs[3].u.data = rid;
374 5919c66e Martin Mares
  return l;
375 00c1f79a Ondrej Filip
}
376
377 70a38319 Ondrej Filip
void
378 b9ed99f7 Ondrej Filip
schedule_net_lsa(struct ospf_iface *ifa)
379
{
380 be862406 Ondrej Zajicek
  struct proto *p = &ifa->oa->po->proto;
381
382
  OSPF_TRACE(D_EVENTS, "Scheduling network-LSA origination for iface %s", ifa->iface->name);
383 2e10a170 Ondrej Filip
  ifa->orignet = 1;
384 b9ed99f7 Ondrej Filip
}
385
386 be862406 Ondrej Zajicek
#ifdef OSPFv3
387
void
388
schedule_link_lsa(struct ospf_iface *ifa)
389
{
390
  struct proto *p = &ifa->oa->po->proto;
391
392
  OSPF_TRACE(D_EVENTS, "Scheduling link-LSA origination for iface %s", ifa->iface->name);
393
  ifa->origlink = 1;
394
}
395
#endif
396
397 b9ed99f7 Ondrej Filip
void
398 70a38319 Ondrej Filip
schedule_rt_lsa(struct ospf_area *oa)
399
{
400 b8f17cf1 Ondrej Filip
  struct proto *p = &oa->po->proto;
401 70a38319 Ondrej Filip
402 be862406 Ondrej Zajicek
  OSPF_TRACE(D_EVENTS, "Scheduling router-LSA origination for area %R", oa->areaid);
403 2e10a170 Ondrej Filip
  oa->origrt = 1;
404 70a38319 Ondrej Filip
}
405
406
void
407 b8f17cf1 Ondrej Filip
schedule_rtcalc(struct proto_ospf *po)
408 70a38319 Ondrej Filip
{
409 2e10a170 Ondrej Filip
  struct proto *p = &po->proto;
410 70a38319 Ondrej Filip
411 b8f17cf1 Ondrej Filip
  if (po->calcrt)
412 2e10a170 Ondrej Filip
    return;
413 3df1e804 Ondrej Filip
414 be862406 Ondrej Zajicek
  OSPF_TRACE(D_EVENTS, "Scheduling routing table calculation");
415 b8f17cf1 Ondrej Filip
  po->calcrt = 1;
416 70a38319 Ondrej Filip
}
417
418 f7574707 Ondrej Zajicek
static int
419
ospf_reload_routes(struct proto *p)
420
{
421
  struct proto_ospf *po = (struct proto_ospf *) p;
422
423
  if (po->calcrt != 2)
424
    OSPF_TRACE(D_EVENTS, "Scheduling routing table calculation with route reload");
425
426
  po->calcrt = 2;
427
428
  return 1;
429
}
430
431 6f58dc64 Ondrej Filip
/**
432 86c84d76 Ondrej Filip
 * area_disp - invokes origination of
433
 * router LSA and routing table cleanup
434
 * @oa: ospf area
435 6f58dc64 Ondrej Filip
 *
436 baa5dd6c Ondrej Filip
 * It invokes aging and when @ospf_area->origrt is set to 1, start
437 b49e6f5a Ondrej Zajicek
 * function for origination of router, network LSAs.
438 6f58dc64 Ondrej Filip
 */
439 70a38319 Ondrej Filip
void
440 3b16080c Ondrej Filip
area_disp(struct ospf_area *oa)
441 70a38319 Ondrej Filip
{
442 2e10a170 Ondrej Filip
  struct proto_ospf *po = oa->po;
443 78e2c6cc Ondrej Filip
  struct ospf_iface *ifa;
444 70a38319 Ondrej Filip
445
  /* Now try to originage rt_lsa */
446 2e10a170 Ondrej Filip
  if (oa->origrt)
447 b49e6f5a Ondrej Zajicek
    update_rt_lsa(oa);
448 78e2c6cc Ondrej Filip
449
  /* Now try to originate network LSA's */
450
  WALK_LIST(ifa, po->iface_list)
451
  {
452 b49e6f5a Ondrej Zajicek
#ifdef OSPFv3
453 be862406 Ondrej Zajicek
    /* Link LSA should be originated before Network LSA */
454 b49e6f5a Ondrej Zajicek
    if (ifa->origlink && (ifa->oa == oa))
455
      update_link_lsa(ifa);
456
#endif
457
458 3b16080c Ondrej Filip
    if (ifa->orignet && (ifa->oa == oa))
459 b49e6f5a Ondrej Zajicek
      update_net_lsa(ifa);
460 78e2c6cc Ondrej Filip
  }
461 b8f17cf1 Ondrej Filip
}
462
463 86c84d76 Ondrej Filip
/**
464 2918e610 Ondrej Zajicek
 * ospf_disp - invokes routing table calculation, aging and also area_disp()
465 86c84d76 Ondrej Filip
 * @timer: timer usually called every @proto_ospf->tick second, @timer->data
466
 * point to @proto_ospf
467
 */
468 b8f17cf1 Ondrej Filip
void
469
ospf_disp(timer * timer)
470
{
471
  struct proto_ospf *po = timer->data;
472 3b16080c Ondrej Filip
  struct ospf_area *oa;
473
474
  WALK_LIST(oa, po->area_list)
475
    area_disp(oa);
476 e59e310e Ondrej Filip
477 86c84d76 Ondrej Filip
  /* Age LSA DB */
478
  ospf_age(po);
479
480 e59e310e Ondrej Filip
  /* Calculate routing table */
481 b8f17cf1 Ondrej Filip
  if (po->calcrt)
482 b49e6f5a Ondrej Zajicek
    ospf_rt_spf(po);
483 70a38319 Ondrej Filip
}
484
485 b8f17cf1 Ondrej Filip
486
487 6f58dc64 Ondrej Filip
/**
488
 * ospf_import_control - accept or reject new route from nest's routing table
489
 * @p: current instance of protocol
490 6567e6cf Martin Mares
 * @new: the new route
491 baa5dd6c Ondrej Filip
 * @attrs: list of attributes
492
 * @pool: pool for allocation of attributes
493 6f58dc64 Ondrej Filip
 *
494 baa5dd6c Ondrej Filip
 * Its quite simple. It does not accept our own routes and leaves the decision on
495
 * import to the filters.
496 6f58dc64 Ondrej Filip
 */
497
498 2d5b9992 Ondrej Filip
int
499 2e10a170 Ondrej Filip
ospf_import_control(struct proto *p, rte ** new, ea_list ** attrs,
500
                    struct linpool *pool)
501 2d5b9992 Ondrej Filip
{
502 41b612c3 Ondrej Zajicek
  struct ospf_area *oa = ospf_main_area((struct proto_ospf *) p);
503 2e10a170 Ondrej Filip
  rte *e = *new;
504 2d5b9992 Ondrej Filip
505 094d2bdb Ondrej Zajicek
  if (e->attrs->src->proto == p)
506 2e10a170 Ondrej Filip
    return -1;                        /* Reject our own routes */
507 ba5e5940 Ondrej Zajicek
508 41b612c3 Ondrej Zajicek
  if (oa_is_stub(oa))
509
    return -1;                        /* Do not export routes to stub areas */
510
511 ba5e5940 Ondrej Zajicek
  eattr *ea = ea_find(e->attrs->eattrs, EA_GEN_IGP_METRIC);
512
  u32 m1 = (ea && (ea->u.data < LSINFINITY)) ? ea->u.data : LSINFINITY;
513
514
  *attrs = ospf_build_attrs(*attrs, pool, m1, 10000, 0, 0);
515 2e10a170 Ondrej Filip
  return 0;                        /* Leave decision to the filters */
516 5919c66e Martin Mares
}
517 2d5b9992 Ondrej Filip
518 5919c66e Martin Mares
struct ea_list *
519
ospf_make_tmp_attrs(struct rte *rt, struct linpool *pool)
520
{
521 2e10a170 Ondrej Filip
  return ospf_build_attrs(NULL, pool, rt->u.ospf.metric1, rt->u.ospf.metric2,
522 c27b2449 Ondrej Zajicek
                          rt->u.ospf.tag, rt->u.ospf.router_id);
523 5919c66e Martin Mares
}
524
525
void
526
ospf_store_tmp_attrs(struct rte *rt, struct ea_list *attrs)
527
{
528 8441f179 Martin Mares
  rt->u.ospf.metric1 = ea_get_int(attrs, EA_OSPF_METRIC1, LSINFINITY);
529
  rt->u.ospf.metric2 = ea_get_int(attrs, EA_OSPF_METRIC2, 10000);
530 2e10a170 Ondrej Filip
  rt->u.ospf.tag = ea_get_int(attrs, EA_OSPF_TAG, 0);
531 c27b2449 Ondrej Zajicek
  rt->u.ospf.router_id = ea_get_int(attrs, EA_OSPF_ROUTER_ID, 0);
532 2d5b9992 Ondrej Filip
}
533
534 6f58dc64 Ondrej Filip
/**
535 baa5dd6c Ondrej Filip
 * ospf_shutdown - Finish of OSPF instance
536 6f58dc64 Ondrej Filip
 * @p: current instance of protocol
537
 *
538 baa5dd6c Ondrej Filip
 * RFC does not define any action that should be taken before router
539 6f58dc64 Ondrej Filip
 * shutdown. To make my neighbors react as fast as possible, I send
540
 * them hello packet with empty neighbor list. They should start
541 baa5dd6c Ondrej Filip
 * their neighbor state machine with event %NEIGHBOR_1WAY.
542 6f58dc64 Ondrej Filip
 */
543
544 4bd28fb6 Ondrej Filip
static int
545
ospf_shutdown(struct proto *p)
546
{
547 2e10a170 Ondrej Filip
  struct proto_ospf *po = (struct proto_ospf *) p;
548 4bd28fb6 Ondrej Filip
  struct ospf_iface *ifa;
549 ee4880c8 Ondrej Filip
  OSPF_TRACE(D_EVENTS, "Shutdown requested");
550 4bd28fb6 Ondrej Filip
551
  /* And send to all my neighbors 1WAY */
552 353729f5 Ondrej Zajicek
  WALK_LIST(ifa, po->iface_list)
553 8e48831a Ondrej Zajicek
    ospf_iface_shutdown(ifa);
554 2e10a170 Ondrej Filip
555 57c574d8 Ondrej Zajicek
  /* Cleanup locked rta entries */
556
  FIB_WALK(&po->rtf, nftmp)
557
  {
558
    rta_free(((ort *) nftmp)->old_rta);
559
  }
560
  FIB_WALK_END;
561
562 4bd28fb6 Ondrej Filip
  return PS_DOWN;
563
}
564
565 3d15dcdb Ondrej Zajicek
static void
566 dca75fd7 Ondrej Zajicek
ospf_rt_notify(struct proto *p, rtable *tbl UNUSED, net * n, rte * new, rte * old UNUSED, ea_list * attrs)
567 e8085aba Ondrej Filip
{
568 2e10a170 Ondrej Filip
  struct proto_ospf *po = (struct proto_ospf *) p;
569 41b612c3 Ondrej Zajicek
  struct ospf_area *oa = ospf_main_area(po);
570 ed317862 Ondrej Zajicek
  ort *nf = (ort *) fib_get(&po->rtf, &n->n.prefix, n->n.pxlen);
571
  struct fib_node *fn = &nf->fn;
572 e8085aba Ondrej Filip
573 ed317862 Ondrej Zajicek
  if (!new)
574
  {
575
    if (fn->x1 != EXT_EXPORT)
576
      return;
577 e8085aba Ondrej Filip
578 bbcfd5a0 Ondrej Zajicek
    flush_ext_lsa(oa, fn, oa_is_nssa(oa));
579 ed317862 Ondrej Zajicek
580
    /* Old external route might blocked some NSSA translation */
581
    if (po->areano > 1)
582
      schedule_rtcalc(po);
583 2918e610 Ondrej Zajicek
584
    return;
585 ed317862 Ondrej Zajicek
  }
586
587
  /* Get route attributes */
588
  u32 m1 = ea_get_int(attrs, EA_OSPF_METRIC1, LSINFINITY);
589
  u32 m2 = ea_get_int(attrs, EA_OSPF_METRIC2, 10000);
590
  u32 metric = (m1 != LSINFINITY) ? m1 : (m2 | LSA_EXT_EBIT);
591
  u32 tag = ea_get_int(attrs, EA_OSPF_TAG, 0);
592
  ip_addr gw = IPA_NONE;
593
  // FIXME check for gw should be per ifa, not per iface
594
  if ((new->attrs->dest == RTD_ROUTER) &&
595
      ipa_nonzero(new->attrs->gw) &&
596
      !ipa_has_link_scope(new->attrs->gw) &&
597
      (ospf_iface_find((struct proto_ospf *) p, new->attrs->iface) != NULL))
598
    gw = new->attrs->gw;
599
600 2918e610 Ondrej Zajicek
  originate_ext_lsa(oa, fn, EXT_EXPORT, metric, gw, tag, 1);
601 e8085aba Ondrej Filip
}
602
603 4414d9a5 Ondrej Filip
static void
604 2e10a170 Ondrej Filip
ospf_get_status(struct proto *p, byte * buf)
605 4414d9a5 Ondrej Filip
{
606 2e10a170 Ondrej Filip
  struct proto_ospf *po = (struct proto_ospf *) p;
607 4414d9a5 Ondrej Filip
608 2e10a170 Ondrej Filip
  if (p->proto_state == PS_DOWN)
609
    buf[0] = 0;
610 4414d9a5 Ondrej Filip
  else
611
  {
612
    struct ospf_iface *ifa;
613
    struct ospf_neighbor *n;
614 2e10a170 Ondrej Filip
    int adj = 0;
615 4414d9a5 Ondrej Filip
616 2e10a170 Ondrej Filip
    WALK_LIST(ifa, po->iface_list)
617
      WALK_LIST(n, ifa->neigh_list) if (n->state == NEIGHBOR_FULL)
618
      adj = 1;
619 4414d9a5 Ondrej Filip
620 2e10a170 Ondrej Filip
    if (adj == 0)
621
      strcpy(buf, "Alone");
622
    else
623
      strcpy(buf, "Running");
624 4414d9a5 Ondrej Filip
  }
625
}
626
627 f7c0525e Ondrej Filip
static void
628 2e10a170 Ondrej Filip
ospf_get_route_info(rte * rte, byte * buf, ea_list * attrs UNUSED)
629 f7c0525e Ondrej Filip
{
630 98ac6176 Ondrej Filip
  char *type = "<bug>";
631 f7c0525e Ondrej Filip
632 98ac6176 Ondrej Filip
  switch(rte->attrs->source)
633 05dbc97b Ondrej Filip
  {
634 98ac6176 Ondrej Filip
    case RTS_OSPF:
635
      type = "I";
636
      break;
637
    case RTS_OSPF_IA:
638
      type = "IA";
639
      break;
640
    case RTS_OSPF_EXT1:
641
      type = "E1";
642
      break;
643
    case RTS_OSPF_EXT2:
644
      type = "E2";
645
      break;
646 05dbc97b Ondrej Filip
  }
647 98ac6176 Ondrej Filip
648
  buf += bsprintf(buf, " %s", type);
649 d6a7e2fb Ondrej Filip
  buf += bsprintf(buf, " (%d/%d", rte->pref, rte->u.ospf.metric1);
650 98ac6176 Ondrej Filip
  if (rte->attrs->source == RTS_OSPF_EXT2)
651 2e10a170 Ondrej Filip
    buf += bsprintf(buf, "/%d", rte->u.ospf.metric2);
652 d6a7e2fb Ondrej Filip
  buf += bsprintf(buf, ")");
653 41b612c3 Ondrej Zajicek
  if ((rte->attrs->source == RTS_OSPF_EXT1 || rte->attrs->source == RTS_OSPF_EXT2) && rte->u.ospf.tag)
654 a7a3a0a3 Ondrej Filip
  {
655 23df5e4c Martin Mares
    buf += bsprintf(buf, " [%x]", rte->u.ospf.tag);
656 a7a3a0a3 Ondrej Filip
  }
657 c27b2449 Ondrej Zajicek
  if (rte->u.ospf.router_id)
658
    buf += bsprintf(buf, " [%R]", rte->u.ospf.router_id);
659 f7c0525e Ondrej Filip
}
660
661 5919c66e Martin Mares
static int
662 aebe06b4 Ondrej Zajicek
ospf_get_attr(eattr * a, byte * buf, int buflen UNUSED)
663 5919c66e Martin Mares
{
664
  switch (a->id)
665 2e10a170 Ondrej Filip
  {
666
  case EA_OSPF_METRIC1:
667
    bsprintf(buf, "metric1");
668
    return GA_NAME;
669
  case EA_OSPF_METRIC2:
670
    bsprintf(buf, "metric2");
671
    return GA_NAME;
672
  case EA_OSPF_TAG:
673 126683fe Ondrej Zajicek
    bsprintf(buf, "tag: 0x%08x", a->u.data);
674 2e10a170 Ondrej Filip
    return GA_FULL;
675 126683fe Ondrej Zajicek
  case EA_OSPF_ROUTER_ID:
676
    bsprintf(buf, "router_id");
677
    return GA_NAME;
678 2e10a170 Ondrej Filip
  default:
679
    return GA_UNKNOWN;
680
  }
681 5919c66e Martin Mares
}
682 6f58dc64 Ondrej Filip
683 8e48831a Ondrej Zajicek
static void
684
ospf_area_reconfigure(struct ospf_area *oa, struct ospf_area_config *nac)
685 edc34dc9 Ondrej Filip
{
686 8e48831a Ondrej Zajicek
  oa->ac = nac;
687 2918e610 Ondrej Zajicek
688
  // FIXME better area type reconfiguration
689
#ifdef OSPFv2
690
  oa->options = nac->type;
691
#else /* OSPFv3 */
692
  oa->options = OPT_R | nac->type | OPT_V6;
693
#endif
694
  if (oa_is_nssa(oa) && (oa->po->areano > 1))
695
    oa->po->ebit = 1;
696 8e48831a Ondrej Zajicek
697
  ospf_ifaces_reconfigure(oa, nac);
698
699
  /* Handle net_list */
700
  fib_free(&oa->net_fib);
701 ed317862 Ondrej Zajicek
  fib_free(&oa->enet_fib);
702 8e48831a Ondrej Zajicek
  add_area_nets(oa, nac);
703
704
  /* No need to handle stubnet_list */
705
706
  oa->marked = 0;
707
  schedule_rt_lsa(oa);
708 edc34dc9 Ondrej Filip
}
709 5919c66e Martin Mares
710 6f58dc64 Ondrej Filip
/**
711
 * ospf_reconfigure - reconfiguration hook
712
 * @p: current instance of protocol (with old configuration)
713
 * @c: new configuration requested by user
714
 *
715 baa5dd6c Ondrej Filip
 * This hook tries to be a little bit intelligent. Instance of OSPF
716 6f58dc64 Ondrej Filip
 * will survive change of many constants like hello interval,
717 baa5dd6c Ondrej Filip
 * password change, addition or deletion of some neighbor on
718 6f58dc64 Ondrej Filip
 * nonbroadcast network, cost of interface, etc.
719
 */
720 80787d41 Ondrej Filip
static int
721
ospf_reconfigure(struct proto *p, struct proto_config *c)
722
{
723 8e48831a Ondrej Zajicek
  struct proto_ospf *po = (struct proto_ospf *) p;
724 2e10a170 Ondrej Filip
  struct ospf_config *old = (struct ospf_config *) (p->cf);
725
  struct ospf_config *new = (struct ospf_config *) c;
726 8e48831a Ondrej Zajicek
  struct ospf_area_config *nac;
727
  struct ospf_area *oa, *oax;
728
  struct ospf_iface *ifa, *ifx;
729
  struct ospf_iface_patt *ip;
730
731 d82fc18d Ondrej Zajicek
  if (po->rfc1583 != new->rfc1583)
732
    return 0;
733
734 8e48831a Ondrej Zajicek
  if (old->abr != new->abr)
735
    return 0;
736 edc34dc9 Ondrej Filip
737 57c574d8 Ondrej Zajicek
  po->ecmp = new->ecmp;
738 8e48831a Ondrej Zajicek
  po->tick = new->tick;
739 52d61a84 Ondrej Filip
  po->disp_timer->recurrent = po->tick;
740
  tm_start(po->disp_timer, 1);
741
742 8e48831a Ondrej Zajicek
  /* Mark all areas and ifaces */
743
  WALK_LIST(oa, po->area_list)
744
    oa->marked = 1;
745 edc34dc9 Ondrej Filip
746 8e48831a Ondrej Zajicek
  WALK_LIST(ifa, po->iface_list)
747
    ifa->marked = 1;
748 2e10a170 Ondrej Filip
749 8e48831a Ondrej Zajicek
  /* Add and update areas */
750
  WALK_LIST(nac, new->area_list)
751 edc34dc9 Ondrej Filip
  {
752 8e48831a Ondrej Zajicek
    oa = ospf_find_area(po, nac->areaid);
753
    if (oa)
754
      ospf_area_reconfigure(oa, nac);
755
    else
756
      ospf_area_add(po, nac, 1);
757
  }
758 c926eee7 Ondrej Filip
759 8e48831a Ondrej Zajicek
  /* Add and update vlinks */
760
  WALK_LIST(ip, new->vlink_list)
761
  {
762
    ifa = ospf_find_vlink(po, ip->voa, ip->vid);
763
    if (ifa)
764
      ospf_iface_reconfigure(ifa, ip);
765
    else
766
      ospf_iface_new(po->backbone, NULL, ip);
767
  }
768 edc34dc9 Ondrej Filip
769 8e48831a Ondrej Zajicek
  /* Delete remaining ifaces and areas */
770
  WALK_LIST_DELSAFE(ifa, ifx, po->iface_list)
771
    if (ifa->marked)
772 edc34dc9 Ondrej Filip
    {
773 8e48831a Ondrej Zajicek
      ospf_iface_shutdown(ifa);
774
      ospf_iface_remove(ifa);
775 edc34dc9 Ondrej Filip
    }
776
777 8e48831a Ondrej Zajicek
  WALK_LIST_DELSAFE(oa, oax, po->area_list)
778
    if (oa->marked)
779
      ospf_area_remove(oa);
780 edc34dc9 Ondrej Filip
781 8e48831a Ondrej Zajicek
  schedule_rtcalc(po);
782
  
783
  return 1;
784 80787d41 Ondrej Filip
}
785 a783e259 Ondrej Filip
786 8e48831a Ondrej Zajicek
787 a783e259 Ondrej Filip
void
788 c4f0f014 Ondrej Filip
ospf_sh_neigh(struct proto *p, char *iff)
789 a783e259 Ondrej Filip
{
790 e81b440f Ondrej Zajicek
  struct ospf_iface *ifa = NULL;
791 a783e259 Ondrej Filip
  struct ospf_neighbor *n;
792 2e10a170 Ondrej Filip
  struct proto_ospf *po = (struct proto_ospf *) p;
793 3488634c Ondrej Filip
794 2e10a170 Ondrej Filip
  if (p->proto_state != PS_UP)
795 3488634c Ondrej Filip
  {
796 2e10a170 Ondrej Filip
    cli_msg(-1013, "%s: is not up", p->name);
797
    cli_msg(0, "");
798 3488634c Ondrej Filip
    return;
799
  }
800 2e10a170 Ondrej Filip
801
  cli_msg(-1013, "%s:", p->name);
802 cf31112f Ondrej Zajicek
  cli_msg(-1013, "%-12s\t%3s\t%-15s\t%-5s\t%-10s %-12s", "Router ID", "Pri",
803
          "     State", "DTime", "Interface", "Router IP");
804 2e10a170 Ondrej Filip
  WALK_LIST(ifa, po->iface_list)
805 610bb3cf Ondrej Zajicek
    if ((iff == NULL) || patmatch(iff, ifa->iface->name))
806
      WALK_LIST(n, ifa->neigh_list)
807
        ospf_sh_neigh_info(n);
808 2e10a170 Ondrej Filip
  cli_msg(0, "");
809 a783e259 Ondrej Filip
}
810 4ab4e977 Ondrej Filip
811
void
812
ospf_sh(struct proto *p)
813
{
814
  struct ospf_area *oa;
815 2e10a170 Ondrej Filip
  struct proto_ospf *po = (struct proto_ospf *) p;
816 4ab4e977 Ondrej Filip
  struct ospf_iface *ifa;
817
  struct ospf_neighbor *n;
818 98ac6176 Ondrej Filip
  int ifano, nno, adjno, firstfib;
819
  struct area_net *anet;
820 4ab4e977 Ondrej Filip
821 2e10a170 Ondrej Filip
  if (p->proto_state != PS_UP)
822 3488634c Ondrej Filip
  {
823 2e10a170 Ondrej Filip
    cli_msg(-1014, "%s: is not up", p->name);
824
    cli_msg(0, "");
825 3488634c Ondrej Filip
    return;
826
  }
827
828 2e10a170 Ondrej Filip
  cli_msg(-1014, "%s:", p->name);
829 3b16080c Ondrej Filip
  cli_msg(-1014, "RFC1583 compatibility: %s", (po->rfc1583 ? "enable" : "disabled"));
830
  cli_msg(-1014, "RT scheduler tick: %d", po->tick);
831 2e10a170 Ondrej Filip
  cli_msg(-1014, "Number of areas: %u", po->areano);
832 86c84d76 Ondrej Filip
  cli_msg(-1014, "Number of LSAs in DB:\t%u", po->gr->hash_entries);
833 2e10a170 Ondrej Filip
834
  WALK_LIST(oa, po->area_list)
835 4ab4e977 Ondrej Filip
  {
836 3aab39f5 Ondrej Zajicek
    cli_msg(-1014, "\tArea: %R (%u) %s", oa->areaid, oa->areaid,
837 2e10a170 Ondrej Filip
            oa->areaid == 0 ? "[BACKBONE]" : "");
838
    ifano = 0;
839
    nno = 0;
840
    adjno = 0;
841 4ab4e977 Ondrej Filip
    WALK_LIST(ifa, po->iface_list)
842
    {
843 2e10a170 Ondrej Filip
      if (oa == ifa->oa)
844 4ab4e977 Ondrej Filip
      {
845 3b16080c Ondrej Filip
        ifano++;
846
        WALK_LIST(n, ifa->neigh_list)
847
        {
848
          nno++;
849
          if (n->state == NEIGHBOR_FULL)
850
            adjno++;
851
        }
852 4ab4e977 Ondrej Filip
      }
853
    }
854 ed317862 Ondrej Zajicek
855
    cli_msg(-1014, "\t\tStub:\t%s", oa_is_stub(oa) ? "Yes" : "No");
856
    cli_msg(-1014, "\t\tNSSA:\t%s", oa_is_nssa(oa) ? "Yes" : "No");
857 3b16080c Ondrej Filip
    cli_msg(-1014, "\t\tTransit:\t%s", oa->trcap ? "Yes" : "No");
858 ed317862 Ondrej Zajicek
859
    if (oa_is_nssa(oa))
860
      cli_msg(-1014, "\t\tNSSA translation:\t%s%s", oa->translate ? "Yes" : "No",
861
              oa->translate == TRANS_WAIT ? " (run down)" : "");
862 2e10a170 Ondrej Filip
    cli_msg(-1014, "\t\tNumber of interfaces:\t%u", ifano);
863
    cli_msg(-1014, "\t\tNumber of neighbors:\t%u", nno);
864
    cli_msg(-1014, "\t\tNumber of adjacent neighbors:\t%u", adjno);
865 98ac6176 Ondrej Filip
866
    firstfib = 1;
867
    FIB_WALK(&oa->net_fib, nftmp)
868 c926eee7 Ondrej Filip
    {
869 98ac6176 Ondrej Filip
      anet = (struct area_net *) nftmp;
870
      if(firstfib)
871 c926eee7 Ondrej Filip
      {
872 98ac6176 Ondrej Filip
        cli_msg(-1014, "\t\tArea networks:");
873
        firstfib = 0;
874 c926eee7 Ondrej Filip
      }
875 98ac6176 Ondrej Filip
      cli_msg(-1014, "\t\t\t%1I/%u\t%s\t%s", anet->fn.prefix, anet->fn.pxlen,
876
                anet->hidden ? "Hidden" : "Advertise", anet->active ? "Active" : "");
877 c926eee7 Ondrej Filip
    }
878 98ac6176 Ondrej Filip
    FIB_WALK_END;
879 ed317862 Ondrej Zajicek
880
    firstfib = 1;
881
    FIB_WALK(&oa->enet_fib, nftmp)
882
    {
883
      anet = (struct area_net *) nftmp;
884
      if(firstfib)
885
      {
886
        cli_msg(-1014, "\t\tArea external networks:");
887
        firstfib = 0;
888
      }
889
      cli_msg(-1014, "\t\t\t%1I/%u\t%s\t%s", anet->fn.prefix, anet->fn.pxlen,
890
                anet->hidden ? "Hidden" : "Advertise", anet->active ? "Active" : "");
891
    }
892
    FIB_WALK_END;
893
894 4ab4e977 Ondrej Filip
  }
895 2e10a170 Ondrej Filip
  cli_msg(0, "");
896 4ab4e977 Ondrej Filip
}
897
898 c4f0f014 Ondrej Filip
void
899
ospf_sh_iface(struct proto *p, char *iff)
900
{
901 2e10a170 Ondrej Filip
  struct proto_ospf *po = (struct proto_ospf *) p;
902 e81b440f Ondrej Zajicek
  struct ospf_iface *ifa = NULL;
903 c4f0f014 Ondrej Filip
904 2e10a170 Ondrej Filip
  if (p->proto_state != PS_UP)
905 3488634c Ondrej Filip
  {
906 2e10a170 Ondrej Filip
    cli_msg(-1015, "%s: is not up", p->name);
907
    cli_msg(0, "");
908 3488634c Ondrej Filip
    return;
909
  }
910
911 2e10a170 Ondrej Filip
  cli_msg(-1015, "%s:", p->name);
912 610bb3cf Ondrej Zajicek
  WALK_LIST(ifa, po->iface_list)
913
    if ((iff == NULL) || patmatch(iff, ifa->iface->name))
914
      ospf_iface_info(ifa);
915 2e10a170 Ondrej Filip
  cli_msg(0, "");
916 c4f0f014 Ondrej Filip
}
917
918 b66abe8e Ondrej Zajicek
/* lsa_compare_for_state() - Compare function for 'show ospf state'
919
 *
920
 * First we want to separate network-LSAs and other LSAs (because network-LSAs
921 a3b70dc4 Ondrej Zajicek
 * will be presented as network nodes and other LSAs together as router nodes)
922
 * Network-LSAs are sorted according to network prefix, other LSAs are sorted
923
 * according to originating router id (to get all LSA needed to represent one
924
 * router node together). Then, according to LSA type, ID and age.
925 b66abe8e Ondrej Zajicek
 *
926
 * For OSPFv3, we have to handle also Prefix-LSAs. We would like to put each
927
 * immediately after the referenced LSA. We will make faked LSA based on ref_
928
 * values
929 a3b70dc4 Ondrej Zajicek
 */
930 b66abe8e Ondrej Zajicek
931 98955023 Ondrej Zajicek
#ifdef OSPFv3
932
933 b66abe8e Ondrej Zajicek
static struct ospf_lsa_header *
934
fake_lsa_from_prefix_lsa(struct ospf_lsa_header *dst, struct ospf_lsa_header *src,
935
                         struct ospf_lsa_prefix *px)
936
{
937
  dst->age = src->age;
938
  dst->type = px->ref_type;
939
  dst->id = px->ref_id;
940
  dst->rt = px->ref_rt;
941
  dst->sn = src->sn;
942
943
  return dst;
944
}
945
946 98955023 Ondrej Zajicek
#endif
947
948 a3b70dc4 Ondrej Zajicek
static int
949 b66abe8e Ondrej Zajicek
lsa_compare_for_state(const void *p1, const void *p2)
950 a3b70dc4 Ondrej Zajicek
{
951
  struct top_hash_entry * he1 = * (struct top_hash_entry **) p1;
952
  struct top_hash_entry * he2 = * (struct top_hash_entry **) p2;
953
  struct ospf_lsa_header *lsa1 = &(he1->lsa);
954
  struct ospf_lsa_header *lsa2 = &(he2->lsa);
955 b66abe8e Ondrej Zajicek
956
  if (he1->domain != he2->domain)
957
    return he1->domain - he2->domain;
958
959
#ifdef OSPFv3
960
  struct ospf_lsa_header lsatmp1, lsatmp2;
961
962
  int px1 = (lsa1->type == LSA_T_PREFIX);
963
  int px2 = (lsa2->type == LSA_T_PREFIX);
964
965
  if (px1)
966
    lsa1 = fake_lsa_from_prefix_lsa(&lsatmp1, lsa1, he1->lsa_body);
967
968
  if (px2)
969
    lsa2 = fake_lsa_from_prefix_lsa(&lsatmp2, lsa2, he2->lsa_body);
970
#endif
971
972 a3b70dc4 Ondrej Zajicek
  int nt1 = (lsa1->type == LSA_T_NET);
973
  int nt2 = (lsa2->type == LSA_T_NET);
974
975
  if (nt1 != nt2)
976
    return nt1 - nt2;
977
978
  if (nt1)
979
  {
980 b66abe8e Ondrej Zajicek
#ifdef OSPFv3
981
    /* In OSPFv3, neworks are named base on ID of DR */
982
    if (lsa1->rt != lsa2->rt)
983
      return lsa1->rt - lsa2->rt;
984
#endif
985
986
    /* For OSPFv2, this is IP of the network,
987
       for OSPFv3, this is interface ID */
988 a3b70dc4 Ondrej Zajicek
    if (lsa1->id != lsa2->id)
989
      return lsa1->id - lsa2->id;
990 b66abe8e Ondrej Zajicek
991
#ifdef OSPFv3
992
    if (px1 != px2)
993
      return px1 - px2;
994
#endif
995
996
    return lsa1->sn - lsa2->sn;
997 a3b70dc4 Ondrej Zajicek
  }
998
  else 
999 b66abe8e Ondrej Zajicek
  {
1000
    if (lsa1->rt != lsa2->rt)
1001
      return lsa1->rt - lsa2->rt;
1002
1003
    if (lsa1->type != lsa2->type)
1004
      return lsa1->type - lsa2->type;
1005 a3b70dc4 Ondrej Zajicek
  
1006 b66abe8e Ondrej Zajicek
    if (lsa1->id != lsa2->id)
1007
      return lsa1->id - lsa2->id;
1008
1009
#ifdef OSPFv3
1010
    if (px1 != px2)
1011
      return px1 - px2;
1012
#endif
1013 a3b70dc4 Ondrej Zajicek
  
1014 b66abe8e Ondrej Zajicek
    return lsa1->sn - lsa2->sn;
1015
  }
1016 a3b70dc4 Ondrej Zajicek
}
1017 b66abe8e Ondrej Zajicek
1018 0ea8fb4a Ondrej Zajicek
static int
1019
ext_compare_for_state(const void *p1, const void *p2)
1020
{
1021
  struct top_hash_entry * he1 = * (struct top_hash_entry **) p1;
1022
  struct top_hash_entry * he2 = * (struct top_hash_entry **) p2;
1023
  struct ospf_lsa_header *lsa1 = &(he1->lsa);
1024
  struct ospf_lsa_header *lsa2 = &(he2->lsa);
1025
1026
  if (lsa1->rt != lsa2->rt)
1027
    return lsa1->rt - lsa2->rt;
1028
1029
  if (lsa1->id != lsa2->id)
1030
    return lsa1->id - lsa2->id;
1031
 
1032
  return lsa1->sn - lsa2->sn;
1033
}
1034
1035
static inline void
1036
show_lsa_distance(struct top_hash_entry *he)
1037
{
1038
  if (he->color == INSPF)
1039
    cli_msg(-1016, "\t\tdistance %u", he->dist);
1040
  else
1041
    cli_msg(-1016, "\t\tunreachable");
1042
}
1043
1044 a3b70dc4 Ondrej Zajicek
static inline void
1045 0ea8fb4a Ondrej Zajicek
show_lsa_router(struct proto_ospf *po, struct top_hash_entry *he, int first, int verbose)
1046 a3b70dc4 Ondrej Zajicek
{
1047
  struct ospf_lsa_header *lsa = &(he->lsa);
1048
  struct ospf_lsa_rt *rt = he->lsa_body;
1049
  struct ospf_lsa_rt_link *rr = (struct ospf_lsa_rt_link *) (rt + 1);
1050 c3226991 Ondrej Zajicek
  int max = lsa_rt_count(lsa);
1051 b66abe8e Ondrej Zajicek
  int i;
1052
1053 0ea8fb4a Ondrej Zajicek
  if (first)
1054
  {
1055
    cli_msg(-1016, "");
1056
    cli_msg(-1016, "\trouter %R", he->lsa.rt);
1057
    show_lsa_distance(he);
1058
  }
1059
1060
1061 b66abe8e Ondrej Zajicek
  for (i = 0; i < max; i++)
1062
    if (rr[i].type == LSART_VLNK)
1063
      cli_msg(-1016, "\t\tvlink %R metric %u", rr[i].id, rr[i].metric);
1064 a3b70dc4 Ondrej Zajicek
1065 c3226991 Ondrej Zajicek
  for (i = 0; i < max; i++)
1066 a3b70dc4 Ondrej Zajicek
    if (rr[i].type == LSART_PTP)
1067 b66abe8e Ondrej Zajicek
      cli_msg(-1016, "\t\trouter %R metric %u", rr[i].id, rr[i].metric);
1068 a3b70dc4 Ondrej Zajicek
1069 c3226991 Ondrej Zajicek
  for (i = 0; i < max; i++)
1070 a3b70dc4 Ondrej Zajicek
    if (rr[i].type == LSART_NET)
1071
    {
1072 b66abe8e Ondrej Zajicek
#ifdef OSPFv2
1073
      struct top_hash_entry *net_he = ospf_hash_find_net(po->gr, he->domain, rr[i].id);
1074
1075 a3b70dc4 Ondrej Zajicek
      if (net_he)
1076
      {
1077
        struct ospf_lsa_header *net_lsa = &(net_he->lsa);
1078
        struct ospf_lsa_net *net_ln = net_he->lsa_body;
1079 b66abe8e Ondrej Zajicek
1080
        cli_msg(-1016, "\t\tnetwork %I/%d metric %u", 
1081
                ipa_and(ipa_from_u32(net_lsa->id), net_ln->netmask),
1082
                ipa_mklen(net_ln->netmask), rr[i].metric);
1083 a3b70dc4 Ondrej Zajicek
      }
1084
      else
1085 b66abe8e Ondrej Zajicek
        cli_msg(-1016, "\t\tnetwork [%R] metric %u", rr[i].id, rr[i].metric);
1086
1087
#else /* OSPFv3 */
1088
      cli_msg(-1016, "\t\tnetwork [%R-%u] metric %u", rr[i].id, rr[i].nif, rr[i].metric);
1089
#endif
1090 a3b70dc4 Ondrej Zajicek
    }
1091
1092 b66abe8e Ondrej Zajicek
#ifdef OSPFv2
1093 0ea8fb4a Ondrej Zajicek
  if (!verbose)
1094
    return;
1095
1096 c3226991 Ondrej Zajicek
  for (i = 0; i < max; i++)
1097 a3b70dc4 Ondrej Zajicek
    if (rr[i].type == LSART_STUB)
1098 b66abe8e Ondrej Zajicek
      cli_msg(-1016, "\t\tstubnet %I/%d metric %u", ipa_from_u32(rr[i].id),
1099
              ipa_mklen(ipa_from_u32(rr[i].data)), rr[i].metric);
1100
#endif
1101 a3b70dc4 Ondrej Zajicek
}
1102
1103
static inline void
1104
show_lsa_network(struct top_hash_entry *he)
1105
{
1106
  struct ospf_lsa_header *lsa = &(he->lsa);
1107
  struct ospf_lsa_net *ln = he->lsa_body;
1108
  u32 i;
1109
1110 b66abe8e Ondrej Zajicek
#ifdef OSPFv2
1111 a3b70dc4 Ondrej Zajicek
  cli_msg(-1016, "");
1112
  cli_msg(-1016, "\tnetwork %I/%d", ipa_and(ipa_from_u32(lsa->id), ln->netmask), ipa_mklen(ln->netmask));
1113 3aab39f5 Ondrej Zajicek
  cli_msg(-1016, "\t\tdr %R", lsa->rt);
1114 b66abe8e Ondrej Zajicek
#else /* OSPFv3 */
1115
  cli_msg(-1016, "");
1116
  cli_msg(-1016, "\tnetwork [%R-%u]", lsa->rt, lsa->id);
1117
#endif
1118 a3b70dc4 Ondrej Zajicek
1119 0ea8fb4a Ondrej Zajicek
  show_lsa_distance(he);
1120
1121 c3226991 Ondrej Zajicek
  for (i = 0; i < lsa_net_count(lsa); i++)
1122 b66abe8e Ondrej Zajicek
    cli_msg(-1016, "\t\trouter %R", ln->routers[i]);
1123 a3b70dc4 Ondrej Zajicek
}
1124
1125
static inline void
1126
show_lsa_sum_net(struct top_hash_entry *he)
1127
{
1128 b66abe8e Ondrej Zajicek
  ip_addr ip;
1129
  int pxlen;
1130 a3b70dc4 Ondrej Zajicek
1131 b66abe8e Ondrej Zajicek
#ifdef OSPFv2
1132
  struct ospf_lsa_sum *ls = he->lsa_body;
1133
  pxlen = ipa_mklen(ls->netmask);
1134
  ip = ipa_and(ipa_from_u32(he->lsa.id), ls->netmask);
1135
#else /* OSPFv3 */
1136
  u8 pxopts;
1137
  u16 rest;
1138
  struct ospf_lsa_sum_net *ls = he->lsa_body;
1139
  lsa_get_ipv6_prefix(ls->prefix, &ip, &pxlen, &pxopts, &rest);
1140
#endif
1141
1142 353729f5 Ondrej Zajicek
  cli_msg(-1016, "\t\txnetwork %I/%d metric %u", ip, pxlen, ls->metric);
1143 a3b70dc4 Ondrej Zajicek
}
1144
1145
static inline void
1146
show_lsa_sum_rt(struct top_hash_entry *he)
1147
{
1148 d494df63 Ondrej Zajicek
  u32 dst_rid;
1149 b66abe8e Ondrej Zajicek
1150
#ifdef OSPFv2
1151
  struct ospf_lsa_sum *ls = he->lsa_body;
1152
  dst_rid = he->lsa.id;
1153 d494df63 Ondrej Zajicek
  // options = 0;
1154 b66abe8e Ondrej Zajicek
#else /* OSPFv3 */
1155
  struct ospf_lsa_sum_rt *ls = he->lsa_body;
1156
  dst_rid = ls->drid; 
1157 d494df63 Ondrej Zajicek
  // options = ls->options & OPTIONS_MASK;
1158 b66abe8e Ondrej Zajicek
#endif
1159
1160 353729f5 Ondrej Zajicek
  cli_msg(-1016, "\t\txrouter %R metric %u", dst_rid, ls->metric);
1161 a3b70dc4 Ondrej Zajicek
}
1162
1163
1164
static inline void
1165
show_lsa_external(struct top_hash_entry *he)
1166
{
1167
  struct ospf_lsa_ext *ext = he->lsa_body;
1168
  char str_via[STD_ADDRESS_P_LENGTH + 8] = "";
1169
  char str_tag[16] = "";
1170 b66abe8e Ondrej Zajicek
  ip_addr ip, rt_fwaddr;
1171
  int pxlen, ebit, rt_fwaddr_valid;
1172
  u32 rt_tag, rt_metric;
1173
1174 41b612c3 Ondrej Zajicek
  if (he->lsa.type == LSA_T_EXT)
1175
    he->domain = 0; /* Unmark the LSA */
1176 0ea8fb4a Ondrej Zajicek
1177 b66abe8e Ondrej Zajicek
  rt_metric = ext->metric & METRIC_MASK;
1178
  ebit = ext->metric & LSA_EXT_EBIT;
1179
#ifdef OSPFv2
1180 ff2857b0 Ondrej Zajicek
  ip = ipa_and(ipa_from_u32(he->lsa.id), ext->netmask);
1181 b66abe8e Ondrej Zajicek
  pxlen = ipa_mklen(ext->netmask);
1182
  rt_fwaddr = ext->fwaddr;
1183
  rt_fwaddr_valid = !ipa_equal(rt_fwaddr, IPA_NONE);
1184
  rt_tag = ext->tag;
1185
#else /* OSPFv3 */
1186
  u8 pxopts;
1187
  u16 rest;
1188
  u32 *buf = ext->rest;
1189
  buf = lsa_get_ipv6_prefix(buf, &ip, &pxlen, &pxopts, &rest);
1190
1191
  rt_fwaddr_valid = ext->metric & LSA_EXT_FBIT;
1192
  if (rt_fwaddr_valid)
1193
    buf = lsa_get_ipv6_addr(buf, &rt_fwaddr);
1194
  else 
1195
    rt_fwaddr = IPA_NONE;
1196
1197
  if (ext->metric & LSA_EXT_TBIT)
1198
    rt_tag = *buf++;
1199
  else
1200
    rt_tag = 0;
1201
#endif
1202 a3b70dc4 Ondrej Zajicek
  
1203 b66abe8e Ondrej Zajicek
  if (rt_fwaddr_valid)
1204
    bsprintf(str_via, " via %I", rt_fwaddr);
1205 a3b70dc4 Ondrej Zajicek
1206 b66abe8e Ondrej Zajicek
  if (rt_tag)
1207
    bsprintf(str_tag, " tag %08x", rt_tag);
1208 a3b70dc4 Ondrej Zajicek
1209 41b612c3 Ondrej Zajicek
  cli_msg(-1016, "\t\t%s %I/%d metric%s %u%s%s",
1210
          (he->lsa.type == LSA_T_NSSA) ? "nssa-ext" : "external",
1211
          ip, pxlen, ebit ? "2" : "", rt_metric, str_via, str_tag);
1212 a3b70dc4 Ondrej Zajicek
}
1213
1214 b66abe8e Ondrej Zajicek
#ifdef OSPFv3
1215
static inline void
1216 0ea8fb4a Ondrej Zajicek
show_lsa_prefix(struct top_hash_entry *he, struct ospf_lsa_header *cnode)
1217 b66abe8e Ondrej Zajicek
{
1218
  struct ospf_lsa_prefix *px = he->lsa_body;
1219
  ip_addr pxa;
1220
  int pxlen;
1221
  u8 pxopts;
1222
  u16 metric;
1223
  u32 *buf;
1224
  int i;
1225
1226 0ea8fb4a Ondrej Zajicek
  /* We check whether given prefix-LSA is related to the current node */
1227
  if ((px->ref_type != cnode->type) || (px->ref_rt != cnode->rt))
1228
    return;
1229
1230
  if ((px->ref_type == LSA_T_RT) && (px->ref_id != 0))
1231
    return;
1232
1233
  if ((px->ref_type == LSA_T_NET) && (px->ref_id != cnode->id))
1234 b66abe8e Ondrej Zajicek
    return;
1235
1236
  buf = px->rest;
1237
  for (i = 0; i < px->pxcount; i++)
1238
    {
1239
      buf = lsa_get_ipv6_prefix(buf, &pxa, &pxlen, &pxopts, &metric);
1240
1241
      if (px->ref_type == LSA_T_RT)
1242
        cli_msg(-1016, "\t\tstubnet %I/%d metric %u", pxa, pxlen, metric);
1243
      else
1244
        cli_msg(-1016, "\t\taddress %I/%d", pxa, pxlen);
1245
    }
1246
}
1247
#endif
1248
1249 a3b70dc4 Ondrej Zajicek
void
1250 0ea8fb4a Ondrej Zajicek
ospf_sh_state(struct proto *p, int verbose, int reachable)
1251 a3b70dc4 Ondrej Zajicek
{
1252
  struct proto_ospf *po = (struct proto_ospf *) p;
1253 0ea8fb4a Ondrej Zajicek
  struct ospf_lsa_header *cnode = NULL;
1254
  int num = po->gr->hash_entries;
1255
  unsigned int i, ix, j1, j2, jx;
1256 a3b70dc4 Ondrej Zajicek
  u32 last_area = 0xFFFFFFFF;
1257 b49e6f5a Ondrej Zajicek
1258 a3b70dc4 Ondrej Zajicek
  if (p->proto_state != PS_UP)
1259
  {
1260
    cli_msg(-1016, "%s: is not up", p->name);
1261
    cli_msg(0, "");
1262
    return;
1263
  }
1264
1265 0ea8fb4a Ondrej Zajicek
  /* We store interesting area-scoped LSAs in array hea and 
1266
     global-scoped (LSA_T_EXT) LSAs in array hex */
1267
1268
  struct top_hash_entry *hea[num];
1269
  struct top_hash_entry *hex[verbose ? num : 0];
1270 a3b70dc4 Ondrej Zajicek
  struct top_hash_entry *he;
1271
1272 0ea8fb4a Ondrej Zajicek
  j1 = j2 = jx = 0;
1273 b66abe8e Ondrej Zajicek
  WALK_SLIST(he, po->lsal)
1274
  {
1275
    int accept;
1276
1277
    switch (he->lsa.type)
1278
      {
1279
      case LSA_T_RT:
1280
      case LSA_T_NET:
1281
        accept = 1;
1282
        break;
1283
1284
      case LSA_T_SUM_NET:
1285
      case LSA_T_SUM_RT:
1286 41b612c3 Ondrej Zajicek
      case LSA_T_NSSA:
1287 b66abe8e Ondrej Zajicek
#ifdef OSPFv3
1288
      case LSA_T_PREFIX:
1289
#endif
1290
        accept = verbose;
1291
        break;
1292
1293 0ea8fb4a Ondrej Zajicek
      case LSA_T_EXT:
1294
        if (verbose)
1295
        {
1296
          he->domain = 1; /* Abuse domain field to mark the LSA */
1297
          hex[jx++] = he;
1298
        }
1299 b66abe8e Ondrej Zajicek
      default:
1300
        accept = 0;
1301
      }
1302 a3b70dc4 Ondrej Zajicek
1303 b66abe8e Ondrej Zajicek
    if (accept)
1304
      hea[j1++] = he;
1305
    else
1306
      j2++;
1307
  }
1308
1309 0ea8fb4a Ondrej Zajicek
  if ((j1 + j2) != num)
1310 a3b70dc4 Ondrej Zajicek
    die("Fatal mismatch");
1311
1312 b66abe8e Ondrej Zajicek
  qsort(hea, j1, sizeof(struct top_hash_entry *), lsa_compare_for_state);
1313 0ea8fb4a Ondrej Zajicek
  qsort(hex, jx, sizeof(struct top_hash_entry *), ext_compare_for_state);
1314 a3b70dc4 Ondrej Zajicek
1315 0ea8fb4a Ondrej Zajicek
  /*
1316
   * This code is a bit tricky, we have a primary LSAs (router and
1317
   * network) that are presented as a node, and secondary LSAs that
1318
   * are presented as a part of a primary node. cnode represents an
1319
   * currently opened node (whose header was presented). The LSAs are
1320
   * sorted to get secondary LSAs just after related primary LSA (if
1321
   * available). We present secondary LSAs only when related primary
1322
   * LSA is opened.
1323
   *
1324
   * AS-external LSAs are stored separately as they might be presented
1325
   * several times (for each area when related ASBR is opened). When
1326
   * the node is closed, related external routes are presented. We
1327
   * also have to take into account that in OSPFv3, there might be
1328
   * more router-LSAs and only the first should be considered as a
1329
   * primary. This is handled by not closing old router-LSA when next
1330
   * one is processed (which is not opened because there is already
1331
   * one opened).
1332
   */
1333
1334
  ix = 0;
1335 b66abe8e Ondrej Zajicek
  for (i = 0; i < j1; i++)
1336 a3b70dc4 Ondrej Zajicek
  {
1337 0ea8fb4a Ondrej Zajicek
    he = hea[i];
1338 a3b70dc4 Ondrej Zajicek
1339 0ea8fb4a Ondrej Zajicek
    /* If there is no opened node, we open the LSA (if appropriate) or skip to the next one */
1340
    if (!cnode)
1341 a3b70dc4 Ondrej Zajicek
    {
1342 0ea8fb4a Ondrej Zajicek
      if (((he->lsa.type == LSA_T_RT) || (he->lsa.type == LSA_T_NET))
1343
          && ((he->color == INSPF) || !reachable))
1344
      {
1345
        cnode = &(he->lsa);
1346
1347
        if (he->domain != last_area)
1348
        {
1349
          cli_msg(-1016, "");
1350
          cli_msg(-1016, "area %R", he->domain);
1351
          last_area = he->domain;
1352
          ix = 0;
1353
        }
1354
      }
1355
      else
1356
        continue;
1357 a3b70dc4 Ondrej Zajicek
    }
1358
1359 0ea8fb4a Ondrej Zajicek
    ASSERT(cnode && (he->domain == last_area) && (he->lsa.rt == cnode->rt));
1360
1361
    switch (he->lsa.type)
1362 a3b70dc4 Ondrej Zajicek
    {
1363
      case LSA_T_RT:
1364 0ea8fb4a Ondrej Zajicek
        show_lsa_router(po, he, he->lsa.id == cnode->id, verbose);
1365 a3b70dc4 Ondrej Zajicek
        break;
1366
1367
      case LSA_T_NET:
1368 0ea8fb4a Ondrej Zajicek
        show_lsa_network(he);
1369 a3b70dc4 Ondrej Zajicek
        break;
1370
1371
      case LSA_T_SUM_NET:
1372 0ea8fb4a Ondrej Zajicek
        if (cnode->type == LSA_T_RT)
1373
          show_lsa_sum_net(he);
1374 a3b70dc4 Ondrej Zajicek
        break;
1375
1376
      case LSA_T_SUM_RT:
1377 0ea8fb4a Ondrej Zajicek
        if (cnode->type == LSA_T_RT)
1378
          show_lsa_sum_rt(he);
1379 a3b70dc4 Ondrej Zajicek
        break;
1380 b66abe8e Ondrej Zajicek
1381
#ifdef OSPFv3
1382
      case LSA_T_PREFIX:
1383 0ea8fb4a Ondrej Zajicek
        show_lsa_prefix(he, cnode);
1384 b66abe8e Ondrej Zajicek
        break;
1385
#endif
1386 0ea8fb4a Ondrej Zajicek
1387
      case LSA_T_EXT:
1388 41b612c3 Ondrej Zajicek
      case LSA_T_NSSA:
1389 0ea8fb4a Ondrej Zajicek
        show_lsa_external(he);
1390
        break;
1391 a3b70dc4 Ondrej Zajicek
    }
1392 b66abe8e Ondrej Zajicek
1393 0ea8fb4a Ondrej Zajicek
    /* In these cases, we close the current node */
1394
    if ((i+1 == j1)
1395
        || (hea[i+1]->domain != last_area)
1396
        || (hea[i+1]->lsa.rt != cnode->rt)
1397
        || (hea[i+1]->lsa.type == LSA_T_NET))
1398
    {
1399
      while ((ix < jx) && (hex[ix]->lsa.rt < cnode->rt))
1400
        ix++;
1401
1402
      while ((ix < jx) && (hex[ix]->lsa.rt == cnode->rt))
1403
        show_lsa_external(hex[ix++]);
1404
1405
      cnode = NULL;
1406
    }
1407
  }
1408
1409
  int hdr = 0;
1410
  u32 last_rt = 0xFFFFFFFF;
1411
  for (ix = 0; ix < jx; ix++)
1412
  {
1413
    he = hex[ix];
1414
1415
    /* If it is still marked, we show it now. */
1416
    if (he->domain)
1417
    {
1418
      he->domain = 0;
1419
1420
      if ((he->color != INSPF) && reachable)
1421
        continue;
1422
1423
      if (!hdr)
1424
      {
1425
        cli_msg(-1016, "");
1426
        cli_msg(-1016, "other ASBRs");
1427
        hdr = 1;
1428
      }
1429
1430
      if (he->lsa.rt != last_rt)
1431
      {
1432
        cli_msg(-1016, "");
1433
        cli_msg(-1016, "\trouter %R", he->lsa.rt);
1434
        last_rt = he->lsa.rt;
1435
      }
1436
1437
      show_lsa_external(he);
1438
    }
1439 a3b70dc4 Ondrej Zajicek
  }
1440 0ea8fb4a Ondrej Zajicek
1441 a3b70dc4 Ondrej Zajicek
  cli_msg(0, "");
1442
}
1443
1444 98899244 Ondrej Zajicek
1445
static int
1446
lsa_compare_for_lsadb(const void *p1, const void *p2)
1447
{
1448
  struct top_hash_entry * he1 = * (struct top_hash_entry **) p1;
1449
  struct top_hash_entry * he2 = * (struct top_hash_entry **) p2;
1450
  struct ospf_lsa_header *lsa1 = &(he1->lsa);
1451
  struct ospf_lsa_header *lsa2 = &(he2->lsa);
1452
  int sc1 = LSA_SCOPE(lsa1);
1453
  int sc2 = LSA_SCOPE(lsa2);
1454
1455
  if (sc1 != sc2)
1456
    return sc2 - sc1;
1457
1458
  if (he1->domain != he2->domain)
1459
    return he1->domain - he2->domain;
1460
1461
  if (lsa1->rt != lsa2->rt)
1462
    return lsa1->rt - lsa2->rt;
1463
  
1464
  if (lsa1->id != lsa2->id)
1465
    return lsa1->id - lsa2->id;
1466
1467
  if (lsa1->type != lsa2->type)
1468
    return lsa1->type - lsa2->type;
1469
1470
  return lsa1->sn - lsa2->sn;
1471
}
1472
1473
void
1474 20ab192b Ondrej Zajicek
ospf_sh_lsadb(struct lsadb_show_data *ld)
1475 98899244 Ondrej Zajicek
{
1476 20ab192b Ondrej Zajicek
  struct proto *p = proto_get_named(ld->name, &proto_ospf);
1477 98899244 Ondrej Zajicek
  struct proto_ospf *po = (struct proto_ospf *) p;
1478 0ea8fb4a Ondrej Zajicek
  int num = po->gr->hash_entries;
1479 98899244 Ondrej Zajicek
  unsigned int i, j;
1480
  int last_dscope = -1;
1481
  u32 last_domain = 0;
1482
1483
  if (p->proto_state != PS_UP)
1484
  {
1485
    cli_msg(-1017, "%s: is not up", p->name);
1486
    cli_msg(0, "");
1487
    return;
1488
  }
1489
1490 20ab192b Ondrej Zajicek
  if (ld->router == SH_ROUTER_SELF)
1491
    ld->router = po->router_id;
1492
1493 0ea8fb4a Ondrej Zajicek
  struct top_hash_entry *hea[num];
1494 98899244 Ondrej Zajicek
  struct top_hash_entry *he;
1495
1496
  j = 0;
1497
  WALK_SLIST(he, po->lsal)
1498
    hea[j++] = he;
1499
1500 0ea8fb4a Ondrej Zajicek
  if (j != num)
1501 98899244 Ondrej Zajicek
    die("Fatal mismatch");
1502
1503
  qsort(hea, j, sizeof(struct top_hash_entry *), lsa_compare_for_lsadb);
1504
1505
  for (i = 0; i < j; i++)
1506
  {
1507
    struct ospf_lsa_header *lsa = &(hea[i]->lsa);
1508
    int dscope = LSA_SCOPE(lsa);
1509 20ab192b Ondrej Zajicek
1510
    if (ld->scope && (dscope != (ld->scope & 0xf000)))
1511
      continue;
1512
1513
    if ((ld->scope == LSA_SCOPE_AREA) && (hea[i]->domain != ld->area))
1514
      continue;
1515
1516
    /* Ignore high nibble */
1517
    if (ld->type && ((lsa->type & 0x0fff) != (ld->type & 0x0fff)))
1518
      continue;
1519
1520
    if (ld->lsid && (lsa->id != ld->lsid))
1521
      continue;
1522
1523
    if (ld->router && (lsa->rt != ld->router))
1524
      continue;
1525 98899244 Ondrej Zajicek
    
1526
    if ((dscope != last_dscope) || (hea[i]->domain != last_domain))
1527
    {
1528
      cli_msg(-1017, "");
1529
      switch (dscope)
1530
      {
1531
        case LSA_SCOPE_AS:
1532
          cli_msg(-1017, "Global");
1533
          break;
1534
        case LSA_SCOPE_AREA:
1535
          cli_msg(-1017, "Area %R", hea[i]->domain);
1536
          break;
1537
#ifdef OSPFv3
1538
        case LSA_SCOPE_LINK:
1539 ff2857b0 Ondrej Zajicek
          {
1540
            struct iface *ifa = if_find_by_index(hea[i]->domain);
1541
            cli_msg(-1017, "Link %s", (ifa != NULL) ? ifa->name : "?");
1542
          }
1543 98899244 Ondrej Zajicek
          break;
1544
#endif
1545
      }
1546
      cli_msg(-1017, "");
1547 34a877cc Ondrej Zajicek
      cli_msg(-1017," Type   LS ID           Router           Age  Sequence  Checksum");
1548 98899244 Ondrej Zajicek
1549
      last_dscope = dscope;
1550
      last_domain = hea[i]->domain;
1551
    }
1552
1553
1554 34a877cc Ondrej Zajicek
    cli_msg(-1017," %04x  %-15R %-15R %5u  %08x    %04x",
1555
            lsa->type, lsa->id, lsa->rt, lsa->age, lsa->sn, lsa->checksum);
1556 98899244 Ondrej Zajicek
  }
1557
  cli_msg(0, "");
1558
}
1559
1560
1561 80787d41 Ondrej Filip
struct protocol proto_ospf = {
1562 b8113a5e Ondrej Zajicek
  name:                        "OSPF",
1563
  template:                "ospf%d",
1564
  attr_class:                EAP_OSPF,
1565 39c028e9 Ondrej Zajicek
  preference:                DEF_PREF_OSPF,
1566 b8113a5e Ondrej Zajicek
  init:                        ospf_init,
1567
  dump:                        ospf_dump,
1568
  start:                ospf_start,
1569
  shutdown:                ospf_shutdown,
1570
  reconfigure:                ospf_reconfigure,
1571
  get_status:                ospf_get_status,
1572
  get_attr:                ospf_get_attr,
1573
  get_route_info:        ospf_get_route_info
1574
  // show_proto_info:        ospf_sh
1575 80787d41 Ondrej Filip
};