]>
git.ipfire.org Git - thirdparty/bird.git/blob - proto/ospf/ospf.c
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.
8 * Can be freely distributed and used under the terms of the GNU GPL.
12 * DOC: Open Shortest Path First (OSPF)
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.).
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.
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.
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.
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()).
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
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
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.
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.
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
108 static int ospf_preexport(struct proto
*P
, rte
**new, struct linpool
*pool
);
109 static void ospf_make_tmp_attrs(struct rte
*rt
, struct linpool
*pool
);
110 static void ospf_store_tmp_attrs(struct rte
*rt
, struct linpool
*pool
);
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
);
118 add_area_nets(struct ospf_area
*oa
, struct ospf_area_config
*ac
)
120 struct ospf_proto
*p
= oa
->po
;
121 struct area_net_config
*anc
;
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
);
129 WALK_LIST(anc
, ac
->net_list
)
131 an
= fib_get(&oa
->net_fib
, &anc
->prefix
);
132 an
->hidden
= anc
->hidden
;
135 WALK_LIST(anc
, ac
->enet_list
)
137 an
= fib_get(&oa
->enet_fib
, &anc
->prefix
);
138 an
->hidden
= anc
->hidden
;
144 ospf_opts(struct ospf_proto
*p
)
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);
154 ospf_area_add(struct ospf_proto
*p
, struct ospf_area_config
*ac
)
156 struct ospf_area
*oa
;
158 OSPF_TRACE(D_EVENTS
, "Adding area %R", ac
->areaid
);
160 oa
= mb_allocz(p
->p
.pool
, sizeof(struct ospf_area
));
161 add_tail(&p
->area_list
, NODE oa
);
165 oa
->areaid
= ac
->areaid
;
168 fib_init(&oa
->rtr
, p
->p
.pool
, NET_IP4
, sizeof(ort
), OFFSETOF(ort
, fn
), 0, NULL
);
169 add_area_nets(oa
, ac
);
174 oa
->options
= ac
->type
| ospf_opts(p
);
176 ospf_notify_rt_lsa(oa
);
180 ospf_flush_area(struct ospf_proto
*p
, u32 areaid
)
182 struct top_hash_entry
*en
;
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
);
190 ospf_area_remove(struct ospf_area
*oa
)
192 struct ospf_proto
*p
= oa
->po
;
193 OSPF_TRACE(D_EVENTS
, "Removing area %R", oa
->areaid
);
195 /* We suppose that interfaces are already removed */
196 ospf_flush_area(p
, oa
->areaid
);
199 fib_free(&oa
->net_fib
);
200 fib_free(&oa
->enet_fib
);
202 if (oa
->translator_timer
)
203 rfree(oa
->translator_timer
);
212 ospf_find_area(struct ospf_proto
*p
, u32 aid
)
214 struct ospf_area
*oa
;
215 WALK_LIST(oa
, p
->area_list
)
216 if (((struct ospf_area
*) oa
)->areaid
== aid
)
221 static struct ospf_iface
*
222 ospf_find_vlink(struct ospf_proto
*p
, u32 voa
, u32 vid
)
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
))
232 ospf_start(struct proto
*P
)
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
;
238 p
->router_id
= proto_get_router_id(P
->cf
);
240 p
->af_ext
= c
->af_ext
;
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
;
247 p
->vpn_pe
= c
->vpn_pe
;
250 p
->disp_timer
= tm_new_init(P
->pool
, ospf_disp
, p
, p
->tick S
, 0);
251 tm_start(p
->disp_timer
, 100 MS
);
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
);
260 idm_init(&p
->idm
, P
->pool
, 16);
262 p
->gr
= ospf_top_new(p
, P
->pool
);
263 s_init_list(&(p
->lsal
));
265 p
->flood_event
= ev_new_init(P
->pool
, ospf_flood_event
, p
);
267 p
->log_pkt_tbf
= (struct tbf
){ .rate
= 1, .burst
= 5 };
268 p
->log_lsa_tbf
= (struct tbf
){ .rate
= 4, .burst
= 20 };
270 WALK_LIST(ac
, c
->area_list
)
271 ospf_area_add(p
, ac
);
274 ospf_open_vlink_sk(p
);
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
);
285 ospf_dump(struct proto
*P
)
287 struct ospf_proto
*p
= (struct ospf_proto
*) P
;
288 struct ospf_iface
*ifa
;
289 struct ospf_neighbor
*n
;
291 OSPF_TRACE(D_EVENTS
, "Area number: %d", p
->areano
);
293 WALK_LIST(ifa
, p
->iface_list
)
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
)
301 OSPF_TRACE(D_EVENTS
, " neighbor %R in state %u", n
->rid
, n
->state
);
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");
313 static struct proto
*
314 ospf_init(struct proto_config
*CF
)
316 struct ospf_config
*cf
= (struct ospf_config
*) CF
;
317 struct proto
*P
= proto_new(CF
);
319 P
->main_channel
= proto_add_channel(P
, proto_cf_main_channel(CF
));
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
;
334 /* If new is better return 1 */
336 ospf_rte_better(struct rte
*new, struct rte
*old
)
338 if (new->u
.ospf
.metric1
== LSINFINITY
)
341 if(new->attrs
->source
< old
->attrs
->source
) return 1;
342 if(new->attrs
->source
> old
->attrs
->source
) return 0;
344 if(new->attrs
->source
== RTS_OSPF_EXT2
)
346 if(new->u
.ospf
.metric2
< old
->u
.ospf
.metric2
) return 1;
347 if(new->u
.ospf
.metric2
> old
->u
.ospf
.metric2
) return 0;
350 if (new->u
.ospf
.metric1
< old
->u
.ospf
.metric1
)
353 return 0; /* Old is shorter or same */
357 ospf_rte_same(struct rte
*new, struct rte
*old
)
359 /* new->attrs == old->attrs always */
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
;
369 ospf_schedule_rtcalc(struct ospf_proto
*p
)
374 OSPF_TRACE(D_EVENTS
, "Scheduling routing table calculation");
379 ospf_reload_routes(struct channel
*C
)
381 struct ospf_proto
*p
= (struct ospf_proto
*) C
->proto
;
386 OSPF_TRACE(D_EVENTS
, "Scheduling routing table calculation with route reload");
392 * ospf_disp - invokes routing table calculation, aging and also area_disp()
393 * @timer: timer usually called every @ospf_proto->tick second, @timer->data
394 * point to @ospf_proto
397 ospf_disp(timer
* timer
)
399 struct ospf_proto
*p
= timer
->data
;
401 /* Originate or flush local topology LSAs */
402 ospf_update_topology(p
);
405 ospf_update_lsadb(p
);
407 /* Calculate routing table */
414 * ospf_preexport - accept or reject new route from nest's routing table
415 * @P: OSPF protocol instance
416 * @new: the new route
417 * @attrs: list of attributes
418 * @pool: pool for allocation of attributes
420 * Its quite simple. It does not accept our own routes and leaves the decision on
421 * import to the filters.
424 ospf_preexport(struct proto
*P
, rte
**new, struct linpool
*pool UNUSED
)
426 struct ospf_proto
*p
= (struct ospf_proto
*) P
;
427 struct ospf_area
*oa
= ospf_main_area(p
);
430 /* Reject our own routes */
431 if (e
->attrs
->src
->proto
== P
)
434 /* Do not export routes to stub areas */
442 ospf_make_tmp_attrs(struct rte
*rt
, struct linpool
*pool
)
444 rte_init_tmp_attrs(rt
, pool
, 4);
445 rte_make_tmp_attr(rt
, EA_OSPF_METRIC1
, EAF_TYPE_INT
, rt
->u
.ospf
.metric1
);
446 rte_make_tmp_attr(rt
, EA_OSPF_METRIC2
, EAF_TYPE_INT
, rt
->u
.ospf
.metric2
);
447 rte_make_tmp_attr(rt
, EA_OSPF_TAG
, EAF_TYPE_INT
, rt
->u
.ospf
.tag
);
448 rte_make_tmp_attr(rt
, EA_OSPF_ROUTER_ID
, EAF_TYPE_ROUTER_ID
, rt
->u
.ospf
.router_id
);
452 ospf_store_tmp_attrs(struct rte
*rt
, struct linpool
*pool
)
454 rte_init_tmp_attrs(rt
, pool
, 4);
455 rt
->u
.ospf
.metric1
= rte_store_tmp_attr(rt
, EA_OSPF_METRIC1
);
456 rt
->u
.ospf
.metric2
= rte_store_tmp_attr(rt
, EA_OSPF_METRIC2
);
457 rt
->u
.ospf
.tag
= rte_store_tmp_attr(rt
, EA_OSPF_TAG
);
458 rt
->u
.ospf
.router_id
= rte_store_tmp_attr(rt
, EA_OSPF_ROUTER_ID
);
462 * ospf_shutdown - Finish of OSPF instance
463 * @P: OSPF protocol instance
465 * RFC does not define any action that should be taken before router
466 * shutdown. To make my neighbors react as fast as possible, I send
467 * them hello packet with empty neighbor list. They should start
468 * their neighbor state machine with event %NEIGHBOR_1WAY.
471 ospf_shutdown(struct proto
*P
)
473 struct ospf_proto
*p
= (struct ospf_proto
*) P
;
474 struct ospf_iface
*ifa
;
476 OSPF_TRACE(D_EVENTS
, "Shutdown requested");
478 /* And send to all my neighbors 1WAY */
479 WALK_LIST(ifa
, p
->iface_list
)
480 ospf_iface_shutdown(ifa
);
482 /* Cleanup locked rta entries */
483 FIB_WALK(&p
->rtf
, ort
, nf
)
485 rta_free(nf
->old_rta
);
493 ospf_get_status(struct proto
*P
, byte
* buf
)
495 struct ospf_proto
*p
= (struct ospf_proto
*) P
;
497 if (p
->p
.proto_state
== PS_DOWN
)
501 struct ospf_iface
*ifa
;
502 struct ospf_neighbor
*n
;
505 WALK_LIST(ifa
, p
->iface_list
)
506 WALK_LIST(n
, ifa
->neigh_list
) if (n
->state
== NEIGHBOR_FULL
)
510 strcpy(buf
, "Alone");
512 strcpy(buf
, "Running");
517 ospf_get_route_info(rte
* rte
, byte
* buf
)
519 char *type
= "<bug>";
521 switch (rte
->attrs
->source
)
537 buf
+= bsprintf(buf
, " %s", type
);
538 buf
+= bsprintf(buf
, " (%d/%d", rte
->pref
, rte
->u
.ospf
.metric1
);
539 if (rte
->attrs
->source
== RTS_OSPF_EXT2
)
540 buf
+= bsprintf(buf
, "/%d", rte
->u
.ospf
.metric2
);
541 buf
+= bsprintf(buf
, ")");
542 if ((rte
->attrs
->source
== RTS_OSPF_EXT1
|| rte
->attrs
->source
== RTS_OSPF_EXT2
) && rte
->u
.ospf
.tag
)
544 buf
+= bsprintf(buf
, " [%x]", rte
->u
.ospf
.tag
);
546 if (rte
->u
.ospf
.router_id
)
547 buf
+= bsprintf(buf
, " [%R]", rte
->u
.ospf
.router_id
);
551 ospf_get_attr(eattr
* a
, byte
* buf
, int buflen UNUSED
)
555 case EA_OSPF_METRIC1
:
556 bsprintf(buf
, "metric1");
558 case EA_OSPF_METRIC2
:
559 bsprintf(buf
, "metric2");
562 bsprintf(buf
, "tag: 0x%08x", a
->u
.data
);
564 case EA_OSPF_ROUTER_ID
:
565 bsprintf(buf
, "router_id");
573 ospf_area_reconfigure(struct ospf_area
*oa
, struct ospf_area_config
*nac
)
575 struct ospf_proto
*p
= oa
->po
;
576 struct ospf_area_config
*oac
= oa
->ac
;
577 struct ospf_iface
*ifa
, *ifx
;
580 oa
->options
= nac
->type
| ospf_opts(p
);
582 if (nac
->type
!= oac
->type
)
584 log(L_INFO
"%s: Restarting area %R", p
->p
.name
, oa
->areaid
);
586 /* Remove area interfaces, will be re-added later */
587 WALK_LIST_DELSAFE(ifa
, ifx
, p
->iface_list
)
590 ospf_iface_shutdown(ifa
);
591 ospf_iface_remove(ifa
);
594 /* Flush area LSAs */
595 ospf_flush_area(p
, oa
->areaid
);
598 /* Handle net_list */
599 fib_free(&oa
->net_fib
);
600 fib_free(&oa
->enet_fib
);
601 add_area_nets(oa
, nac
);
603 /* No need to handle stubnet_list */
606 ospf_notify_rt_lsa(oa
);
610 * ospf_reconfigure - reconfiguration hook
611 * @P: current instance of protocol (with old configuration)
612 * @c: new configuration requested by user
614 * This hook tries to be a little bit intelligent. Instance of OSPF
615 * will survive change of many constants like hello interval,
616 * password change, addition or deletion of some neighbor on
617 * nonbroadcast network, cost of interface, etc.
620 ospf_reconfigure(struct proto
*P
, struct proto_config
*CF
)
622 struct ospf_proto
*p
= (struct ospf_proto
*) P
;
623 struct ospf_config
*old
= (struct ospf_config
*) (P
->cf
);
624 struct ospf_config
*new = (struct ospf_config
*) CF
;
625 struct ospf_area_config
*oac
, *nac
;
626 struct ospf_area
*oa
, *oax
;
627 struct ospf_iface
*ifa
, *ifx
;
628 struct ospf_iface_patt
*ip
;
630 if (proto_get_router_id(CF
) != p
->router_id
)
633 if (p
->ospf2
!= new->ospf2
)
636 if (p
->rfc1583
!= new->rfc1583
)
639 if (p
->instance_id
!= new->instance_id
)
642 if (old
->abr
!= new->abr
)
647 oac
= HEAD(old
->area_list
);
648 nac
= HEAD(new->area_list
);
650 if (oac
->type
!= nac
->type
)
654 if (old
->vpn_pe
!= new->vpn_pe
)
657 if ((p
->af_ext
!= new->af_ext
) || (p
->af_mc
!= new->af_mc
))
660 if (!proto_configure_channel(P
, &P
->main_channel
, proto_cf_main_channel(CF
)))
663 p
->stub_router
= new->stub_router
;
664 p
->merge_external
= new->merge_external
;
668 p
->disp_timer
->recurrent
= p
->tick S
;
669 tm_start(p
->disp_timer
, 10 MS
);
671 /* Mark all areas and ifaces */
672 WALK_LIST(oa
, p
->area_list
)
675 WALK_LIST(ifa
, p
->iface_list
)
678 /* Add and update areas */
679 WALK_LIST(nac
, new->area_list
)
681 oa
= ospf_find_area(p
, nac
->areaid
);
683 ospf_area_reconfigure(oa
, nac
);
685 ospf_area_add(p
, nac
);
688 /* Add and update interfaces */
689 ospf_reconfigure_ifaces(p
);
691 /* Add and update vlinks */
692 WALK_LIST(ip
, new->vlink_list
)
694 ifa
= ospf_find_vlink(p
, ip
->voa
, ip
->vid
);
696 ospf_iface_reconfigure(ifa
, ip
);
698 ospf_iface_new_vlink(p
, ip
);
701 /* Delete remaining ifaces and areas */
702 WALK_LIST_DELSAFE(ifa
, ifx
, p
->iface_list
)
705 ospf_iface_shutdown(ifa
);
706 ospf_iface_remove(ifa
);
709 WALK_LIST_DELSAFE(oa
, oax
, p
->area_list
)
711 ospf_area_remove(oa
);
713 ospf_schedule_rtcalc(p
);
720 ospf_sh_neigh(struct proto
*P
, char *iff
)
722 struct ospf_proto
*p
= (struct ospf_proto
*) P
;
723 struct ospf_iface
*ifa
= NULL
;
724 struct ospf_neighbor
*n
;
726 if (p
->p
.proto_state
!= PS_UP
)
728 cli_msg(-1013, "%s: is not up", p
->p
.name
);
733 cli_msg(-1013, "%s:", p
->p
.name
);
734 cli_msg(-1013, "%-12s\t%3s\t%-15s\t%-5s\t%-10s %-12s", "Router ID", "Pri",
735 " State", "DTime", "Interface", "Router IP");
736 WALK_LIST(ifa
, p
->iface_list
)
737 if ((iff
== NULL
) || patmatch(iff
, ifa
->ifname
))
738 WALK_LIST(n
, ifa
->neigh_list
)
739 ospf_sh_neigh_info(n
);
744 ospf_sh(struct proto
*P
)
746 struct ospf_proto
*p
= (struct ospf_proto
*) P
;
747 struct ospf_area
*oa
;
748 struct ospf_iface
*ifa
;
749 struct ospf_neighbor
*n
;
750 int ifano
, nno
, adjno
, firstfib
;
752 if (p
->p
.proto_state
!= PS_UP
)
754 cli_msg(-1014, "%s: is not up", p
->p
.name
);
759 cli_msg(-1014, "%s:", p
->p
.name
);
760 cli_msg(-1014, "RFC1583 compatibility: %s", (p
->rfc1583
? "enabled" : "disabled"));
761 cli_msg(-1014, "Stub router: %s", (p
->stub_router
? "Yes" : "No"));
762 cli_msg(-1014, "RT scheduler tick: %d", p
->tick
);
763 cli_msg(-1014, "Number of areas: %u", p
->areano
);
764 cli_msg(-1014, "Number of LSAs in DB:\t%u", p
->gr
->hash_entries
);
766 WALK_LIST(oa
, p
->area_list
)
768 cli_msg(-1014, "\tArea: %R (%u) %s", oa
->areaid
, oa
->areaid
,
769 oa
->areaid
== 0 ? "[BACKBONE]" : "");
773 WALK_LIST(ifa
, p
->iface_list
)
778 WALK_LIST(n
, ifa
->neigh_list
)
781 if (n
->state
== NEIGHBOR_FULL
)
787 cli_msg(-1014, "\t\tStub:\t%s", oa_is_stub(oa
) ? "Yes" : "No");
788 cli_msg(-1014, "\t\tNSSA:\t%s", oa_is_nssa(oa
) ? "Yes" : "No");
789 cli_msg(-1014, "\t\tTransit:\t%s", oa
->trcap
? "Yes" : "No");
792 cli_msg(-1014, "\t\tNSSA translation:\t%s%s", oa
->translate
? "Yes" : "No",
793 oa
->translate
== TRANS_WAIT
? " (run down)" : "");
794 cli_msg(-1014, "\t\tNumber of interfaces:\t%u", ifano
);
795 cli_msg(-1014, "\t\tNumber of neighbors:\t%u", nno
);
796 cli_msg(-1014, "\t\tNumber of adjacent neighbors:\t%u", adjno
);
799 FIB_WALK(&oa
->net_fib
, struct area_net
, anet
)
803 cli_msg(-1014, "\t\tArea networks:");
806 cli_msg(-1014, "\t\t\t%1N\t%s\t%s", anet
->fn
.addr
,
807 anet
->hidden
? "Hidden" : "Advertise", anet
->active
? "Active" : "");
812 FIB_WALK(&oa
->enet_fib
, struct area_net
, anet
)
816 cli_msg(-1014, "\t\tArea external networks:");
819 cli_msg(-1014, "\t\t\t%1N\t%s\t%s", anet
->fn
.addr
,
820 anet
->hidden
? "Hidden" : "Advertise", anet
->active
? "Active" : "");
829 ospf_sh_iface(struct proto
*P
, char *iff
)
831 struct ospf_proto
*p
= (struct ospf_proto
*) P
;
832 struct ospf_iface
*ifa
= NULL
;
834 if (p
->p
.proto_state
!= PS_UP
)
836 cli_msg(-1015, "%s: is not up", p
->p
.name
);
841 cli_msg(-1015, "%s:", p
->p
.name
);
842 WALK_LIST(ifa
, p
->iface_list
)
843 if ((iff
== NULL
) || patmatch(iff
, ifa
->ifname
))
844 ospf_iface_info(ifa
);
848 /* lsa_compare_for_state() - Compare function for 'show ospf state'
850 * First we want to separate network-LSAs and other LSAs (because network-LSAs
851 * will be presented as network nodes and other LSAs together as router nodes)
852 * Network-LSAs are sorted according to network prefix, other LSAs are sorted
853 * according to originating router id (to get all LSA needed to represent one
854 * router node together). Then, according to LSA type, ID and age.
856 * For OSPFv3, we have to handle also Prefix-LSAs. We would like to put each
857 * immediately after the referenced LSA. We will make faked LSA based on ref_
861 static struct ospf_lsa_header
*
862 fake_lsa_from_prefix_lsa(struct ospf_lsa_header
*dst
, struct ospf_lsa_header
*src
,
863 struct ospf_lsa_prefix
*px
)
866 dst
->type_raw
= px
->ref_type
;
867 dst
->id
= px
->ref_id
;
868 dst
->rt
= px
->ref_rt
;
875 static int lsa_compare_ospf3
;
878 lsa_compare_for_state(const void *p1
, const void *p2
)
880 struct top_hash_entry
*he1
= * (struct top_hash_entry
**) p1
;
881 struct top_hash_entry
*he2
= * (struct top_hash_entry
**) p2
;
882 struct ospf_lsa_header
*lsa1
= &(he1
->lsa
);
883 struct ospf_lsa_header
*lsa2
= &(he2
->lsa
);
884 struct ospf_lsa_header lsatmp1
, lsatmp2
;
885 u16 lsa1_type
= he1
->lsa_type
;
886 u16 lsa2_type
= he2
->lsa_type
;
888 if (he1
->domain
< he2
->domain
)
890 if (he1
->domain
> he2
->domain
)
894 /* px1 or px2 assumes OSPFv3 */
895 int px1
= (lsa1_type
== LSA_T_PREFIX
);
896 int px2
= (lsa2_type
== LSA_T_PREFIX
);
900 lsa1
= fake_lsa_from_prefix_lsa(&lsatmp1
, lsa1
, he1
->lsa_body
);
901 lsa1_type
= lsa1
->type_raw
; /* FIXME: handle unknown ref_type */
906 lsa2
= fake_lsa_from_prefix_lsa(&lsatmp2
, lsa2
, he2
->lsa_body
);
907 lsa2_type
= lsa2
->type_raw
;
911 int nt1
= (lsa1_type
== LSA_T_NET
);
912 int nt2
= (lsa2_type
== LSA_T_NET
);
919 /* In OSPFv3, networks are named based on ID of DR */
920 if (lsa_compare_ospf3
)
922 if (lsa1
->rt
< lsa2
->rt
)
924 if (lsa1
->rt
> lsa2
->rt
)
928 /* For OSPFv2, this is IP of the network,
929 for OSPFv3, this is interface ID */
930 if (lsa1
->id
< lsa2
->id
)
932 if (lsa1
->id
> lsa2
->id
)
938 return lsa1
->sn
- lsa2
->sn
;
942 if (lsa1
->rt
< lsa2
->rt
)
944 if (lsa1
->rt
> lsa2
->rt
)
947 if (lsa1_type
< lsa2_type
)
949 if (lsa1_type
> lsa2_type
)
952 if (lsa1
->id
< lsa2
->id
)
954 if (lsa1
->id
> lsa2
->id
)
960 return lsa1
->sn
- lsa2
->sn
;
965 ext_compare_for_state(const void *p1
, const void *p2
)
967 struct top_hash_entry
* he1
= * (struct top_hash_entry
**) p1
;
968 struct top_hash_entry
* he2
= * (struct top_hash_entry
**) p2
;
969 struct ospf_lsa_header
*lsa1
= &(he1
->lsa
);
970 struct ospf_lsa_header
*lsa2
= &(he2
->lsa
);
972 if (lsa1
->rt
< lsa2
->rt
)
974 if (lsa1
->rt
> lsa2
->rt
)
977 if (lsa1
->id
< lsa2
->id
)
979 if (lsa1
->id
> lsa2
->id
)
982 return lsa1
->sn
- lsa2
->sn
;
986 show_lsa_distance(struct top_hash_entry
*he
)
988 if (he
->color
== INSPF
)
989 cli_msg(-1016, "\t\tdistance %u", he
->dist
);
991 cli_msg(-1016, "\t\tunreachable");
995 show_lsa_router(struct ospf_proto
*p
, struct top_hash_entry
*he
, int verbose
)
997 struct ospf_lsa_rt_walk rtl
;
1000 cli_msg(-1016, "\trouter %R", he
->lsa
.rt
);
1001 show_lsa_distance(he
);
1003 lsa_walk_rt_init(p
, he
, &rtl
);
1004 while (lsa_walk_rt(&rtl
))
1005 if (rtl
.type
== LSART_VLNK
)
1006 cli_msg(-1016, "\t\tvlink %R metric %u", rtl
.id
, rtl
.metric
);
1008 lsa_walk_rt_init(p
, he
, &rtl
);
1009 while (lsa_walk_rt(&rtl
))
1010 if (rtl
.type
== LSART_PTP
)
1011 cli_msg(-1016, "\t\trouter %R metric %u", rtl
.id
, rtl
.metric
);
1013 lsa_walk_rt_init(p
, he
, &rtl
);
1014 while (lsa_walk_rt(&rtl
))
1015 if (rtl
.type
== LSART_NET
)
1019 /* In OSPFv2, we try to find network-LSA to get prefix/pxlen */
1020 struct top_hash_entry
*net_he
= ospf_hash_find_net2(p
->gr
, he
->domain
, rtl
.id
);
1022 if (net_he
&& (net_he
->lsa
.age
< LSA_MAXAGE
))
1024 struct ospf_lsa_header
*net_lsa
= &(net_he
->lsa
);
1025 struct ospf_lsa_net
*net_ln
= net_he
->lsa_body
;
1027 cli_msg(-1016, "\t\tnetwork %I/%d metric %u",
1028 ipa_from_u32(net_lsa
->id
& net_ln
->optx
),
1029 u32_masklen(net_ln
->optx
), rtl
.metric
);
1032 cli_msg(-1016, "\t\tnetwork [%R] metric %u", rtl
.id
, rtl
.metric
);
1035 cli_msg(-1016, "\t\tnetwork [%R-%u] metric %u", rtl
.id
, rtl
.nif
, rtl
.metric
);
1038 if (ospf_is_v2(p
) && verbose
)
1040 lsa_walk_rt_init(p
, he
, &rtl
);
1041 while (lsa_walk_rt(&rtl
))
1042 if (rtl
.type
== LSART_STUB
)
1043 cli_msg(-1016, "\t\tstubnet %I/%d metric %u",
1044 ipa_from_u32(rtl
.id
), u32_masklen(rtl
.data
), rtl
.metric
);
1049 show_lsa_network(struct top_hash_entry
*he
, int ospf2
)
1051 struct ospf_lsa_header
*lsa
= &(he
->lsa
);
1052 struct ospf_lsa_net
*ln
= he
->lsa_body
;
1058 cli_msg(-1016, "\tnetwork %I/%d", ipa_from_u32(lsa
->id
& ln
->optx
), u32_masklen(ln
->optx
));
1059 cli_msg(-1016, "\t\tdr %R", lsa
->rt
);
1064 cli_msg(-1016, "\tnetwork [%R-%u]", lsa
->rt
, lsa
->id
);
1067 show_lsa_distance(he
);
1069 for (i
= 0; i
< lsa_net_count(lsa
); i
++)
1070 cli_msg(-1016, "\t\trouter %R", ln
->routers
[i
]);
1074 show_lsa_sum_net(struct top_hash_entry
*he
, int ospf2
, int af
)
1080 lsa_parse_sum_net(he
, ospf2
, af
, &net
, &pxopts
, &metric
);
1081 cli_msg(-1016, "\t\txnetwork %N metric %u", &net
, metric
);
1085 show_lsa_sum_rt(struct top_hash_entry
*he
, int ospf2
)
1091 lsa_parse_sum_rt(he
, ospf2
, &dst_rid
, &metric
, &options
);
1092 cli_msg(-1016, "\t\txrouter %R metric %u", dst_rid
, metric
);
1097 show_lsa_external(struct top_hash_entry
*he
, int ospf2
, int af
)
1099 struct ospf_lsa_ext_local rt
;
1100 char str_via
[IPA_MAX_TEXT_LENGTH
+ 8] = "";
1101 char str_tag
[16] = "";
1103 if (he
->lsa_type
== LSA_T_EXT
)
1104 he
->domain
= 0; /* Unmark the LSA */
1106 lsa_parse_ext(he
, ospf2
, af
, &rt
);
1109 bsprintf(str_via
, " via %I", rt
.fwaddr
);
1112 bsprintf(str_tag
, " tag %08x", rt
.tag
);
1114 cli_msg(-1016, "\t\t%s %N metric%s %u%s%s",
1115 (he
->lsa_type
== LSA_T_NSSA
) ? "nssa-ext" : "external",
1116 &rt
.net
, rt
.ebit
? "2" : "", rt
.metric
, str_via
, str_tag
);
1120 show_lsa_prefix(struct top_hash_entry
*he
, struct top_hash_entry
*cnode
, int af
)
1122 struct ospf_lsa_prefix
*px
= he
->lsa_body
;
1126 /* We check whether given prefix-LSA is related to the current node */
1127 if ((px
->ref_type
!= cnode
->lsa
.type_raw
) || (px
->ref_rt
!= cnode
->lsa
.rt
))
1130 if ((px
->ref_type
== LSA_T_RT
) && (px
->ref_id
!= 0))
1133 if ((px
->ref_type
== LSA_T_NET
) && (px
->ref_id
!= cnode
->lsa
.id
))
1137 for (i
= 0; i
< px
->pxcount
; i
++)
1143 buf
= ospf3_get_prefix(buf
, af
, &net
, &pxopts
, &metric
);
1145 if (px
->ref_type
== LSA_T_RT
)
1146 cli_msg(-1016, "\t\tstubnet %N metric %u", &net
, metric
);
1148 cli_msg(-1016, "\t\taddress %N", &net
);
1153 ospf_sh_state(struct proto
*P
, int verbose
, int reachable
)
1155 struct ospf_proto
*p
= (struct ospf_proto
*) P
;
1156 int ospf2
= ospf_is_v2(p
);
1157 int af
= ospf_get_af(p
);
1159 u32 last_area
= 0xFFFFFFFF;
1161 if (p
->p
.proto_state
!= PS_UP
)
1163 cli_msg(-1016, "%s: is not up", p
->p
.name
);
1168 /* We store interesting area-scoped LSAs in array hea and
1169 global-scoped (LSA_T_EXT) LSAs in array hex */
1171 uint num
= p
->gr
->hash_entries
;
1172 struct top_hash_entry
*hea
[num
];
1173 struct top_hash_entry
*hex
[verbose
? num
: 0];
1174 struct top_hash_entry
*he
;
1175 struct top_hash_entry
*cnode
= NULL
;
1178 WALK_SLIST(he
, p
->lsal
)
1182 if (he
->lsa
.age
== LSA_MAXAGE
)
1185 switch (he
->lsa_type
)
1202 he
->domain
= 1; /* Abuse domain field to mark the LSA */
1214 ASSERT(j1
<= num
&& jx
<= num
);
1216 lsa_compare_ospf3
= !ospf2
;
1217 qsort(hea
, j1
, sizeof(struct top_hash_entry
*), lsa_compare_for_state
);
1218 qsort(hex
, jx
, sizeof(struct top_hash_entry
*), ext_compare_for_state
);
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
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
1240 for (i
= 0; i
< j1
; i
++)
1244 /* If there is no opened node, we open the LSA (if appropriate) or skip to the next one */
1247 if (((he
->lsa_type
== LSA_T_RT
) || (he
->lsa_type
== LSA_T_NET
))
1248 && ((he
->color
== INSPF
) || !reachable
))
1252 if (he
->domain
!= last_area
)
1255 cli_msg(-1016, "area %R", he
->domain
);
1256 last_area
= he
->domain
;
1264 ASSERT(cnode
&& (he
->domain
== last_area
) && (he
->lsa
.rt
== cnode
->lsa
.rt
));
1266 switch (he
->lsa_type
)
1269 if (he
->lsa
.id
== cnode
->lsa
.id
)
1270 show_lsa_router(p
, he
, verbose
);
1274 show_lsa_network(he
, ospf2
);
1278 if (cnode
->lsa_type
== LSA_T_RT
)
1279 show_lsa_sum_net(he
, ospf2
, af
);
1283 if (cnode
->lsa_type
== LSA_T_RT
)
1284 show_lsa_sum_rt(he
, ospf2
);
1289 show_lsa_external(he
, ospf2
, af
);
1293 show_lsa_prefix(he
, cnode
, af
);
1297 /* In these cases, we close the current node */
1299 || (hea
[i
+1]->domain
!= last_area
)
1300 || (hea
[i
+1]->lsa
.rt
!= cnode
->lsa
.rt
)
1301 || (hea
[i
+1]->lsa_type
== LSA_T_NET
))
1303 while ((ix
< jx
) && (hex
[ix
]->lsa
.rt
< cnode
->lsa
.rt
))
1306 while ((ix
< jx
) && (hex
[ix
]->lsa
.rt
== cnode
->lsa
.rt
))
1307 show_lsa_external(hex
[ix
++], ospf2
, af
);
1314 u32 last_rt
= 0xFFFFFFFF;
1315 for (ix
= 0; ix
< jx
; ix
++)
1319 /* If it is still marked, we show it now. */
1324 if ((he
->color
!= INSPF
) && reachable
)
1330 cli_msg(-1016, "other ASBRs");
1334 if (he
->lsa
.rt
!= last_rt
)
1337 cli_msg(-1016, "\trouter %R", he
->lsa
.rt
);
1338 last_rt
= he
->lsa
.rt
;
1341 show_lsa_external(he
, ospf2
, af
);
1350 lsa_compare_for_lsadb(const void *p1
, const void *p2
)
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 int sc1
= LSA_SCOPE(he1
->lsa_type
);
1357 int sc2
= LSA_SCOPE(he2
->lsa_type
);
1362 if (he1
->domain
!= he2
->domain
)
1363 return he1
->domain
- he2
->domain
;
1365 if (lsa1
->rt
!= lsa2
->rt
)
1366 return lsa1
->rt
- lsa2
->rt
;
1368 if (lsa1
->id
!= lsa2
->id
)
1369 return lsa1
->id
- lsa2
->id
;
1371 if (he1
->lsa_type
!= he2
->lsa_type
)
1372 return he1
->lsa_type
- he2
->lsa_type
;
1374 return lsa1
->sn
- lsa2
->sn
;
1378 ospf_sh_lsadb(struct lsadb_show_data
*ld
)
1380 struct ospf_proto
*p
= (struct ospf_proto
*) proto_get_named(ld
->name
, &proto_ospf
);
1381 uint num
= p
->gr
->hash_entries
;
1383 int last_dscope
= -1;
1384 u32 last_domain
= 0;
1385 u16 type_mask
= ospf_is_v2(p
) ? 0x00ff : 0xffff; /* see lsa_etype() */
1387 if (p
->p
.proto_state
!= PS_UP
)
1389 cli_msg(-1017, "%s: is not up", p
->p
.name
);
1394 if (ld
->router
== SH_ROUTER_SELF
)
1395 ld
->router
= p
->router_id
;
1397 struct top_hash_entry
*hea
[num
];
1398 struct top_hash_entry
*he
;
1401 WALK_SLIST(he
, p
->lsal
)
1407 qsort(hea
, j
, sizeof(struct top_hash_entry
*), lsa_compare_for_lsadb
);
1409 for (i
= 0; i
< j
; i
++)
1411 struct ospf_lsa_header
*lsa
= &(hea
[i
]->lsa
);
1412 u16 lsa_type
= lsa
->type_raw
& type_mask
;
1413 u16 dscope
= LSA_SCOPE(hea
[i
]->lsa_type
);
1415 /* Hack: 1 is used for LSA_SCOPE_LINK, fixed by & 0xf000 */
1416 if (ld
->scope
&& (dscope
!= (ld
->scope
& 0xf000)))
1419 if ((ld
->scope
== LSA_SCOPE_AREA
) && (hea
[i
]->domain
!= ld
->area
))
1422 /* For user convenience ignore high nibble */
1423 if (ld
->type
&& ((lsa_type
& 0x0fff) != (ld
->type
& 0x0fff)))
1426 if (ld
->lsid
&& (lsa
->id
!= ld
->lsid
))
1429 if (ld
->router
&& (lsa
->rt
!= ld
->router
))
1432 if ((dscope
!= last_dscope
) || (hea
[i
]->domain
!= last_domain
))
1438 cli_msg(-1017, "Global");
1441 case LSA_SCOPE_AREA
:
1442 cli_msg(-1017, "Area %R", hea
[i
]->domain
);
1445 case LSA_SCOPE_LINK
:
1447 struct iface
*ifa
= if_find_by_index(hea
[i
]->domain
);
1448 cli_msg(-1017, "Link %s", (ifa
!= NULL
) ? ifa
->name
: "?");
1453 cli_msg(-1017," Type LS ID Router Sequence Age Checksum");
1455 last_dscope
= dscope
;
1456 last_domain
= hea
[i
]->domain
;
1459 cli_msg(-1017," %04x %-15R %-15R %08x %5u %04x",
1460 lsa_type
, lsa
->id
, lsa
->rt
, lsa
->sn
, lsa
->age
, lsa
->checksum
);
1466 struct protocol proto_ospf
= {
1468 .template = "ospf%d",
1469 .class = PROTOCOL_OSPF
,
1470 .preference
= DEF_PREF_OSPF
,
1471 .channel_mask
= NB_IP
,
1472 .proto_size
= sizeof(struct ospf_proto
),
1473 .config_size
= sizeof(struct ospf_config
),
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