Statistics
| Branch: | Revision:

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

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