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