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