Statistics
| Branch: | Revision:

iof-bird-daemon / proto / ospf / ospf.c @ 600998fc

History | View | Annotate | Download (38.4 KB)

1 c1f8dc91 Ondrej Filip
/*
2
 *        BIRD -- OSPF
3
 *
4 98ac6176 Ondrej Filip
 *        (c) 1999--2004 Ondrej Filip <feela@network.cz>
5 70945cb6 Ondrej Zajicek
 *        (c) 2009--2014 Ondrej Zajicek <santiago@crfreenet.org>
6
 *        (c) 2009--2014 CZ.NIC z.s.p.o.
7 c1f8dc91 Ondrej Filip
 *
8
 *        Can be freely distributed and used under the terms of the GNU GPL.
9
 */
10
11 89755a86 Ondrej Filip
/**
12
 * DOC: Open Shortest Path First (OSPF)
13 742029eb Ondrej Zajicek
 *
14 a7a7372a Ondrej Zajicek
 * The OSPF protocol is quite complicated and its complex implemenation is split
15
 * to many files. In |ospf.c|, you will find mainly the interface for
16
 * communication with the core (e.g., reconfiguration hooks, shutdown and
17
 * initialisation and so on). File |iface.c| contains the interface state
18
 * machine and functions for allocation and deallocation of OSPF's interface
19
 * data structures. Source |neighbor.c| includes the neighbor state machine and
20
 * functions for election of Designated Router and Backup Designated router. In
21
 * |packet.c|, you will find various functions for sending and receiving generic
22
 * OSPF packets. There are also routines for authentication and checksumming.
23
 * In |hello.c|, there are routines for sending and receiving of hello packets
24
 * as well as functions for maintaining wait times and the inactivity timer.
25
 * Files |lsreq.c|, |lsack.c|, |dbdes.c| contain functions for sending and
26
 * receiving of link-state requests, link-state acknowledgements and database
27
 * descriptions respectively.  In |lsupd.c|, there are functions for sending and
28
 * receiving of link-state updates and also the flooding algorithm. Source
29
 * |topology.c| is a place where routines for searching LSAs in the link-state
30
 * database, adding and deleting them reside, there also are functions for
31
 * originating of various types of LSAs (router LSA, net LSA, external LSA).
32
 * File |rt.c| contains routines for calculating the routing table. |lsalib.c|
33
 * is a set of various functions for working with the LSAs (endianity
34
 * conversions, calculation of checksum etc.).
35 89755a86 Ondrej Filip
 *
36 a7a7372a Ondrej Zajicek
 * One instance of the protocol is able to hold LSA databases for multiple OSPF
37
 * areas, to exchange routing information between multiple neighbors and to
38
 * calculate the routing tables. The core structure is &ospf_proto to which
39
 * multiple &ospf_area and &ospf_iface structures are connected. &ospf_proto is
40
 * also connected to &top_hash_graph which is a dynamic hashing structure that
41
 * describes the link-state database. It allows fast search, addition and
42
 * 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 a7a7372a Ondrej Zajicek
 * In OSPFv2 specification, it is implied that there is one IP prefix for each
46
 * physical network/interface (unless it is an ptp link). But in modern systems,
47
 * there might be more independent IP prefixes associated with an interface.  To
48
 * handle this situation, we have one &ospf_iface for each active IP prefix
49
 * (instead for each active iface); This behaves like virtual interface for the
50
 * purpose of OSPF.  If we receive packet, we associate it with a proper virtual
51
 * interface mainly according to its source address.
52 48cff379 Ondrej Zajicek
 *
53 a7a7372a Ondrej Zajicek
 * OSPF keeps one socket per &ospf_iface. This allows us (compared to one socket
54
 * approach) to evade problems with a limit of multicast groups per socket and
55
 * with sending multicast packets to appropriate interface in a portable way.
56
 * The socket is associated with underlying physical iface and should not
57
 * receive packets received on other ifaces (unfortunately, this is not true on
58
 * BSD). Generally, one packet can be received by more sockets (for example, if
59
 * there are more &ospf_iface on one physical iface), therefore we explicitly
60
 * filter received packets according to src/dst IP address and received iface.
61 48cff379 Ondrej Zajicek
 *
62 a7a7372a Ondrej Zajicek
 * Vlinks are implemented using particularly degenerate form of &ospf_iface,
63
 * which has several exceptions: it does not have its iface or socket (it copies
64
 * these from 'parent' &ospf_iface) and it is present in iface list even when
65
 * down (it is not freed in ospf_iface_down()).
66 48cff379 Ondrej Zajicek
 *
67 86c84d76 Ondrej Filip
 * The heart beat of ospf is ospf_disp(). It is called at regular intervals
68 a7a7372a Ondrej Zajicek
 * (&ospf_proto->tick). It is responsible for aging and flushing of LSAs in the
69
 * database, updating topology information in LSAs and for routing table
70
 * calculation.
71 742029eb Ondrej Zajicek
 *
72 a7a7372a Ondrej Zajicek
 * To every &ospf_iface, we connect one or more &ospf_neighbor's -- a structure
73
 * containing many timers and queues for building adjacency and for exchange of
74
 * routing messages.
75 89755a86 Ondrej Filip
 *
76 a7a7372a Ondrej Zajicek
 * BIRD's OSPF implementation respects RFC2328 in every detail, but some of
77
 * internal algorithms do differ. The RFC recommends making a snapshot of the
78
 * link-state database when a new adjacency is forming and sending the database
79
 * description packets based on the information in this snapshot. The database
80
 * can be quite large in some networks, so rather we walk through a &slist
81
 * structure which allows us to continue even if the actual LSA we were working
82
 * with is deleted. New LSAs are added at the tail of this &slist.
83 89755a86 Ondrej Filip
 *
84 a7a7372a Ondrej Zajicek
 * We also do not keep a separate OSPF routing table, because the core helps us
85
 * by being able to recognize when a route is updated to an identical one and it
86
 * suppresses the update automatically. Due to this, we can flush all the routes
87
 * we have recalculated and also those we have deleted to the core's routing
88
 * table and the core will take care of the rest. This simplifies the process
89 725270cb Martin Mares
 * and conserves memory.
90 178a197a Ondrej Zajicek
 *
91
 * Supported standards:
92
 * - RFC 2328 - main OSPFv2 standard
93
 * - RFC 5340 - main OSPFv3 standard
94
 * - RFC 3101 - OSPFv2 NSSA areas
95
 * - RFC 6549 - OSPFv2 multi-instance extensions
96
 * - RFC 6987 - OSPF stub router advertisement
97 89755a86 Ondrej Filip
 */
98
99 a3b70dc4 Ondrej Zajicek
#include <stdlib.h>
100 c1f8dc91 Ondrej Filip
#include "ospf.h"
101
102 70945cb6 Ondrej Zajicek
static int ospf_import_control(struct proto *P, rte **new, ea_list **attrs, struct linpool *pool);
103
static struct ea_list *ospf_make_tmp_attrs(struct rte *rt, struct linpool *pool);
104
static void ospf_store_tmp_attrs(struct rte *rt, struct ea_list *attrs);
105
static int ospf_reload_routes(struct proto *P);
106 d83faf8d Martin Mares
static int ospf_rte_better(struct rte *new, struct rte *old);
107
static int ospf_rte_same(struct rte *new, struct rte *old);
108 b8f17cf1 Ondrej Filip
static void ospf_disp(timer *timer);
109
110 98ac6176 Ondrej Filip
111
static void
112
add_area_nets(struct ospf_area *oa, struct ospf_area_config *ac)
113
{
114 70945cb6 Ondrej Zajicek
  struct ospf_proto *p = oa->po;
115
  struct area_net_config *anc;
116
  struct area_net *an;
117 98ac6176 Ondrej Filip
118 fe9f1a6d Ondrej Zajicek (work)
  fib_init(&oa->net_fib,  p->p.pool, ospf_is_v2(p) ? NET_IP4 : NET_IP6,
119
           sizeof(struct area_net), OFFSETOF(struct area_net, fn), 0, NULL);
120
  fib_init(&oa->enet_fib, p->p.pool, ospf_is_v2(p) ? NET_IP4 : NET_IP6,
121
           sizeof(struct area_net), OFFSETOF(struct area_net, fn), 0, NULL);
122 98ac6176 Ondrej Filip
123 70945cb6 Ondrej Zajicek
  WALK_LIST(anc, ac->net_list)
124
  {
125 d44e686e Ondrej Zajicek (work)
    an = fib_get(&oa->net_fib, &anc->prefix);
126 70945cb6 Ondrej Zajicek
    an->hidden = anc->hidden;
127
  }
128 ed317862 Ondrej Zajicek
129 70945cb6 Ondrej Zajicek
  WALK_LIST(anc, ac->enet_list)
130
  {
131 d44e686e Ondrej Zajicek (work)
    an = fib_get(&oa->enet_fib, &anc->prefix);
132 70945cb6 Ondrej Zajicek
    an->hidden = anc->hidden;
133
    an->tag = anc->tag;
134
  }
135 98ac6176 Ondrej Filip
}
136 d83faf8d Martin Mares
137 8e48831a Ondrej Zajicek
static void
138 a7a7372a Ondrej Zajicek
ospf_area_add(struct ospf_proto *p, struct ospf_area_config *ac)
139 8e48831a Ondrej Zajicek
{
140
  struct ospf_area *oa;
141
142
  OSPF_TRACE(D_EVENTS, "Adding area %R", ac->areaid);
143
144 70945cb6 Ondrej Zajicek
  oa = mb_allocz(p->p.pool, sizeof(struct ospf_area));
145
  add_tail(&p->area_list, NODE oa);
146
  p->areano++;
147 8e48831a Ondrej Zajicek
148
  oa->ac = ac;
149
  oa->areaid = ac->areaid;
150
  oa->rt = NULL;
151 70945cb6 Ondrej Zajicek
  oa->po = p;
152 fe9f1a6d Ondrej Zajicek (work)
  fib_init(&oa->rtr, p->p.pool, NET_IP4, sizeof(ort), OFFSETOF(ort, fn), 0, NULL);
153 8e48831a Ondrej Zajicek
  add_area_nets(oa, ac);
154
155
  if (oa->areaid == 0)
156 70945cb6 Ondrej Zajicek
    p->backbone = oa;
157 8e48831a Ondrej Zajicek
158 70945cb6 Ondrej Zajicek
  if (ospf_is_v2(p))
159
    oa->options = ac->type;
160
  else
161
    oa->options = ac->type | OPT_V6 | (p->stub_router ? 0 : OPT_R);
162 a7a7372a Ondrej Zajicek
163
  ospf_notify_rt_lsa(oa);
164 70945cb6 Ondrej Zajicek
}
165 8e48831a Ondrej Zajicek
166 70945cb6 Ondrej Zajicek
static void
167
ospf_flush_area(struct ospf_proto *p, u32 areaid)
168
{
169
  struct top_hash_entry *en;
170 2918e610 Ondrej Zajicek
171 70945cb6 Ondrej Zajicek
  WALK_SLIST(en, p->lsal)
172
    if ((LSA_SCOPE(en->lsa_type) == LSA_SCOPE_AREA) && (en->domain == areaid))
173
      ospf_flush_lsa(p, en);
174 8e48831a Ondrej Zajicek
}
175
176
static void
177
ospf_area_remove(struct ospf_area *oa)
178
{
179 70945cb6 Ondrej Zajicek
  struct ospf_proto *p = oa->po;
180 8e48831a Ondrej Zajicek
  OSPF_TRACE(D_EVENTS, "Removing area %R", oa->areaid);
181
182
  /* We suppose that interfaces are already removed */
183 70945cb6 Ondrej Zajicek
  ospf_flush_area(p, oa->areaid);
184 742029eb Ondrej Zajicek
185 8e48831a Ondrej Zajicek
  fib_free(&oa->rtr);
186
  fib_free(&oa->net_fib);
187 ed317862 Ondrej Zajicek
  fib_free(&oa->enet_fib);
188 8e48831a Ondrej Zajicek
189 4160a9dd Ondrej Zajicek
  if (oa->translator_timer)
190
    rfree(oa->translator_timer);
191
192 70945cb6 Ondrej Zajicek
  p->areano--;
193 8e48831a Ondrej Zajicek
  rem_node(NODE oa);
194
  mb_free(oa);
195
}
196
197
198
struct ospf_area *
199 70945cb6 Ondrej Zajicek
ospf_find_area(struct ospf_proto *p, u32 aid)
200 8e48831a Ondrej Zajicek
{
201
  struct ospf_area *oa;
202 70945cb6 Ondrej Zajicek
  WALK_LIST(oa, p->area_list)
203 8e48831a Ondrej Zajicek
    if (((struct ospf_area *) oa)->areaid == aid)
204
      return oa;
205
  return NULL;
206
}
207
208
static struct ospf_iface *
209 70945cb6 Ondrej Zajicek
ospf_find_vlink(struct ospf_proto *p, u32 voa, u32 vid)
210 8e48831a Ondrej Zajicek
{
211
  struct ospf_iface *ifa;
212 742029eb Ondrej Zajicek
  WALK_LIST(ifa, p->iface_list)
213 8e48831a Ondrej Zajicek
    if ((ifa->type == OSPF_IT_VLINK) && (ifa->voa->areaid == voa) && (ifa->vid == vid))
214
      return ifa;
215
  return NULL;
216
}
217
218 c1f8dc91 Ondrej Filip
static int
219 70945cb6 Ondrej Zajicek
ospf_start(struct proto *P)
220 c1f8dc91 Ondrej Filip
{
221 70945cb6 Ondrej Zajicek
  struct ospf_proto *p = (struct ospf_proto *) P;
222
  struct ospf_config *c = (struct ospf_config *) (P->cf);
223 51cff78b Ondrej Filip
  struct ospf_area_config *ac;
224 f14032ef Ondrej Filip
225 70945cb6 Ondrej Zajicek
  p->router_id = proto_get_router_id(P->cf);
226
  p->ospf2 = c->ospf2;
227
  p->rfc1583 = c->rfc1583;
228
  p->stub_router = c->stub_router;
229
  p->merge_external = c->merge_external;
230
  p->asbr = c->asbr;
231
  p->ecmp = c->ecmp;
232
  p->tick = c->tick;
233 6f8bbaa1 Ondrej Zajicek
  p->disp_timer = tm_new_set(P->pool, ospf_disp, p, 0, p->tick);
234 70945cb6 Ondrej Zajicek
  tm_start(p->disp_timer, 1);
235
  p->lsab_size = 256;
236
  p->lsab_used = 0;
237
  p->lsab = mb_alloc(P->pool, p->lsab_size);
238
  p->nhpool = lp_new(P->pool, 12*sizeof(struct mpnh));
239
  init_list(&(p->iface_list));
240
  init_list(&(p->area_list));
241 fe9f1a6d Ondrej Zajicek (work)
  fib_init(&p->rtf, P->pool, p->ospf2 ? NET_IP4 : NET_IP6,
242
           sizeof(ort), OFFSETOF(ort, fn), 0, NULL);
243 70945cb6 Ondrej Zajicek
  p->areano = 0;
244 a7a7372a Ondrej Zajicek
  p->gr = ospf_top_new(p, P->pool);
245 70945cb6 Ondrej Zajicek
  s_init_list(&(p->lsal));
246 5b1a92e6 Ondrej Filip
247 6f8bbaa1 Ondrej Zajicek
  p->flood_event = ev_new(P->pool);
248
  p->flood_event->hook = ospf_flood_event;
249
  p->flood_event->data = p;
250
251 f8fefde3 Ondrej Zajicek
  p->log_pkt_tbf = (struct tbf){ .rate = 1, .burst = 5 };
252
  p->log_lsa_tbf = (struct tbf){ .rate = 4, .burst = 20 };
253
254 2e10a170 Ondrej Filip
  WALK_LIST(ac, c->area_list)
255 a7a7372a Ondrej Zajicek
    ospf_area_add(p, ac);
256 6384c7d7 Ondrej Zajicek
257 48e5f32d Ondrej Zajicek
  if (c->abr)
258 70945cb6 Ondrej Zajicek
    ospf_open_vlink_sk(p);
259 48e5f32d Ondrej Zajicek
260 8e48831a Ondrej Zajicek
  /* Add all virtual links */
261
  struct ospf_iface_patt *ic;
262
  WALK_LIST(ic, c->vlink_list)
263 70945cb6 Ondrej Zajicek
    ospf_iface_new_vlink(p, ic);
264 6384c7d7 Ondrej Zajicek
265 c1f8dc91 Ondrej Filip
  return PS_UP;
266
}
267
268
static void
269 70945cb6 Ondrej Zajicek
ospf_dump(struct proto *P)
270 c1f8dc91 Ondrej Filip
{
271 70945cb6 Ondrej Zajicek
  struct ospf_proto *p = (struct ospf_proto *) P;
272 b332fcdf Ondrej Filip
  struct ospf_iface *ifa;
273
  struct ospf_neighbor *n;
274 c1f8dc91 Ondrej Filip
275 70945cb6 Ondrej Zajicek
  OSPF_TRACE(D_EVENTS, "Area number: %d", p->areano);
276 b332fcdf Ondrej Filip
277 70945cb6 Ondrej Zajicek
  WALK_LIST(ifa, p->iface_list)
278 b332fcdf Ondrej Filip
  {
279 48e5f32d Ondrej Zajicek
    OSPF_TRACE(D_EVENTS, "Interface: %s", ifa->ifname);
280 f14032ef Ondrej Filip
    OSPF_TRACE(D_EVENTS, "state: %u", ifa->state);
281 3aab39f5 Ondrej Zajicek
    OSPF_TRACE(D_EVENTS, "DR:  %R", ifa->drid);
282
    OSPF_TRACE(D_EVENTS, "BDR: %R", ifa->bdrid);
283 b332fcdf Ondrej Filip
    WALK_LIST(n, ifa->neigh_list)
284
    {
285 3aab39f5 Ondrej Zajicek
      OSPF_TRACE(D_EVENTS, "  neighbor %R in state %u", n->rid, n->state);
286 b332fcdf Ondrej Filip
    }
287
  }
288 163b2073 Ondrej Filip
289 e81b440f Ondrej Zajicek
  /*
290 86c84d76 Ondrej Filip
  OSPF_TRACE(D_EVENTS, "LSA graph dump start:");
291 70945cb6 Ondrej Zajicek
  ospf_top_dump(p->gr, p);
292 86c84d76 Ondrej Filip
  OSPF_TRACE(D_EVENTS, "LSA graph dump finished");
293 e81b440f Ondrej Zajicek
  */
294 c6c56264 Ondrej Filip
  neigh_dump_all();
295 c1f8dc91 Ondrej Filip
}
296
297
static struct proto *
298
ospf_init(struct proto_config *c)
299
{
300 742029eb Ondrej Zajicek
  struct ospf_config *oc = (struct ospf_config *) c;
301 70945cb6 Ondrej Zajicek
  struct proto *P = proto_new(c, sizeof(struct ospf_proto));
302
303
  P->accept_ra_types = RA_OPTIMAL;
304
  P->rt_notify = ospf_rt_notify;
305
  P->if_notify = ospf_if_notify;
306
  P->ifa_notify = oc->ospf2 ? ospf_ifa_notify2 : ospf_ifa_notify3;
307
  P->import_control = ospf_import_control;
308
  P->reload_routes = ospf_reload_routes;
309
  P->make_tmp_attrs = ospf_make_tmp_attrs;
310
  P->store_tmp_attrs = ospf_store_tmp_attrs;
311
  P->rte_better = ospf_rte_better;
312
  P->rte_same = ospf_rte_same;
313
314
  return P;
315 c1f8dc91 Ondrej Filip
}
316
317 bbd76b42 Ondrej Filip
/* If new is better return 1 */
318
static int
319
ospf_rte_better(struct rte *new, struct rte *old)
320
{
321 2e10a170 Ondrej Filip
  if (new->u.ospf.metric1 == LSINFINITY)
322
    return 0;
323 bbd76b42 Ondrej Filip
324 98ac6176 Ondrej Filip
  if(new->attrs->source < old->attrs->source) return 1;
325
  if(new->attrs->source > old->attrs->source) return 0;
326 2e10a170 Ondrej Filip
327 98ac6176 Ondrej Filip
  if(new->attrs->source == RTS_OSPF_EXT2)
328 bbd76b42 Ondrej Filip
  {
329 98ac6176 Ondrej Filip
    if(new->u.ospf.metric2 < old->u.ospf.metric2) return 1;
330
    if(new->u.ospf.metric2 > old->u.ospf.metric2) return 0;
331 bbd76b42 Ondrej Filip
  }
332 d6a7e2fb Ondrej Filip
333 2e10a170 Ondrej Filip
  if (new->u.ospf.metric1 < old->u.ospf.metric1)
334
    return 1;
335 98ac6176 Ondrej Filip
336 2e10a170 Ondrej Filip
  return 0;                        /* Old is shorter or same */
337 bbd76b42 Ondrej Filip
}
338
339 00c1f79a Ondrej Filip
static int
340
ospf_rte_same(struct rte *new, struct rte *old)
341
{
342 5919c66e Martin Mares
  /* new->attrs == old->attrs always */
343
  return
344
    new->u.ospf.metric1 == old->u.ospf.metric1 &&
345
    new->u.ospf.metric2 == old->u.ospf.metric2 &&
346 c27b2449 Ondrej Zajicek
    new->u.ospf.tag == old->u.ospf.tag &&
347
    new->u.ospf.router_id == old->u.ospf.router_id;
348 5919c66e Martin Mares
}
349 00c1f79a Ondrej Filip
350 5919c66e Martin Mares
static ea_list *
351 2e10a170 Ondrej Filip
ospf_build_attrs(ea_list * next, struct linpool *pool, u32 m1, u32 m2,
352 c27b2449 Ondrej Zajicek
                 u32 tag, u32 rid)
353 5919c66e Martin Mares
{
354 2e10a170 Ondrej Filip
  struct ea_list *l =
355 c27b2449 Ondrej Zajicek
    lp_alloc(pool, sizeof(struct ea_list) + 4 * sizeof(eattr));
356 5919c66e Martin Mares
357
  l->next = next;
358
  l->flags = EALF_SORTED;
359 c27b2449 Ondrej Zajicek
  l->count = 4;
360 5919c66e Martin Mares
  l->attrs[0].id = EA_OSPF_METRIC1;
361
  l->attrs[0].flags = 0;
362
  l->attrs[0].type = EAF_TYPE_INT | EAF_TEMP;
363
  l->attrs[0].u.data = m1;
364
  l->attrs[1].id = EA_OSPF_METRIC2;
365
  l->attrs[1].flags = 0;
366
  l->attrs[1].type = EAF_TYPE_INT | EAF_TEMP;
367
  l->attrs[1].u.data = m2;
368
  l->attrs[2].id = EA_OSPF_TAG;
369
  l->attrs[2].flags = 0;
370
  l->attrs[2].type = EAF_TYPE_INT | EAF_TEMP;
371
  l->attrs[2].u.data = tag;
372 c27b2449 Ondrej Zajicek
  l->attrs[3].id = EA_OSPF_ROUTER_ID;
373
  l->attrs[3].flags = 0;
374 126683fe Ondrej Zajicek
  l->attrs[3].type = EAF_TYPE_ROUTER_ID | EAF_TEMP;
375 c27b2449 Ondrej Zajicek
  l->attrs[3].u.data = rid;
376 5919c66e Martin Mares
  return l;
377 00c1f79a Ondrej Filip
}
378
379 70a38319 Ondrej Filip
380
void
381 a7a7372a Ondrej Zajicek
ospf_schedule_rtcalc(struct ospf_proto *p)
382 70a38319 Ondrej Filip
{
383 70945cb6 Ondrej Zajicek
  if (p->calcrt)
384 2e10a170 Ondrej Filip
    return;
385 3df1e804 Ondrej Filip
386 be862406 Ondrej Zajicek
  OSPF_TRACE(D_EVENTS, "Scheduling routing table calculation");
387 70945cb6 Ondrej Zajicek
  p->calcrt = 1;
388 70a38319 Ondrej Filip
}
389
390 f7574707 Ondrej Zajicek
static int
391 70945cb6 Ondrej Zajicek
ospf_reload_routes(struct proto *P)
392 f7574707 Ondrej Zajicek
{
393 70945cb6 Ondrej Zajicek
  struct ospf_proto *p = (struct ospf_proto *) P;
394 f7574707 Ondrej Zajicek
395 70945cb6 Ondrej Zajicek
  if (p->calcrt != 2)
396 f7574707 Ondrej Zajicek
    OSPF_TRACE(D_EVENTS, "Scheduling routing table calculation with route reload");
397
398 70945cb6 Ondrej Zajicek
  p->calcrt = 2;
399 f7574707 Ondrej Zajicek
400
  return 1;
401
}
402
403 b8f17cf1 Ondrej Filip
404 86c84d76 Ondrej Filip
/**
405 2918e610 Ondrej Zajicek
 * ospf_disp - invokes routing table calculation, aging and also area_disp()
406 70945cb6 Ondrej Zajicek
 * @timer: timer usually called every @ospf_proto->tick second, @timer->data
407
 * point to @ospf_proto
408 86c84d76 Ondrej Filip
 */
409 70945cb6 Ondrej Zajicek
static void
410 b8f17cf1 Ondrej Filip
ospf_disp(timer * timer)
411
{
412 70945cb6 Ondrej Zajicek
  struct ospf_proto *p = timer->data;
413 3b16080c Ondrej Filip
414 70945cb6 Ondrej Zajicek
  /* Originate or flush local topology LSAs */
415
  ospf_update_topology(p);
416 e59e310e Ondrej Filip
417 a7a7372a Ondrej Zajicek
  /* Process LSA DB */
418 70945cb6 Ondrej Zajicek
  ospf_update_lsadb(p);
419 86c84d76 Ondrej Filip
420 e59e310e Ondrej Filip
  /* Calculate routing table */
421 70945cb6 Ondrej Zajicek
  if (p->calcrt)
422
    ospf_rt_spf(p);
423 70a38319 Ondrej Filip
}
424
425 b8f17cf1 Ondrej Filip
426 6f58dc64 Ondrej Filip
/**
427
 * ospf_import_control - accept or reject new route from nest's routing table
428 a7a7372a Ondrej Zajicek
 * @P: OSPF protocol instance
429 6567e6cf Martin Mares
 * @new: the new route
430 baa5dd6c Ondrej Filip
 * @attrs: list of attributes
431
 * @pool: pool for allocation of attributes
432 6f58dc64 Ondrej Filip
 *
433 baa5dd6c Ondrej Filip
 * Its quite simple. It does not accept our own routes and leaves the decision on
434
 * import to the filters.
435 6f58dc64 Ondrej Filip
 */
436 70945cb6 Ondrej Zajicek
static int
437
ospf_import_control(struct proto *P, rte **new, ea_list **attrs, struct linpool *pool)
438 2d5b9992 Ondrej Filip
{
439 70945cb6 Ondrej Zajicek
  struct ospf_proto *p = (struct ospf_proto *) P;
440
  struct ospf_area *oa = ospf_main_area(p);
441 2e10a170 Ondrej Filip
  rte *e = *new;
442 2d5b9992 Ondrej Filip
443 70945cb6 Ondrej Zajicek
  if (e->attrs->src->proto == P)
444 2e10a170 Ondrej Filip
    return -1;                        /* Reject our own routes */
445 ba5e5940 Ondrej Zajicek
446 41b612c3 Ondrej Zajicek
  if (oa_is_stub(oa))
447
    return -1;                        /* Do not export routes to stub areas */
448
449 1321e12a Ondrej Zajicek
  ea_list *ea = e->attrs->eattrs;
450
  u32 m0 = ea_get_int(ea, EA_GEN_IGP_METRIC, LSINFINITY);
451
  u32 m1 = MIN(m0, LSINFINITY);
452
  u32 m2 = 10000;
453
  u32 tag = 0;
454
455
  /* Hack for setting attributes directly in static protocol */
456
  if (e->attrs->source == RTS_STATIC)
457
  {
458
    m1 = ea_get_int(ea, EA_OSPF_METRIC1, m1);
459
    m2 = ea_get_int(ea, EA_OSPF_METRIC2, 10000);
460
    tag = ea_get_int(ea, EA_OSPF_TAG, 0);
461
  }
462 ba5e5940 Ondrej Zajicek
463 1321e12a Ondrej Zajicek
  *attrs = ospf_build_attrs(*attrs, pool, m1, m2, tag, 0);
464 2e10a170 Ondrej Filip
  return 0;                        /* Leave decision to the filters */
465 5919c66e Martin Mares
}
466 2d5b9992 Ondrej Filip
467 70945cb6 Ondrej Zajicek
static struct ea_list *
468 5919c66e Martin Mares
ospf_make_tmp_attrs(struct rte *rt, struct linpool *pool)
469
{
470 2e10a170 Ondrej Filip
  return ospf_build_attrs(NULL, pool, rt->u.ospf.metric1, rt->u.ospf.metric2,
471 c27b2449 Ondrej Zajicek
                          rt->u.ospf.tag, rt->u.ospf.router_id);
472 5919c66e Martin Mares
}
473
474 70945cb6 Ondrej Zajicek
static void
475 5919c66e Martin Mares
ospf_store_tmp_attrs(struct rte *rt, struct ea_list *attrs)
476
{
477 8441f179 Martin Mares
  rt->u.ospf.metric1 = ea_get_int(attrs, EA_OSPF_METRIC1, LSINFINITY);
478
  rt->u.ospf.metric2 = ea_get_int(attrs, EA_OSPF_METRIC2, 10000);
479 2e10a170 Ondrej Filip
  rt->u.ospf.tag = ea_get_int(attrs, EA_OSPF_TAG, 0);
480 c27b2449 Ondrej Zajicek
  rt->u.ospf.router_id = ea_get_int(attrs, EA_OSPF_ROUTER_ID, 0);
481 2d5b9992 Ondrej Filip
}
482
483 6f58dc64 Ondrej Filip
/**
484 baa5dd6c Ondrej Filip
 * ospf_shutdown - Finish of OSPF instance
485 a7a7372a Ondrej Zajicek
 * @P: OSPF protocol instance
486 6f58dc64 Ondrej Filip
 *
487 baa5dd6c Ondrej Filip
 * RFC does not define any action that should be taken before router
488 6f58dc64 Ondrej Filip
 * shutdown. To make my neighbors react as fast as possible, I send
489
 * them hello packet with empty neighbor list. They should start
490 baa5dd6c Ondrej Filip
 * their neighbor state machine with event %NEIGHBOR_1WAY.
491 6f58dc64 Ondrej Filip
 */
492 4bd28fb6 Ondrej Filip
static int
493 70945cb6 Ondrej Zajicek
ospf_shutdown(struct proto *P)
494 4bd28fb6 Ondrej Filip
{
495 70945cb6 Ondrej Zajicek
  struct ospf_proto *p = (struct ospf_proto *) P;
496 4bd28fb6 Ondrej Filip
  struct ospf_iface *ifa;
497 70945cb6 Ondrej Zajicek
498 ee4880c8 Ondrej Filip
  OSPF_TRACE(D_EVENTS, "Shutdown requested");
499 4bd28fb6 Ondrej Filip
500
  /* And send to all my neighbors 1WAY */
501 70945cb6 Ondrej Zajicek
  WALK_LIST(ifa, p->iface_list)
502 8e48831a Ondrej Zajicek
    ospf_iface_shutdown(ifa);
503 2e10a170 Ondrej Filip
504 57c574d8 Ondrej Zajicek
  /* Cleanup locked rta entries */
505 600998fc Ondrej Zajicek (work)
  FIB_WALK(&p->rtf, ort, nf)
506 57c574d8 Ondrej Zajicek
  {
507 600998fc Ondrej Zajicek (work)
    rta_free(nf->old_rta);
508 57c574d8 Ondrej Zajicek
  }
509
  FIB_WALK_END;
510
511 4bd28fb6 Ondrej Filip
  return PS_DOWN;
512
}
513
514 3d15dcdb Ondrej Zajicek
static void
515 70945cb6 Ondrej Zajicek
ospf_get_status(struct proto *P, byte * buf)
516 e8085aba Ondrej Filip
{
517 70945cb6 Ondrej Zajicek
  struct ospf_proto *p = (struct ospf_proto *) P;
518 e8085aba Ondrej Filip
519 70945cb6 Ondrej Zajicek
  if (p->p.proto_state == PS_DOWN)
520 2e10a170 Ondrej Filip
    buf[0] = 0;
521 4414d9a5 Ondrej Filip
  else
522
  {
523
    struct ospf_iface *ifa;
524
    struct ospf_neighbor *n;
525 2e10a170 Ondrej Filip
    int adj = 0;
526 4414d9a5 Ondrej Filip
527 70945cb6 Ondrej Zajicek
    WALK_LIST(ifa, p->iface_list)
528 2e10a170 Ondrej Filip
      WALK_LIST(n, ifa->neigh_list) if (n->state == NEIGHBOR_FULL)
529
      adj = 1;
530 4414d9a5 Ondrej Filip
531 2e10a170 Ondrej Filip
    if (adj == 0)
532
      strcpy(buf, "Alone");
533
    else
534
      strcpy(buf, "Running");
535 4414d9a5 Ondrej Filip
  }
536
}
537
538 f7c0525e Ondrej Filip
static void
539 2e10a170 Ondrej Filip
ospf_get_route_info(rte * rte, byte * buf, ea_list * attrs UNUSED)
540 f7c0525e Ondrej Filip
{
541 98ac6176 Ondrej Filip
  char *type = "<bug>";
542 f7c0525e Ondrej Filip
543 48e5f32d Ondrej Zajicek
  switch (rte->attrs->source)
544 05dbc97b Ondrej Filip
  {
545 70945cb6 Ondrej Zajicek
  case RTS_OSPF:
546
    type = "I";
547
    break;
548
  case RTS_OSPF_IA:
549
    type = "IA";
550
    break;
551
  case RTS_OSPF_EXT1:
552
    type = "E1";
553
    break;
554
  case RTS_OSPF_EXT2:
555
    type = "E2";
556
    break;
557 05dbc97b Ondrej Filip
  }
558 98ac6176 Ondrej Filip
559
  buf += bsprintf(buf, " %s", type);
560 d6a7e2fb Ondrej Filip
  buf += bsprintf(buf, " (%d/%d", rte->pref, rte->u.ospf.metric1);
561 98ac6176 Ondrej Filip
  if (rte->attrs->source == RTS_OSPF_EXT2)
562 2e10a170 Ondrej Filip
    buf += bsprintf(buf, "/%d", rte->u.ospf.metric2);
563 d6a7e2fb Ondrej Filip
  buf += bsprintf(buf, ")");
564 41b612c3 Ondrej Zajicek
  if ((rte->attrs->source == RTS_OSPF_EXT1 || rte->attrs->source == RTS_OSPF_EXT2) && rte->u.ospf.tag)
565 a7a3a0a3 Ondrej Filip
  {
566 23df5e4c Martin Mares
    buf += bsprintf(buf, " [%x]", rte->u.ospf.tag);
567 a7a3a0a3 Ondrej Filip
  }
568 c27b2449 Ondrej Zajicek
  if (rte->u.ospf.router_id)
569
    buf += bsprintf(buf, " [%R]", rte->u.ospf.router_id);
570 f7c0525e Ondrej Filip
}
571
572 5919c66e Martin Mares
static int
573 aebe06b4 Ondrej Zajicek
ospf_get_attr(eattr * a, byte * buf, int buflen UNUSED)
574 5919c66e Martin Mares
{
575
  switch (a->id)
576 2e10a170 Ondrej Filip
  {
577
  case EA_OSPF_METRIC1:
578
    bsprintf(buf, "metric1");
579
    return GA_NAME;
580
  case EA_OSPF_METRIC2:
581
    bsprintf(buf, "metric2");
582
    return GA_NAME;
583
  case EA_OSPF_TAG:
584 126683fe Ondrej Zajicek
    bsprintf(buf, "tag: 0x%08x", a->u.data);
585 2e10a170 Ondrej Filip
    return GA_FULL;
586 126683fe Ondrej Zajicek
  case EA_OSPF_ROUTER_ID:
587
    bsprintf(buf, "router_id");
588
    return GA_NAME;
589 2e10a170 Ondrej Filip
  default:
590
    return GA_UNKNOWN;
591
  }
592 5919c66e Martin Mares
}
593 6f58dc64 Ondrej Filip
594 8e48831a Ondrej Zajicek
static void
595
ospf_area_reconfigure(struct ospf_area *oa, struct ospf_area_config *nac)
596 edc34dc9 Ondrej Filip
{
597 70945cb6 Ondrej Zajicek
  struct ospf_proto *p = oa->po;
598
  struct ospf_area_config *oac = oa->ac;
599
  struct ospf_iface *ifa;
600
601 8e48831a Ondrej Zajicek
  oa->ac = nac;
602 2918e610 Ondrej Zajicek
603 70945cb6 Ondrej Zajicek
  if (ospf_is_v2(p))
604
    oa->options = nac->type;
605
  else
606
    oa->options = nac->type | OPT_V6 | (p->stub_router ? 0 : OPT_R);
607 8e48831a Ondrej Zajicek
608 70945cb6 Ondrej Zajicek
  if (nac->type != oac->type)
609
  {
610
    /* Force restart of area interfaces */
611
    WALK_LIST(ifa, p->iface_list)
612
      if (ifa->oa == oa)
613
        ifa->marked = 2;
614
  }
615 8e48831a Ondrej Zajicek
616
  /* Handle net_list */
617
  fib_free(&oa->net_fib);
618 ed317862 Ondrej Zajicek
  fib_free(&oa->enet_fib);
619 8e48831a Ondrej Zajicek
  add_area_nets(oa, nac);
620
621
  /* No need to handle stubnet_list */
622
623
  oa->marked = 0;
624 70945cb6 Ondrej Zajicek
  ospf_notify_rt_lsa(oa);
625 edc34dc9 Ondrej Filip
}
626 5919c66e Martin Mares
627 6f58dc64 Ondrej Filip
/**
628
 * ospf_reconfigure - reconfiguration hook
629 a7a7372a Ondrej Zajicek
 * @P: current instance of protocol (with old configuration)
630 6f58dc64 Ondrej Filip
 * @c: new configuration requested by user
631
 *
632 baa5dd6c Ondrej Filip
 * This hook tries to be a little bit intelligent. Instance of OSPF
633 6f58dc64 Ondrej Filip
 * will survive change of many constants like hello interval,
634 baa5dd6c Ondrej Filip
 * password change, addition or deletion of some neighbor on
635 6f58dc64 Ondrej Filip
 * nonbroadcast network, cost of interface, etc.
636
 */
637 80787d41 Ondrej Filip
static int
638 70945cb6 Ondrej Zajicek
ospf_reconfigure(struct proto *P, struct proto_config *c)
639 80787d41 Ondrej Filip
{
640 70945cb6 Ondrej Zajicek
  struct ospf_proto *p = (struct ospf_proto *) P;
641
  struct ospf_config *old = (struct ospf_config *) (P->cf);
642 2e10a170 Ondrej Filip
  struct ospf_config *new = (struct ospf_config *) c;
643 8e48831a Ondrej Zajicek
  struct ospf_area_config *nac;
644
  struct ospf_area *oa, *oax;
645
  struct ospf_iface *ifa, *ifx;
646
  struct ospf_iface_patt *ip;
647
648 70945cb6 Ondrej Zajicek
  if (proto_get_router_id(c) != p->router_id)
649 79b4e12e Ondrej Zajicek
    return 0;
650
651 23c212e7 Ondrej Zajicek (work)
  if (p->ospf2 != new->ospf2)
652
    return 0;
653
654 70945cb6 Ondrej Zajicek
  if (p->rfc1583 != new->rfc1583)
655 d82fc18d Ondrej Zajicek
    return 0;
656
657 8e48831a Ondrej Zajicek
  if (old->abr != new->abr)
658
    return 0;
659 edc34dc9 Ondrej Filip
660 70945cb6 Ondrej Zajicek
  p->stub_router = new->stub_router;
661
  p->merge_external = new->merge_external;
662
  p->asbr = new->asbr;
663
  p->ecmp = new->ecmp;
664
  p->tick = new->tick;
665
  p->disp_timer->recurrent = p->tick;
666
  tm_start(p->disp_timer, 1);
667 52d61a84 Ondrej Filip
668 8e48831a Ondrej Zajicek
  /* Mark all areas and ifaces */
669 70945cb6 Ondrej Zajicek
  WALK_LIST(oa, p->area_list)
670 8e48831a Ondrej Zajicek
    oa->marked = 1;
671 edc34dc9 Ondrej Filip
672 70945cb6 Ondrej Zajicek
  WALK_LIST(ifa, p->iface_list)
673 8e48831a Ondrej Zajicek
    ifa->marked = 1;
674 2e10a170 Ondrej Filip
675 8e48831a Ondrej Zajicek
  /* Add and update areas */
676
  WALK_LIST(nac, new->area_list)
677 edc34dc9 Ondrej Filip
  {
678 70945cb6 Ondrej Zajicek
    oa = ospf_find_area(p, nac->areaid);
679 8e48831a Ondrej Zajicek
    if (oa)
680
      ospf_area_reconfigure(oa, nac);
681
    else
682 a7a7372a Ondrej Zajicek
      ospf_area_add(p, nac);
683 8e48831a Ondrej Zajicek
  }
684 c926eee7 Ondrej Filip
685 70945cb6 Ondrej Zajicek
  /* Add and update interfaces */
686
  ospf_reconfigure_ifaces(p);
687
688 8e48831a Ondrej Zajicek
  /* Add and update vlinks */
689
  WALK_LIST(ip, new->vlink_list)
690
  {
691 70945cb6 Ondrej Zajicek
    ifa = ospf_find_vlink(p, ip->voa, ip->vid);
692 8e48831a Ondrej Zajicek
    if (ifa)
693
      ospf_iface_reconfigure(ifa, ip);
694
    else
695 70945cb6 Ondrej Zajicek
      ospf_iface_new_vlink(p, ip);
696 8e48831a Ondrej Zajicek
  }
697 edc34dc9 Ondrej Filip
698 8e48831a Ondrej Zajicek
  /* Delete remaining ifaces and areas */
699 70945cb6 Ondrej Zajicek
  WALK_LIST_DELSAFE(ifa, ifx, p->iface_list)
700 8e48831a Ondrej Zajicek
    if (ifa->marked)
701 edc34dc9 Ondrej Filip
    {
702 8e48831a Ondrej Zajicek
      ospf_iface_shutdown(ifa);
703
      ospf_iface_remove(ifa);
704 edc34dc9 Ondrej Filip
    }
705
706 70945cb6 Ondrej Zajicek
  WALK_LIST_DELSAFE(oa, oax, p->area_list)
707 8e48831a Ondrej Zajicek
    if (oa->marked)
708
      ospf_area_remove(oa);
709 edc34dc9 Ondrej Filip
710 a7a7372a Ondrej Zajicek
  ospf_schedule_rtcalc(p);
711 742029eb Ondrej Zajicek
712 8e48831a Ondrej Zajicek
  return 1;
713 80787d41 Ondrej Filip
}
714 a783e259 Ondrej Filip
715 8e48831a Ondrej Zajicek
716 a783e259 Ondrej Filip
void
717 70945cb6 Ondrej Zajicek
ospf_sh_neigh(struct proto *P, char *iff)
718 a783e259 Ondrej Filip
{
719 70945cb6 Ondrej Zajicek
  struct ospf_proto *p = (struct ospf_proto *) P;
720 e81b440f Ondrej Zajicek
  struct ospf_iface *ifa = NULL;
721 a783e259 Ondrej Filip
  struct ospf_neighbor *n;
722 3488634c Ondrej Filip
723 70945cb6 Ondrej Zajicek
  if (p->p.proto_state != PS_UP)
724 3488634c Ondrej Filip
  {
725 70945cb6 Ondrej Zajicek
    cli_msg(-1013, "%s: is not up", p->p.name);
726 2e10a170 Ondrej Filip
    cli_msg(0, "");
727 3488634c Ondrej Filip
    return;
728
  }
729 2e10a170 Ondrej Filip
730 70945cb6 Ondrej Zajicek
  cli_msg(-1013, "%s:", p->p.name);
731 cf31112f Ondrej Zajicek
  cli_msg(-1013, "%-12s\t%3s\t%-15s\t%-5s\t%-10s %-12s", "Router ID", "Pri",
732
          "     State", "DTime", "Interface", "Router IP");
733 70945cb6 Ondrej Zajicek
  WALK_LIST(ifa, p->iface_list)
734 48e5f32d Ondrej Zajicek
    if ((iff == NULL) || patmatch(iff, ifa->ifname))
735 610bb3cf Ondrej Zajicek
      WALK_LIST(n, ifa->neigh_list)
736
        ospf_sh_neigh_info(n);
737 2e10a170 Ondrej Filip
  cli_msg(0, "");
738 a783e259 Ondrej Filip
}
739 4ab4e977 Ondrej Filip
740
void
741 70945cb6 Ondrej Zajicek
ospf_sh(struct proto *P)
742 4ab4e977 Ondrej Filip
{
743 70945cb6 Ondrej Zajicek
  struct ospf_proto *p = (struct ospf_proto *) P;
744 4ab4e977 Ondrej Filip
  struct ospf_area *oa;
745
  struct ospf_iface *ifa;
746
  struct ospf_neighbor *n;
747 98ac6176 Ondrej Filip
  int ifano, nno, adjno, firstfib;
748 4ab4e977 Ondrej Filip
749 70945cb6 Ondrej Zajicek
  if (p->p.proto_state != PS_UP)
750 3488634c Ondrej Filip
  {
751 70945cb6 Ondrej Zajicek
    cli_msg(-1014, "%s: is not up", p->p.name);
752 2e10a170 Ondrej Filip
    cli_msg(0, "");
753 3488634c Ondrej Filip
    return;
754
  }
755
756 70945cb6 Ondrej Zajicek
  cli_msg(-1014, "%s:", p->p.name);
757 f8fefde3 Ondrej Zajicek
  cli_msg(-1014, "RFC1583 compatibility: %s", (p->rfc1583 ? "enabled" : "disabled"));
758 70945cb6 Ondrej Zajicek
  cli_msg(-1014, "Stub router: %s", (p->stub_router ? "Yes" : "No"));
759
  cli_msg(-1014, "RT scheduler tick: %d", p->tick);
760
  cli_msg(-1014, "Number of areas: %u", p->areano);
761
  cli_msg(-1014, "Number of LSAs in DB:\t%u", p->gr->hash_entries);
762 2e10a170 Ondrej Filip
763 70945cb6 Ondrej Zajicek
  WALK_LIST(oa, p->area_list)
764 4ab4e977 Ondrej Filip
  {
765 3aab39f5 Ondrej Zajicek
    cli_msg(-1014, "\tArea: %R (%u) %s", oa->areaid, oa->areaid,
766 2e10a170 Ondrej Filip
            oa->areaid == 0 ? "[BACKBONE]" : "");
767
    ifano = 0;
768
    nno = 0;
769
    adjno = 0;
770 70945cb6 Ondrej Zajicek
    WALK_LIST(ifa, p->iface_list)
771 4ab4e977 Ondrej Filip
    {
772 2e10a170 Ondrej Filip
      if (oa == ifa->oa)
773 4ab4e977 Ondrej Filip
      {
774 3b16080c Ondrej Filip
        ifano++;
775 742029eb Ondrej Zajicek
        WALK_LIST(n, ifa->neigh_list)
776
        {
777 3b16080c Ondrej Filip
          nno++;
778
          if (n->state == NEIGHBOR_FULL)
779
            adjno++;
780 742029eb Ondrej Zajicek
        }
781 4ab4e977 Ondrej Filip
      }
782
    }
783 ed317862 Ondrej Zajicek
784
    cli_msg(-1014, "\t\tStub:\t%s", oa_is_stub(oa) ? "Yes" : "No");
785
    cli_msg(-1014, "\t\tNSSA:\t%s", oa_is_nssa(oa) ? "Yes" : "No");
786 3b16080c Ondrej Filip
    cli_msg(-1014, "\t\tTransit:\t%s", oa->trcap ? "Yes" : "No");
787 ed317862 Ondrej Zajicek
788
    if (oa_is_nssa(oa))
789
      cli_msg(-1014, "\t\tNSSA translation:\t%s%s", oa->translate ? "Yes" : "No",
790
              oa->translate == TRANS_WAIT ? " (run down)" : "");
791 2e10a170 Ondrej Filip
    cli_msg(-1014, "\t\tNumber of interfaces:\t%u", ifano);
792
    cli_msg(-1014, "\t\tNumber of neighbors:\t%u", nno);
793
    cli_msg(-1014, "\t\tNumber of adjacent neighbors:\t%u", adjno);
794 98ac6176 Ondrej Filip
795
    firstfib = 1;
796 600998fc Ondrej Zajicek (work)
    FIB_WALK(&oa->net_fib, struct area_net, anet)
797 c926eee7 Ondrej Filip
    {
798 98ac6176 Ondrej Filip
      if(firstfib)
799 c926eee7 Ondrej Filip
      {
800 742029eb Ondrej Zajicek
        cli_msg(-1014, "\t\tArea networks:");
801
        firstfib = 0;
802 c926eee7 Ondrej Filip
      }
803 fe9f1a6d Ondrej Zajicek (work)
      cli_msg(-1014, "\t\t\t%1N\t%s\t%s", anet->fn.addr,
804 98ac6176 Ondrej Filip
                anet->hidden ? "Hidden" : "Advertise", anet->active ? "Active" : "");
805 c926eee7 Ondrej Filip
    }
806 98ac6176 Ondrej Filip
    FIB_WALK_END;
807 ed317862 Ondrej Zajicek
808
    firstfib = 1;
809 600998fc Ondrej Zajicek (work)
    FIB_WALK(&oa->enet_fib, struct area_net, anet)
810 ed317862 Ondrej Zajicek
    {
811
      if(firstfib)
812
      {
813 742029eb Ondrej Zajicek
        cli_msg(-1014, "\t\tArea external networks:");
814
        firstfib = 0;
815 ed317862 Ondrej Zajicek
      }
816 fe9f1a6d Ondrej Zajicek (work)
      cli_msg(-1014, "\t\t\t%1N\t%s\t%s", anet->fn.addr,
817 ed317862 Ondrej Zajicek
                anet->hidden ? "Hidden" : "Advertise", anet->active ? "Active" : "");
818
    }
819
    FIB_WALK_END;
820
821 4ab4e977 Ondrej Filip
  }
822 2e10a170 Ondrej Filip
  cli_msg(0, "");
823 4ab4e977 Ondrej Filip
}
824
825 c4f0f014 Ondrej Filip
void
826 70945cb6 Ondrej Zajicek
ospf_sh_iface(struct proto *P, char *iff)
827 c4f0f014 Ondrej Filip
{
828 70945cb6 Ondrej Zajicek
  struct ospf_proto *p = (struct ospf_proto *) P;
829 e81b440f Ondrej Zajicek
  struct ospf_iface *ifa = NULL;
830 c4f0f014 Ondrej Filip
831 70945cb6 Ondrej Zajicek
  if (p->p.proto_state != PS_UP)
832 3488634c Ondrej Filip
  {
833 70945cb6 Ondrej Zajicek
    cli_msg(-1015, "%s: is not up", p->p.name);
834 2e10a170 Ondrej Filip
    cli_msg(0, "");
835 3488634c Ondrej Filip
    return;
836
  }
837
838 70945cb6 Ondrej Zajicek
  cli_msg(-1015, "%s:", p->p.name);
839
  WALK_LIST(ifa, p->iface_list)
840 48e5f32d Ondrej Zajicek
    if ((iff == NULL) || patmatch(iff, ifa->ifname))
841 610bb3cf Ondrej Zajicek
      ospf_iface_info(ifa);
842 2e10a170 Ondrej Filip
  cli_msg(0, "");
843 c4f0f014 Ondrej Filip
}
844
845 b66abe8e Ondrej Zajicek
/* lsa_compare_for_state() - Compare function for 'show ospf state'
846
 *
847
 * First we want to separate network-LSAs and other LSAs (because network-LSAs
848 a3b70dc4 Ondrej Zajicek
 * will be presented as network nodes and other LSAs together as router nodes)
849
 * Network-LSAs are sorted according to network prefix, other LSAs are sorted
850
 * according to originating router id (to get all LSA needed to represent one
851
 * router node together). Then, according to LSA type, ID and age.
852 b66abe8e Ondrej Zajicek
 *
853
 * For OSPFv3, we have to handle also Prefix-LSAs. We would like to put each
854
 * immediately after the referenced LSA. We will make faked LSA based on ref_
855
 * values
856 a3b70dc4 Ondrej Zajicek
 */
857 b66abe8e Ondrej Zajicek
858
static struct ospf_lsa_header *
859
fake_lsa_from_prefix_lsa(struct ospf_lsa_header *dst, struct ospf_lsa_header *src,
860
                         struct ospf_lsa_prefix *px)
861
{
862
  dst->age = src->age;
863 70945cb6 Ondrej Zajicek
  dst->type_raw = px->ref_type;
864 b66abe8e Ondrej Zajicek
  dst->id = px->ref_id;
865
  dst->rt = px->ref_rt;
866
  dst->sn = src->sn;
867
868
  return dst;
869
}
870
871 70945cb6 Ondrej Zajicek
872
static int lsa_compare_ospf3;
873 98955023 Ondrej Zajicek
874 a3b70dc4 Ondrej Zajicek
static int
875 b66abe8e Ondrej Zajicek
lsa_compare_for_state(const void *p1, const void *p2)
876 a3b70dc4 Ondrej Zajicek
{
877 70945cb6 Ondrej Zajicek
  struct top_hash_entry *he1 = * (struct top_hash_entry **) p1;
878
  struct top_hash_entry *he2 = * (struct top_hash_entry **) p2;
879 a3b70dc4 Ondrej Zajicek
  struct ospf_lsa_header *lsa1 = &(he1->lsa);
880
  struct ospf_lsa_header *lsa2 = &(he2->lsa);
881 70945cb6 Ondrej Zajicek
  struct ospf_lsa_header lsatmp1, lsatmp2;
882
  u16 lsa1_type = he1->lsa_type;
883
  u16 lsa2_type = he2->lsa_type;
884 b66abe8e Ondrej Zajicek
885 8249ad9b Ondrej Zajicek
  if (he1->domain < he2->domain)
886
    return -1;
887
  if (he1->domain > he2->domain)
888
    return 1;
889 b66abe8e Ondrej Zajicek
890
891 70945cb6 Ondrej Zajicek
  /* px1 or px2 assumes OSPFv3 */
892
  int px1 = (lsa1_type == LSA_T_PREFIX);
893
  int px2 = (lsa2_type == LSA_T_PREFIX);
894 b66abe8e Ondrej Zajicek
895
  if (px1)
896 70945cb6 Ondrej Zajicek
  {
897 b66abe8e Ondrej Zajicek
    lsa1 = fake_lsa_from_prefix_lsa(&lsatmp1, lsa1, he1->lsa_body);
898 70945cb6 Ondrej Zajicek
    lsa1_type = lsa1->type_raw;        /* FIXME: handle unknown ref_type */
899
  }
900 b66abe8e Ondrej Zajicek
901
  if (px2)
902 70945cb6 Ondrej Zajicek
  {
903 b66abe8e Ondrej Zajicek
    lsa2 = fake_lsa_from_prefix_lsa(&lsatmp2, lsa2, he2->lsa_body);
904 70945cb6 Ondrej Zajicek
    lsa2_type = lsa2->type_raw;
905
  }
906
907 b66abe8e Ondrej Zajicek
908 70945cb6 Ondrej Zajicek
  int nt1 = (lsa1_type == LSA_T_NET);
909
  int nt2 = (lsa2_type == LSA_T_NET);
910 a3b70dc4 Ondrej Zajicek
911
  if (nt1 != nt2)
912
    return nt1 - nt2;
913
914
  if (nt1)
915
  {
916 70945cb6 Ondrej Zajicek
    /* In OSPFv3, networks are named based on ID of DR */
917
    if (lsa_compare_ospf3)
918
    {
919
      if (lsa1->rt < lsa2->rt)
920
        return -1;
921
      if (lsa1->rt > lsa2->rt)
922
        return 1;
923
    }
924 b66abe8e Ondrej Zajicek
925
    /* For OSPFv2, this is IP of the network,
926
       for OSPFv3, this is interface ID */
927 8249ad9b Ondrej Zajicek
    if (lsa1->id < lsa2->id)
928
      return -1;
929
    if (lsa1->id > lsa2->id)
930
      return 1;
931 b66abe8e Ondrej Zajicek
932
    if (px1 != px2)
933
      return px1 - px2;
934
935
    return lsa1->sn - lsa2->sn;
936 a3b70dc4 Ondrej Zajicek
  }
937 742029eb Ondrej Zajicek
  else
938 b66abe8e Ondrej Zajicek
  {
939 8249ad9b Ondrej Zajicek
    if (lsa1->rt < lsa2->rt)
940
      return -1;
941
    if (lsa1->rt > lsa2->rt)
942
      return 1;
943 b66abe8e Ondrej Zajicek
944 70945cb6 Ondrej Zajicek
    if (lsa1_type < lsa2_type)
945 8249ad9b Ondrej Zajicek
      return -1;
946 70945cb6 Ondrej Zajicek
    if (lsa1_type > lsa2_type)
947 8249ad9b Ondrej Zajicek
      return 1;
948
949
    if (lsa1->id < lsa2->id)
950
      return -1;
951
    if (lsa1->id > lsa2->id)
952
      return 1;
953 b66abe8e Ondrej Zajicek
954
    if (px1 != px2)
955
      return px1 - px2;
956 742029eb Ondrej Zajicek
957 b66abe8e Ondrej Zajicek
    return lsa1->sn - lsa2->sn;
958
  }
959 a3b70dc4 Ondrej Zajicek
}
960 b66abe8e Ondrej Zajicek
961 0ea8fb4a Ondrej Zajicek
static int
962
ext_compare_for_state(const void *p1, const void *p2)
963
{
964
  struct top_hash_entry * he1 = * (struct top_hash_entry **) p1;
965
  struct top_hash_entry * he2 = * (struct top_hash_entry **) p2;
966
  struct ospf_lsa_header *lsa1 = &(he1->lsa);
967
  struct ospf_lsa_header *lsa2 = &(he2->lsa);
968
969 8249ad9b Ondrej Zajicek
  if (lsa1->rt < lsa2->rt)
970
    return -1;
971
  if (lsa1->rt > lsa2->rt)
972
    return 1;
973
974
  if (lsa1->id < lsa2->id)
975
    return -1;
976
  if (lsa1->id > lsa2->id)
977
    return 1;
978 0ea8fb4a Ondrej Zajicek
979
  return lsa1->sn - lsa2->sn;
980
}
981
982
static inline void
983
show_lsa_distance(struct top_hash_entry *he)
984
{
985
  if (he->color == INSPF)
986
    cli_msg(-1016, "\t\tdistance %u", he->dist);
987
  else
988
    cli_msg(-1016, "\t\tunreachable");
989
}
990
991 a3b70dc4 Ondrej Zajicek
static inline void
992 70945cb6 Ondrej Zajicek
show_lsa_router(struct ospf_proto *p, struct top_hash_entry *he, int verbose)
993 a3b70dc4 Ondrej Zajicek
{
994 70945cb6 Ondrej Zajicek
  struct ospf_lsa_rt_walk rtl;
995 0ea8fb4a Ondrej Zajicek
996 70945cb6 Ondrej Zajicek
  cli_msg(-1016, "");
997
  cli_msg(-1016, "\trouter %R", he->lsa.rt);
998
  show_lsa_distance(he);
999 0ea8fb4a Ondrej Zajicek
1000 70945cb6 Ondrej Zajicek
  lsa_walk_rt_init(p, he, &rtl);
1001
  while (lsa_walk_rt(&rtl))
1002
    if (rtl.type == LSART_VLNK)
1003
      cli_msg(-1016, "\t\tvlink %R metric %u", rtl.id, rtl.metric);
1004 a3b70dc4 Ondrej Zajicek
1005 70945cb6 Ondrej Zajicek
  lsa_walk_rt_init(p, he, &rtl);
1006
  while (lsa_walk_rt(&rtl))
1007
    if (rtl.type == LSART_PTP)
1008
      cli_msg(-1016, "\t\trouter %R metric %u", rtl.id, rtl.metric);
1009 a3b70dc4 Ondrej Zajicek
1010 70945cb6 Ondrej Zajicek
  lsa_walk_rt_init(p, he, &rtl);
1011
  while (lsa_walk_rt(&rtl))
1012
    if (rtl.type == LSART_NET)
1013 a3b70dc4 Ondrej Zajicek
    {
1014 70945cb6 Ondrej Zajicek
      if (ospf_is_v2(p))
1015 a3b70dc4 Ondrej Zajicek
      {
1016 70945cb6 Ondrej Zajicek
        /* In OSPFv2, we try to find network-LSA to get prefix/pxlen */
1017
        struct top_hash_entry *net_he = ospf_hash_find_net2(p->gr, he->domain, rtl.id);
1018 b66abe8e Ondrej Zajicek
1019 a7a7372a Ondrej Zajicek
        if (net_he && (net_he->lsa.age < LSA_MAXAGE))
1020 70945cb6 Ondrej Zajicek
        {
1021
          struct ospf_lsa_header *net_lsa = &(net_he->lsa);
1022
          struct ospf_lsa_net *net_ln = net_he->lsa_body;
1023
1024 742029eb Ondrej Zajicek
          cli_msg(-1016, "\t\tnetwork %I/%d metric %u",
1025 70945cb6 Ondrej Zajicek
                  ipa_from_u32(net_lsa->id & net_ln->optx),
1026
                  u32_masklen(net_ln->optx), rtl.metric);
1027
        }
1028
        else
1029
          cli_msg(-1016, "\t\tnetwork [%R] metric %u", rtl.id, rtl.metric);
1030 a3b70dc4 Ondrej Zajicek
      }
1031
      else
1032 70945cb6 Ondrej Zajicek
        cli_msg(-1016, "\t\tnetwork [%R-%u] metric %u", rtl.id, rtl.nif, rtl.metric);
1033 a3b70dc4 Ondrej Zajicek
    }
1034
1035 70945cb6 Ondrej Zajicek
  if (ospf_is_v2(p) && verbose)
1036
  {
1037
    lsa_walk_rt_init(p, he, &rtl);
1038
    while (lsa_walk_rt(&rtl))
1039
      if (rtl.type == LSART_STUB)
1040
        cli_msg(-1016, "\t\tstubnet %I/%d metric %u",
1041
                ipa_from_u32(rtl.id), u32_masklen(rtl.data), rtl.metric);
1042
  }
1043 a3b70dc4 Ondrej Zajicek
}
1044
1045
static inline void
1046 70945cb6 Ondrej Zajicek
show_lsa_network(struct top_hash_entry *he, int ospf2)
1047 a3b70dc4 Ondrej Zajicek
{
1048
  struct ospf_lsa_header *lsa = &(he->lsa);
1049
  struct ospf_lsa_net *ln = he->lsa_body;
1050
  u32 i;
1051
1052 70945cb6 Ondrej Zajicek
  if (ospf2)
1053
  {
1054
    cli_msg(-1016, "");
1055
    cli_msg(-1016, "\tnetwork %I/%d", ipa_from_u32(lsa->id & ln->optx), u32_masklen(ln->optx));
1056
    cli_msg(-1016, "\t\tdr %R", lsa->rt);
1057
  }
1058
  else
1059
  {
1060
    cli_msg(-1016, "");
1061
    cli_msg(-1016, "\tnetwork [%R-%u]", lsa->rt, lsa->id);
1062
  }
1063 a3b70dc4 Ondrej Zajicek
1064 0ea8fb4a Ondrej Zajicek
  show_lsa_distance(he);
1065
1066 c3226991 Ondrej Zajicek
  for (i = 0; i < lsa_net_count(lsa); i++)
1067 b66abe8e Ondrej Zajicek
    cli_msg(-1016, "\t\trouter %R", ln->routers[i]);
1068 a3b70dc4 Ondrej Zajicek
}
1069
1070
static inline void
1071 70945cb6 Ondrej Zajicek
show_lsa_sum_net(struct top_hash_entry *he, int ospf2)
1072 a3b70dc4 Ondrej Zajicek
{
1073 fe9f1a6d Ondrej Zajicek (work)
  net_addr net;
1074 b66abe8e Ondrej Zajicek
  u8 pxopts;
1075 70945cb6 Ondrej Zajicek
  u32 metric;
1076 b66abe8e Ondrej Zajicek
1077 fe9f1a6d Ondrej Zajicek (work)
  lsa_parse_sum_net(he, ospf2, &net, &pxopts, &metric);
1078
  cli_msg(-1016, "\t\txnetwork %N metric %u", &net, metric);
1079 a3b70dc4 Ondrej Zajicek
}
1080
1081
static inline void
1082 70945cb6 Ondrej Zajicek
show_lsa_sum_rt(struct top_hash_entry *he, int ospf2)
1083 a3b70dc4 Ondrej Zajicek
{
1084 70945cb6 Ondrej Zajicek
  u32 metric;
1085 d494df63 Ondrej Zajicek
  u32 dst_rid;
1086 70945cb6 Ondrej Zajicek
  u32 options;
1087 b66abe8e Ondrej Zajicek
1088 70945cb6 Ondrej Zajicek
  lsa_parse_sum_rt(he, ospf2, &dst_rid, &metric, &options);
1089
  cli_msg(-1016, "\t\txrouter %R metric %u", dst_rid, metric);
1090 a3b70dc4 Ondrej Zajicek
}
1091
1092
1093
static inline void
1094 70945cb6 Ondrej Zajicek
show_lsa_external(struct top_hash_entry *he, int ospf2)
1095 a3b70dc4 Ondrej Zajicek
{
1096 70945cb6 Ondrej Zajicek
  struct ospf_lsa_ext_local rt;
1097 7fd4143e Jan Moskyto Matejka
  char str_via[IPA_MAX_TEXT_LENGTH + 8] = "";
1098 a3b70dc4 Ondrej Zajicek
  char str_tag[16] = "";
1099 b66abe8e Ondrej Zajicek
1100 70945cb6 Ondrej Zajicek
  if (he->lsa_type == LSA_T_EXT)
1101 41b612c3 Ondrej Zajicek
    he->domain = 0; /* Unmark the LSA */
1102 0ea8fb4a Ondrej Zajicek
1103 70945cb6 Ondrej Zajicek
  lsa_parse_ext(he, ospf2, &rt);
1104 742029eb Ondrej Zajicek
1105 70945cb6 Ondrej Zajicek
  if (rt.fbit)
1106
    bsprintf(str_via, " via %I", rt.fwaddr);
1107 a3b70dc4 Ondrej Zajicek
1108 70945cb6 Ondrej Zajicek
  if (rt.tag)
1109
    bsprintf(str_tag, " tag %08x", rt.tag);
1110 a3b70dc4 Ondrej Zajicek
1111 fe9f1a6d Ondrej Zajicek (work)
  cli_msg(-1016, "\t\t%s %N metric%s %u%s%s",
1112 70945cb6 Ondrej Zajicek
          (he->lsa_type == LSA_T_NSSA) ? "nssa-ext" : "external",
1113 fe9f1a6d Ondrej Zajicek (work)
          &rt.net, rt.ebit ? "2" : "", rt.metric, str_via, str_tag);
1114 a3b70dc4 Ondrej Zajicek
}
1115
1116 b66abe8e Ondrej Zajicek
static inline void
1117 70945cb6 Ondrej Zajicek
show_lsa_prefix(struct top_hash_entry *he, struct top_hash_entry *cnode)
1118 b66abe8e Ondrej Zajicek
{
1119
  struct ospf_lsa_prefix *px = he->lsa_body;
1120
  u32 *buf;
1121
  int i;
1122
1123 0ea8fb4a Ondrej Zajicek
  /* We check whether given prefix-LSA is related to the current node */
1124 70945cb6 Ondrej Zajicek
  if ((px->ref_type != cnode->lsa.type_raw) || (px->ref_rt != cnode->lsa.rt))
1125 0ea8fb4a Ondrej Zajicek
    return;
1126
1127
  if ((px->ref_type == LSA_T_RT) && (px->ref_id != 0))
1128
    return;
1129
1130 70945cb6 Ondrej Zajicek
  if ((px->ref_type == LSA_T_NET) && (px->ref_id != cnode->lsa.id))
1131 b66abe8e Ondrej Zajicek
    return;
1132
1133
  buf = px->rest;
1134
  for (i = 0; i < px->pxcount; i++)
1135 fe9f1a6d Ondrej Zajicek (work)
  {
1136
    net_addr net;
1137
    u8 pxopts;
1138
    u16 metric;
1139 b66abe8e Ondrej Zajicek
1140 fe9f1a6d Ondrej Zajicek (work)
    buf = ospf_get_ipv6_prefix(buf, &net, &pxopts, &metric);
1141
1142
    if (px->ref_type == LSA_T_RT)
1143
      cli_msg(-1016, "\t\tstubnet %N metric %u", &net, metric);
1144
    else
1145
      cli_msg(-1016, "\t\taddress %N", &net);
1146
  }
1147 b66abe8e Ondrej Zajicek
}
1148
1149 a3b70dc4 Ondrej Zajicek
void
1150 70945cb6 Ondrej Zajicek
ospf_sh_state(struct proto *P, int verbose, int reachable)
1151 a3b70dc4 Ondrej Zajicek
{
1152 70945cb6 Ondrej Zajicek
  struct ospf_proto *p = (struct ospf_proto *) P;
1153
  int ospf2 = ospf_is_v2(p);
1154
  uint i, ix, j1, jx;
1155 a3b70dc4 Ondrej Zajicek
  u32 last_area = 0xFFFFFFFF;
1156 b49e6f5a Ondrej Zajicek
1157 70945cb6 Ondrej Zajicek
  if (p->p.proto_state != PS_UP)
1158 a3b70dc4 Ondrej Zajicek
  {
1159 70945cb6 Ondrej Zajicek
    cli_msg(-1016, "%s: is not up", p->p.name);
1160 a3b70dc4 Ondrej Zajicek
    cli_msg(0, "");
1161
    return;
1162
  }
1163
1164 742029eb Ondrej Zajicek
  /* We store interesting area-scoped LSAs in array hea and
1165 0ea8fb4a Ondrej Zajicek
     global-scoped (LSA_T_EXT) LSAs in array hex */
1166
1167 70945cb6 Ondrej Zajicek
  int num = p->gr->hash_entries;
1168 0ea8fb4a Ondrej Zajicek
  struct top_hash_entry *hea[num];
1169
  struct top_hash_entry *hex[verbose ? num : 0];
1170 a3b70dc4 Ondrej Zajicek
  struct top_hash_entry *he;
1171 70945cb6 Ondrej Zajicek
  struct top_hash_entry *cnode = NULL;
1172 a3b70dc4 Ondrej Zajicek
1173 70945cb6 Ondrej Zajicek
  j1 = jx = 0;
1174
  WALK_SLIST(he, p->lsal)
1175 b66abe8e Ondrej Zajicek
  {
1176
    int accept;
1177
1178 70945cb6 Ondrej Zajicek
    if (he->lsa.age == LSA_MAXAGE)
1179
      continue;
1180 b66abe8e Ondrej Zajicek
1181 70945cb6 Ondrej Zajicek
    switch (he->lsa_type)
1182
    {
1183
    case LSA_T_RT:
1184
    case LSA_T_NET:
1185
      accept = 1;
1186
      break;
1187 b66abe8e Ondrej Zajicek
1188 70945cb6 Ondrej Zajicek
    case LSA_T_SUM_NET:
1189
    case LSA_T_SUM_RT:
1190
    case LSA_T_NSSA:
1191
    case LSA_T_PREFIX:
1192
      accept = verbose;
1193
      break;
1194
1195
    case LSA_T_EXT:
1196
      if (verbose)
1197
      {
1198
        he->domain = 1; /* Abuse domain field to mark the LSA */
1199
        hex[jx++] = he;
1200 b66abe8e Ondrej Zajicek
      }
1201 70945cb6 Ondrej Zajicek
    default:
1202
      accept = 0;
1203
    }
1204 a3b70dc4 Ondrej Zajicek
1205 b66abe8e Ondrej Zajicek
    if (accept)
1206
      hea[j1++] = he;
1207
  }
1208
1209 70945cb6 Ondrej Zajicek
  ASSERT(j1 <= num && jx <= num);
1210 a3b70dc4 Ondrej Zajicek
1211 70945cb6 Ondrej Zajicek
  lsa_compare_ospf3 = !ospf2;
1212 b66abe8e Ondrej Zajicek
  qsort(hea, j1, sizeof(struct top_hash_entry *), lsa_compare_for_state);
1213 0ea8fb4a Ondrej Zajicek
  qsort(hex, jx, sizeof(struct top_hash_entry *), ext_compare_for_state);
1214 a3b70dc4 Ondrej Zajicek
1215 0ea8fb4a Ondrej Zajicek
  /*
1216
   * This code is a bit tricky, we have a primary LSAs (router and
1217
   * network) that are presented as a node, and secondary LSAs that
1218
   * are presented as a part of a primary node. cnode represents an
1219
   * currently opened node (whose header was presented). The LSAs are
1220
   * sorted to get secondary LSAs just after related primary LSA (if
1221
   * available). We present secondary LSAs only when related primary
1222
   * LSA is opened.
1223
   *
1224
   * AS-external LSAs are stored separately as they might be presented
1225
   * several times (for each area when related ASBR is opened). When
1226
   * the node is closed, related external routes are presented. We
1227
   * also have to take into account that in OSPFv3, there might be
1228
   * more router-LSAs and only the first should be considered as a
1229
   * primary. This is handled by not closing old router-LSA when next
1230
   * one is processed (which is not opened because there is already
1231
   * one opened).
1232
   */
1233
1234
  ix = 0;
1235 b66abe8e Ondrej Zajicek
  for (i = 0; i < j1; i++)
1236 a3b70dc4 Ondrej Zajicek
  {
1237 0ea8fb4a Ondrej Zajicek
    he = hea[i];
1238 a3b70dc4 Ondrej Zajicek
1239 0ea8fb4a Ondrej Zajicek
    /* If there is no opened node, we open the LSA (if appropriate) or skip to the next one */
1240
    if (!cnode)
1241 a3b70dc4 Ondrej Zajicek
    {
1242 70945cb6 Ondrej Zajicek
      if (((he->lsa_type == LSA_T_RT) || (he->lsa_type == LSA_T_NET))
1243 0ea8fb4a Ondrej Zajicek
          && ((he->color == INSPF) || !reachable))
1244
      {
1245 70945cb6 Ondrej Zajicek
        cnode = he;
1246 0ea8fb4a Ondrej Zajicek
1247
        if (he->domain != last_area)
1248
        {
1249
          cli_msg(-1016, "");
1250
          cli_msg(-1016, "area %R", he->domain);
1251
          last_area = he->domain;
1252
          ix = 0;
1253
        }
1254
      }
1255
      else
1256
        continue;
1257 a3b70dc4 Ondrej Zajicek
    }
1258
1259 70945cb6 Ondrej Zajicek
    ASSERT(cnode && (he->domain == last_area) && (he->lsa.rt == cnode->lsa.rt));
1260 0ea8fb4a Ondrej Zajicek
1261 70945cb6 Ondrej Zajicek
    switch (he->lsa_type)
1262 a3b70dc4 Ondrej Zajicek
    {
1263 70945cb6 Ondrej Zajicek
    case LSA_T_RT:
1264
      if (he->lsa.id == cnode->lsa.id)
1265
        show_lsa_router(p, he, verbose);
1266
      break;
1267 a3b70dc4 Ondrej Zajicek
1268 70945cb6 Ondrej Zajicek
    case LSA_T_NET:
1269
      show_lsa_network(he, ospf2);
1270
      break;
1271 a3b70dc4 Ondrej Zajicek
1272 70945cb6 Ondrej Zajicek
    case LSA_T_SUM_NET:
1273
      if (cnode->lsa_type == LSA_T_RT)
1274
        show_lsa_sum_net(he, ospf2);
1275
      break;
1276 a3b70dc4 Ondrej Zajicek
1277 70945cb6 Ondrej Zajicek
    case LSA_T_SUM_RT:
1278
      if (cnode->lsa_type == LSA_T_RT)
1279
        show_lsa_sum_rt(he, ospf2);
1280
      break;
1281 b66abe8e Ondrej Zajicek
1282 70945cb6 Ondrej Zajicek
    case LSA_T_EXT:
1283
    case LSA_T_NSSA:
1284
      show_lsa_external(he, ospf2);
1285
      break;
1286 0ea8fb4a Ondrej Zajicek
1287 70945cb6 Ondrej Zajicek
    case LSA_T_PREFIX:
1288
      show_lsa_prefix(he, cnode);
1289
      break;
1290 a3b70dc4 Ondrej Zajicek
    }
1291 b66abe8e Ondrej Zajicek
1292 0ea8fb4a Ondrej Zajicek
    /* In these cases, we close the current node */
1293
    if ((i+1 == j1)
1294
        || (hea[i+1]->domain != last_area)
1295 70945cb6 Ondrej Zajicek
        || (hea[i+1]->lsa.rt != cnode->lsa.rt)
1296
        || (hea[i+1]->lsa_type == LSA_T_NET))
1297 0ea8fb4a Ondrej Zajicek
    {
1298 70945cb6 Ondrej Zajicek
      while ((ix < jx) && (hex[ix]->lsa.rt < cnode->lsa.rt))
1299 0ea8fb4a Ondrej Zajicek
        ix++;
1300
1301 70945cb6 Ondrej Zajicek
      while ((ix < jx) && (hex[ix]->lsa.rt == cnode->lsa.rt))
1302
        show_lsa_external(hex[ix++], ospf2);
1303 0ea8fb4a Ondrej Zajicek
1304
      cnode = NULL;
1305
    }
1306
  }
1307
1308
  int hdr = 0;
1309
  u32 last_rt = 0xFFFFFFFF;
1310
  for (ix = 0; ix < jx; ix++)
1311
  {
1312
    he = hex[ix];
1313
1314
    /* If it is still marked, we show it now. */
1315
    if (he->domain)
1316
    {
1317
      he->domain = 0;
1318
1319
      if ((he->color != INSPF) && reachable)
1320
        continue;
1321
1322
      if (!hdr)
1323
      {
1324
        cli_msg(-1016, "");
1325
        cli_msg(-1016, "other ASBRs");
1326
        hdr = 1;
1327
      }
1328
1329
      if (he->lsa.rt != last_rt)
1330
      {
1331
        cli_msg(-1016, "");
1332
        cli_msg(-1016, "\trouter %R", he->lsa.rt);
1333
        last_rt = he->lsa.rt;
1334
      }
1335
1336 70945cb6 Ondrej Zajicek
      show_lsa_external(he, ospf2);
1337 0ea8fb4a Ondrej Zajicek
    }
1338 a3b70dc4 Ondrej Zajicek
  }
1339 0ea8fb4a Ondrej Zajicek
1340 a3b70dc4 Ondrej Zajicek
  cli_msg(0, "");
1341
}
1342
1343 98899244 Ondrej Zajicek
1344
static int
1345
lsa_compare_for_lsadb(const void *p1, const void *p2)
1346
{
1347
  struct top_hash_entry * he1 = * (struct top_hash_entry **) p1;
1348
  struct top_hash_entry * he2 = * (struct top_hash_entry **) p2;
1349
  struct ospf_lsa_header *lsa1 = &(he1->lsa);
1350
  struct ospf_lsa_header *lsa2 = &(he2->lsa);
1351 70945cb6 Ondrej Zajicek
  int sc1 = LSA_SCOPE(he1->lsa_type);
1352
  int sc2 = LSA_SCOPE(he2->lsa_type);
1353 98899244 Ondrej Zajicek
1354
  if (sc1 != sc2)
1355
    return sc2 - sc1;
1356
1357
  if (he1->domain != he2->domain)
1358
    return he1->domain - he2->domain;
1359
1360
  if (lsa1->rt != lsa2->rt)
1361
    return lsa1->rt - lsa2->rt;
1362 742029eb Ondrej Zajicek
1363 98899244 Ondrej Zajicek
  if (lsa1->id != lsa2->id)
1364
    return lsa1->id - lsa2->id;
1365
1366 70945cb6 Ondrej Zajicek
  if (he1->lsa_type != he2->lsa_type)
1367
    return he1->lsa_type - he2->lsa_type;
1368 98899244 Ondrej Zajicek
1369
  return lsa1->sn - lsa2->sn;
1370
}
1371
1372
void
1373 20ab192b Ondrej Zajicek
ospf_sh_lsadb(struct lsadb_show_data *ld)
1374 98899244 Ondrej Zajicek
{
1375 70945cb6 Ondrej Zajicek
  struct ospf_proto *p = (struct ospf_proto *) proto_get_named(ld->name, &proto_ospf);
1376 a7a7372a Ondrej Zajicek
  uint num = p->gr->hash_entries;
1377
  uint i, j;
1378 98899244 Ondrej Zajicek
  int last_dscope = -1;
1379
  u32 last_domain = 0;
1380 70945cb6 Ondrej Zajicek
  u16 type_mask = ospf_is_v2(p) ?  0x00ff : 0xffff;        /* see lsa_etype() */
1381 98899244 Ondrej Zajicek
1382 70945cb6 Ondrej Zajicek
  if (p->p.proto_state != PS_UP)
1383 98899244 Ondrej Zajicek
  {
1384 70945cb6 Ondrej Zajicek
    cli_msg(-1017, "%s: is not up", p->p.name);
1385 98899244 Ondrej Zajicek
    cli_msg(0, "");
1386
    return;
1387
  }
1388
1389 20ab192b Ondrej Zajicek
  if (ld->router == SH_ROUTER_SELF)
1390 70945cb6 Ondrej Zajicek
    ld->router = p->router_id;
1391 20ab192b Ondrej Zajicek
1392 0ea8fb4a Ondrej Zajicek
  struct top_hash_entry *hea[num];
1393 98899244 Ondrej Zajicek
  struct top_hash_entry *he;
1394
1395
  j = 0;
1396 70945cb6 Ondrej Zajicek
  WALK_SLIST(he, p->lsal)
1397
    if (he->lsa_body)
1398
      hea[j++] = he;
1399 98899244 Ondrej Zajicek
1400 70945cb6 Ondrej Zajicek
  ASSERT(j <= num);
1401 98899244 Ondrej Zajicek
1402
  qsort(hea, j, sizeof(struct top_hash_entry *), lsa_compare_for_lsadb);
1403
1404
  for (i = 0; i < j; i++)
1405
  {
1406
    struct ospf_lsa_header *lsa = &(hea[i]->lsa);
1407 70945cb6 Ondrej Zajicek
    u16 lsa_type = lsa->type_raw & type_mask;
1408
    u16 dscope = LSA_SCOPE(hea[i]->lsa_type);
1409 742029eb Ondrej Zajicek
1410 70945cb6 Ondrej Zajicek
    /* Hack: 1 is used for LSA_SCOPE_LINK, fixed by & 0xf000 */
1411 20ab192b Ondrej Zajicek
    if (ld->scope && (dscope != (ld->scope & 0xf000)))
1412
      continue;
1413
1414
    if ((ld->scope == LSA_SCOPE_AREA) && (hea[i]->domain != ld->area))
1415
      continue;
1416
1417 70945cb6 Ondrej Zajicek
    /* For user convenience ignore high nibble */
1418
    if (ld->type && ((lsa_type & 0x0fff) != (ld->type & 0x0fff)))
1419 20ab192b Ondrej Zajicek
      continue;
1420
1421
    if (ld->lsid && (lsa->id != ld->lsid))
1422
      continue;
1423
1424
    if (ld->router && (lsa->rt != ld->router))
1425
      continue;
1426 742029eb Ondrej Zajicek
1427 98899244 Ondrej Zajicek
    if ((dscope != last_dscope) || (hea[i]->domain != last_domain))
1428
    {
1429
      cli_msg(-1017, "");
1430
      switch (dscope)
1431
      {
1432 70945cb6 Ondrej Zajicek
      case LSA_SCOPE_AS:
1433
        cli_msg(-1017, "Global");
1434
        break;
1435
1436
      case LSA_SCOPE_AREA:
1437
        cli_msg(-1017, "Area %R", hea[i]->domain);
1438
        break;
1439
1440
      case LSA_SCOPE_LINK:
1441
        {
1442
          struct iface *ifa = if_find_by_index(hea[i]->domain);
1443
          cli_msg(-1017, "Link %s", (ifa != NULL) ? ifa->name : "?");
1444
        }
1445
        break;
1446 98899244 Ondrej Zajicek
      }
1447
      cli_msg(-1017, "");
1448 6f8bbaa1 Ondrej Zajicek
      cli_msg(-1017," Type   LS ID           Router          Sequence   Age  Checksum");
1449 98899244 Ondrej Zajicek
1450
      last_dscope = dscope;
1451
      last_domain = hea[i]->domain;
1452
    }
1453
1454 6f8bbaa1 Ondrej Zajicek
    cli_msg(-1017," %04x  %-15R %-15R  %08x %5u    %04x",
1455
            lsa_type, lsa->id, lsa->rt, lsa->sn, lsa->age, lsa->checksum);
1456 98899244 Ondrej Zajicek
  }
1457
  cli_msg(0, "");
1458
}
1459
1460
1461 80787d41 Ondrej Filip
struct protocol proto_ospf = {
1462 70945cb6 Ondrej Zajicek
  .name =                "OSPF",
1463
  .template =                "ospf%d",
1464
  .attr_class =                EAP_OSPF,
1465
  .preference =                DEF_PREF_OSPF,
1466 2bbc3083 Ondrej Zajicek
  .config_size =        sizeof(struct ospf_config),
1467 70945cb6 Ondrej Zajicek
  .init =                ospf_init,
1468
  .dump =                ospf_dump,
1469
  .start =                ospf_start,
1470
  .shutdown =                ospf_shutdown,
1471
  .reconfigure =        ospf_reconfigure,
1472
  .get_status =                ospf_get_status,
1473
  .get_attr =                ospf_get_attr,
1474
  .get_route_info =        ospf_get_route_info
1475 80787d41 Ondrej Filip
};