Statistics
| Branch: | Revision:

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

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