2 * OSPF Interface functions.
3 * Copyright (C) 1999, 2000 Toshiaki Takada
5 * This file is part of GNU Zebra.
7 * GNU Zebra is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation; either version 2, or (at your
10 * option) any later version.
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
36 #include "ospfd/ospfd.h"
37 #include "ospfd/ospf_spf.h"
38 #include "ospfd/ospf_interface.h"
39 #include "ospfd/ospf_ism.h"
40 #include "ospfd/ospf_asbr.h"
41 #include "ospfd/ospf_lsa.h"
42 #include "ospfd/ospf_lsdb.h"
43 #include "ospfd/ospf_neighbor.h"
44 #include "ospfd/ospf_nsm.h"
45 #include "ospfd/ospf_packet.h"
46 #include "ospfd/ospf_abr.h"
47 #include "ospfd/ospf_network.h"
48 #include "ospfd/ospf_dump.h"
50 DEFINE_QOBJ_TYPE(ospf_interface
)
51 DEFINE_HOOK(ospf_vl_add
, (struct ospf_vl_data
* vd
), (vd
))
52 DEFINE_HOOK(ospf_vl_delete
, (struct ospf_vl_data
* vd
), (vd
))
54 int ospf_if_get_output_cost(struct ospf_interface
*oi
)
56 /* If all else fails, use default OSPF cost */
60 /* ifp speed and bw can be 0 in some platforms, use ospf default bw
61 if bw is configured under interface it would be used.
63 if (!oi
->ifp
->bandwidth
&& oi
->ifp
->speed
)
66 bw
= oi
->ifp
->bandwidth
? oi
->ifp
->bandwidth
67 : OSPF_DEFAULT_BANDWIDTH
;
68 refbw
= oi
->ospf
->ref_bandwidth
;
70 /* A specifed ip ospf cost overrides a calculated one. */
71 if (OSPF_IF_PARAM_CONFIGURED(IF_DEF_PARAMS(oi
->ifp
), output_cost_cmd
)
72 || OSPF_IF_PARAM_CONFIGURED(oi
->params
, output_cost_cmd
))
73 cost
= OSPF_IF_PARAM(oi
, output_cost_cmd
);
74 /* See if a cost can be calculated from the zebra processes
75 interface bandwidth field. */
77 cost
= (u_int32_t
)((double)refbw
/ (double)bw
+ (double)0.5);
80 else if (cost
> 65535)
87 void ospf_if_recalculate_output_cost(struct interface
*ifp
)
90 struct route_node
*rn
;
92 for (rn
= route_top(IF_OIFS(ifp
)); rn
; rn
= route_next(rn
)) {
93 struct ospf_interface
*oi
;
95 if ((oi
= rn
->info
) == NULL
)
98 newcost
= ospf_if_get_output_cost(oi
);
100 /* Is actual output cost changed? */
101 if (oi
->output_cost
!= newcost
) {
102 oi
->output_cost
= newcost
;
103 ospf_router_lsa_update_area(oi
->area
);
108 /* Simulate down/up on the interface. This is needed, for example, when
110 void ospf_if_reset(struct interface
*ifp
)
112 struct route_node
*rn
;
114 for (rn
= route_top(IF_OIFS(ifp
)); rn
; rn
= route_next(rn
)) {
115 struct ospf_interface
*oi
;
117 if ((oi
= rn
->info
) == NULL
)
125 void ospf_if_reset_variables(struct ospf_interface
*oi
)
127 /* Set default values. */
128 /* don't clear this flag. oi->flag = OSPF_IF_DISABLE; */
131 oi
->type
= OSPF_IFTYPE_VIRTUALLINK
;
133 /* preserve network-type */
134 if (oi
->type
!= OSPF_IFTYPE_NBMA
)
135 oi
->type
= OSPF_IFTYPE_BROADCAST
;
137 oi
->state
= ISM_Down
;
139 oi
->crypt_seqnum
= 0;
141 /* This must be short, (less than RxmtInterval)
142 - RFC 2328 Section 13.5 para 3. Set to 1 second to avoid Acks being
143 held back for too long - MAG */
147 /* lookup oi for specified prefix/ifp */
148 struct ospf_interface
*ospf_if_table_lookup(struct interface
*ifp
,
149 struct prefix
*prefix
)
152 struct route_node
*rn
;
153 struct ospf_interface
*rninfo
= NULL
;
156 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
158 /* route_node_get implicitely locks */
159 if ((rn
= route_node_lookup(IF_OIFS(ifp
), &p
))) {
160 rninfo
= (struct ospf_interface
*)rn
->info
;
161 route_unlock_node(rn
);
167 static void ospf_add_to_if(struct interface
*ifp
, struct ospf_interface
*oi
)
169 struct route_node
*rn
;
173 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
176 rn
= route_node_get(IF_OIFS(ifp
), &p
);
177 /* rn->info should either be NULL or equal to this oi
178 * as route_node_get may return an existing node
180 assert(!rn
->info
|| rn
->info
== oi
);
184 static void ospf_delete_from_if(struct interface
*ifp
,
185 struct ospf_interface
*oi
)
187 struct route_node
*rn
;
191 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
193 rn
= route_node_lookup(IF_OIFS(oi
->ifp
), &p
);
197 route_unlock_node(rn
);
198 route_unlock_node(rn
);
201 struct ospf_interface
*ospf_if_new(struct ospf
*ospf
, struct interface
*ifp
,
204 struct ospf_interface
*oi
;
206 if ((oi
= ospf_if_table_lookup(ifp
, p
)) == NULL
) {
207 oi
= XCALLOC(MTYPE_OSPF_IF
, sizeof(struct ospf_interface
));
208 memset(oi
, 0, sizeof(struct ospf_interface
));
212 /* Set zebra interface pointer. */
216 ospf_add_to_if(ifp
, oi
);
217 listnode_add(ospf
->oiflist
, oi
);
219 /* Initialize neighbor list. */
220 oi
->nbrs
= route_table_init();
222 /* Initialize static neighbor list. */
223 oi
->nbr_nbma
= list_new();
225 /* Initialize Link State Acknowledgment list. */
226 oi
->ls_ack
= list_new();
227 oi
->ls_ack_direct
.ls_ack
= list_new();
229 /* Set default values. */
230 ospf_if_reset_variables(oi
);
232 /* Set pseudo neighbor to Null */
235 oi
->ls_upd_queue
= route_table_init();
236 oi
->t_ls_upd_event
= NULL
;
237 oi
->t_ls_ack_direct
= NULL
;
239 oi
->crypt_seqnum
= time(NULL
);
241 ospf_opaque_type9_lsa_init(oi
);
244 QOBJ_REG(oi
, ospf_interface
);
246 if (IS_DEBUG_OSPF_EVENT
)
247 zlog_debug("%s: ospf interface %s vrf %s id %u created",
248 __PRETTY_FUNCTION__
, ifp
->name
,
249 ospf_vrf_id_to_name(ospf
->vrf_id
), ospf
->vrf_id
);
254 /* Restore an interface to its pre UP state
255 Used from ism_interface_down only */
256 void ospf_if_cleanup(struct ospf_interface
*oi
)
258 struct route_node
*rn
;
259 struct listnode
*node
, *nnode
;
260 struct ospf_neighbor
*nbr
;
261 struct ospf_nbr_nbma
*nbr_nbma
;
262 struct ospf_lsa
*lsa
;
264 /* oi->nbrs and oi->nbr_nbma should be deleted on InterfaceDown event */
265 /* delete all static neighbors attached to this interface */
266 for (ALL_LIST_ELEMENTS(oi
->nbr_nbma
, node
, nnode
, nbr_nbma
)) {
267 OSPF_POLL_TIMER_OFF(nbr_nbma
->t_poll
);
270 nbr_nbma
->nbr
->nbr_nbma
= NULL
;
271 nbr_nbma
->nbr
= NULL
;
276 listnode_delete(oi
->nbr_nbma
, nbr_nbma
);
279 /* send Neighbor event KillNbr to all associated neighbors. */
280 for (rn
= route_top(oi
->nbrs
); rn
; rn
= route_next(rn
))
281 if ((nbr
= rn
->info
) != NULL
)
282 if (nbr
!= oi
->nbr_self
)
283 OSPF_NSM_EVENT_EXECUTE(nbr
, NSM_KillNbr
);
285 /* Cleanup Link State Acknowlegdment list. */
286 for (ALL_LIST_ELEMENTS(oi
->ls_ack
, node
, nnode
, lsa
))
287 ospf_lsa_unlock(&lsa
); /* oi->ls_ack */
288 list_delete_all_node(oi
->ls_ack
);
290 oi
->crypt_seqnum
= 0;
292 /* Empty link state update queue */
293 ospf_ls_upd_queue_empty(oi
);
295 /* Reset pseudo neighbor. */
296 ospf_nbr_self_reset(oi
, oi
->ospf
->router_id
);
299 void ospf_if_free(struct ospf_interface
*oi
)
303 assert(oi
->state
== ISM_Down
);
305 ospf_opaque_type9_lsa_term(oi
);
309 /* Free Pseudo Neighbour */
310 ospf_nbr_delete(oi
->nbr_self
);
312 route_table_finish(oi
->nbrs
);
313 route_table_finish(oi
->ls_upd_queue
);
315 /* Free any lists that should be freed */
316 list_free(oi
->nbr_nbma
);
318 list_free(oi
->ls_ack
);
319 list_free(oi
->ls_ack_direct
.ls_ack
);
321 if (IS_DEBUG_OSPF_EVENT
)
322 zlog_debug("%s: ospf interface %s vrf %s id %u deleted",
323 __PRETTY_FUNCTION__
, oi
->ifp
->name
,
324 ospf_vrf_id_to_name(oi
->ifp
->vrf_id
),
327 ospf_delete_from_if(oi
->ifp
, oi
);
329 listnode_delete(oi
->ospf
->oiflist
, oi
);
330 listnode_delete(oi
->area
->oiflist
, oi
);
332 thread_cancel_event(master
, oi
);
334 memset(oi
, 0, sizeof(*oi
));
335 XFREE(MTYPE_OSPF_IF
, oi
);
338 int ospf_if_is_up(struct ospf_interface
*oi
)
340 return if_is_up(oi
->ifp
);
343 struct ospf_interface
*ospf_if_exists(struct ospf_interface
*oic
)
345 struct listnode
*node
;
347 struct ospf_interface
*oi
;
356 for (ALL_LIST_ELEMENTS_RO(ospf
->oiflist
, node
, oi
))
363 /* Lookup OSPF interface by router LSA posistion */
364 struct ospf_interface
*ospf_if_lookup_by_lsa_pos(struct ospf_area
*area
,
367 struct listnode
*node
;
368 struct ospf_interface
*oi
;
370 for (ALL_LIST_ELEMENTS_RO(area
->oiflist
, node
, oi
)) {
371 if (lsa_pos
>= oi
->lsa_pos_beg
&& lsa_pos
< oi
->lsa_pos_end
)
377 struct ospf_interface
*ospf_if_lookup_by_local_addr(struct ospf
*ospf
,
378 struct interface
*ifp
,
379 struct in_addr address
)
381 struct listnode
*node
;
382 struct ospf_interface
*oi
;
384 for (ALL_LIST_ELEMENTS_RO(ospf
->oiflist
, node
, oi
))
385 if (oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
) {
386 if (ifp
&& oi
->ifp
!= ifp
)
389 if (IPV4_ADDR_SAME(&address
, &oi
->address
->u
.prefix4
))
396 struct ospf_interface
*ospf_if_lookup_by_prefix(struct ospf
*ospf
,
397 struct prefix_ipv4
*p
)
399 struct listnode
*node
;
400 struct ospf_interface
*oi
;
402 /* Check each Interface. */
403 for (ALL_LIST_ELEMENTS_RO(ospf
->oiflist
, node
, oi
)) {
404 if (oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
) {
407 prefix_copy(&ptmp
, CONNECTED_PREFIX(oi
->connected
));
409 if (prefix_same(&ptmp
, (struct prefix
*)p
))
416 /* determine receiving interface by ifp and source address */
417 struct ospf_interface
*ospf_if_lookup_recv_if(struct ospf
*ospf
,
419 struct interface
*ifp
)
421 struct route_node
*rn
;
422 struct prefix_ipv4 addr
;
423 struct ospf_interface
*oi
, *match
;
425 addr
.family
= AF_INET
;
427 addr
.prefixlen
= IPV4_MAX_BITLEN
;
431 for (rn
= route_top(IF_OIFS(ifp
)); rn
; rn
= route_next(rn
)) {
434 if (!oi
) /* oi can be NULL for PtP aliases */
437 if (oi
->type
== OSPF_IFTYPE_VIRTUALLINK
)
440 if (if_is_loopback(oi
->ifp
))
443 if (CHECK_FLAG(oi
->connected
->flags
, ZEBRA_IFA_UNNUMBERED
))
445 else if (prefix_match(CONNECTED_PREFIX(oi
->connected
),
446 (struct prefix
*)&addr
)) {
447 if ((match
== NULL
) || (match
->address
->prefixlen
448 < oi
->address
->prefixlen
))
456 void ospf_if_stream_set(struct ospf_interface
*oi
)
458 /* set output fifo queue. */
459 if (oi
->obuf
== NULL
)
460 oi
->obuf
= ospf_fifo_new();
463 void ospf_if_stream_unset(struct ospf_interface
*oi
)
465 struct ospf
*ospf
= oi
->ospf
;
468 ospf_fifo_free(oi
->obuf
);
471 if (oi
->on_write_q
) {
472 listnode_delete(ospf
->oi_write_q
, oi
);
473 if (list_isempty(ospf
->oi_write_q
))
474 OSPF_TIMER_OFF(ospf
->t_write
);
481 static struct ospf_if_params
*ospf_new_if_params(void)
483 struct ospf_if_params
*oip
;
485 oip
= XCALLOC(MTYPE_OSPF_IF_PARAMS
, sizeof(struct ospf_if_params
));
490 UNSET_IF_PARAM(oip
, output_cost_cmd
);
491 UNSET_IF_PARAM(oip
, transmit_delay
);
492 UNSET_IF_PARAM(oip
, retransmit_interval
);
493 UNSET_IF_PARAM(oip
, passive_interface
);
494 UNSET_IF_PARAM(oip
, v_hello
);
495 UNSET_IF_PARAM(oip
, fast_hello
);
496 UNSET_IF_PARAM(oip
, v_wait
);
497 UNSET_IF_PARAM(oip
, priority
);
498 UNSET_IF_PARAM(oip
, type
);
499 UNSET_IF_PARAM(oip
, auth_simple
);
500 UNSET_IF_PARAM(oip
, auth_crypt
);
501 UNSET_IF_PARAM(oip
, auth_type
);
503 oip
->auth_crypt
= list_new();
505 oip
->network_lsa_seqnum
= htonl(OSPF_INITIAL_SEQUENCE_NUMBER
);
510 void ospf_del_if_params(struct ospf_if_params
*oip
)
512 list_delete_and_null(&oip
->auth_crypt
);
513 bfd_info_free(&(oip
->bfd_info
));
514 XFREE(MTYPE_OSPF_IF_PARAMS
, oip
);
517 void ospf_free_if_params(struct interface
*ifp
, struct in_addr addr
)
519 struct ospf_if_params
*oip
;
520 struct prefix_ipv4 p
;
521 struct route_node
*rn
;
524 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
526 rn
= route_node_lookup(IF_OIFS_PARAMS(ifp
), (struct prefix
*)&p
);
527 if (!rn
|| !rn
->info
)
531 route_unlock_node(rn
);
533 if (!OSPF_IF_PARAM_CONFIGURED(oip
, output_cost_cmd
)
534 && !OSPF_IF_PARAM_CONFIGURED(oip
, transmit_delay
)
535 && !OSPF_IF_PARAM_CONFIGURED(oip
, retransmit_interval
)
536 && !OSPF_IF_PARAM_CONFIGURED(oip
, passive_interface
)
537 && !OSPF_IF_PARAM_CONFIGURED(oip
, v_hello
)
538 && !OSPF_IF_PARAM_CONFIGURED(oip
, fast_hello
)
539 && !OSPF_IF_PARAM_CONFIGURED(oip
, v_wait
)
540 && !OSPF_IF_PARAM_CONFIGURED(oip
, priority
)
541 && !OSPF_IF_PARAM_CONFIGURED(oip
, type
)
542 && !OSPF_IF_PARAM_CONFIGURED(oip
, auth_simple
)
543 && !OSPF_IF_PARAM_CONFIGURED(oip
, auth_type
)
544 && listcount(oip
->auth_crypt
) == 0
545 && ntohl(oip
->network_lsa_seqnum
) != OSPF_INITIAL_SEQUENCE_NUMBER
) {
546 ospf_del_if_params(oip
);
548 route_unlock_node(rn
);
552 struct ospf_if_params
*ospf_lookup_if_params(struct interface
*ifp
,
555 struct prefix_ipv4 p
;
556 struct route_node
*rn
;
559 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
562 rn
= route_node_lookup(IF_OIFS_PARAMS(ifp
), (struct prefix
*)&p
);
565 route_unlock_node(rn
);
572 struct ospf_if_params
*ospf_get_if_params(struct interface
*ifp
,
575 struct prefix_ipv4 p
;
576 struct route_node
*rn
;
579 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
583 rn
= route_node_get(IF_OIFS_PARAMS(ifp
), (struct prefix
*)&p
);
585 if (rn
->info
== NULL
)
586 rn
->info
= ospf_new_if_params();
588 route_unlock_node(rn
);
593 void ospf_if_update_params(struct interface
*ifp
, struct in_addr addr
)
595 struct route_node
*rn
;
596 struct ospf_interface
*oi
;
598 for (rn
= route_top(IF_OIFS(ifp
)); rn
; rn
= route_next(rn
)) {
599 if ((oi
= rn
->info
) == NULL
)
602 if (IPV4_ADDR_SAME(&oi
->address
->u
.prefix4
, &addr
))
603 oi
->params
= ospf_lookup_if_params(
604 ifp
, oi
->address
->u
.prefix4
);
608 int ospf_if_new_hook(struct interface
*ifp
)
612 ifp
->info
= XCALLOC(MTYPE_OSPF_IF_INFO
, sizeof(struct ospf_if_info
));
614 IF_OIFS(ifp
) = route_table_init();
615 IF_OIFS_PARAMS(ifp
) = route_table_init();
617 IF_DEF_PARAMS(ifp
) = ospf_new_if_params();
619 SET_IF_PARAM(IF_DEF_PARAMS(ifp
), transmit_delay
);
620 IF_DEF_PARAMS(ifp
)->transmit_delay
= OSPF_TRANSMIT_DELAY_DEFAULT
;
622 SET_IF_PARAM(IF_DEF_PARAMS(ifp
), retransmit_interval
);
623 IF_DEF_PARAMS(ifp
)->retransmit_interval
=
624 OSPF_RETRANSMIT_INTERVAL_DEFAULT
;
626 SET_IF_PARAM(IF_DEF_PARAMS(ifp
), priority
);
627 IF_DEF_PARAMS(ifp
)->priority
= OSPF_ROUTER_PRIORITY_DEFAULT
;
629 IF_DEF_PARAMS(ifp
)->mtu_ignore
= OSPF_MTU_IGNORE_DEFAULT
;
631 SET_IF_PARAM(IF_DEF_PARAMS(ifp
), v_hello
);
632 IF_DEF_PARAMS(ifp
)->v_hello
= OSPF_HELLO_INTERVAL_DEFAULT
;
634 SET_IF_PARAM(IF_DEF_PARAMS(ifp
), fast_hello
);
635 IF_DEF_PARAMS(ifp
)->fast_hello
= OSPF_FAST_HELLO_DEFAULT
;
637 SET_IF_PARAM(IF_DEF_PARAMS(ifp
), v_wait
);
638 IF_DEF_PARAMS(ifp
)->v_wait
= OSPF_ROUTER_DEAD_INTERVAL_DEFAULT
;
640 SET_IF_PARAM(IF_DEF_PARAMS(ifp
), auth_simple
);
641 memset(IF_DEF_PARAMS(ifp
)->auth_simple
, 0, OSPF_AUTH_SIMPLE_SIZE
);
643 SET_IF_PARAM(IF_DEF_PARAMS(ifp
), auth_type
);
644 IF_DEF_PARAMS(ifp
)->auth_type
= OSPF_AUTH_NOTSET
;
646 rc
= ospf_opaque_new_if(ifp
);
650 static int ospf_if_delete_hook(struct interface
*ifp
)
653 struct route_node
*rn
;
654 rc
= ospf_opaque_del_if(ifp
);
656 route_table_finish(IF_OIFS(ifp
));
658 for (rn
= route_top(IF_OIFS_PARAMS(ifp
)); rn
; rn
= route_next(rn
))
660 ospf_del_if_params(rn
->info
);
661 route_table_finish(IF_OIFS_PARAMS(ifp
));
663 ospf_del_if_params((struct ospf_if_params
*)IF_DEF_PARAMS(ifp
));
664 XFREE(MTYPE_OSPF_IF_INFO
, ifp
->info
);
670 int ospf_if_is_enable(struct ospf_interface
*oi
)
672 if (!if_is_loopback(oi
->ifp
))
673 if (if_is_up(oi
->ifp
))
679 void ospf_if_set_multicast(struct ospf_interface
*oi
)
681 if ((oi
->state
> ISM_Loopback
) && (oi
->type
!= OSPF_IFTYPE_LOOPBACK
)
682 && (oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
)
683 && (OSPF_IF_PASSIVE_STATUS(oi
) == OSPF_IF_ACTIVE
)) {
684 /* The interface should belong to the OSPF-all-routers group. */
685 if (!OI_MEMBER_CHECK(oi
, MEMBER_ALLROUTERS
)
686 && (ospf_if_add_allspfrouters(oi
->ospf
, oi
->address
,
689 /* Set the flag only if the system call to join
691 OI_MEMBER_JOINED(oi
, MEMBER_ALLROUTERS
);
693 /* The interface should NOT belong to the OSPF-all-routers
695 if (OI_MEMBER_CHECK(oi
, MEMBER_ALLROUTERS
)) {
696 /* Only actually drop if this is the last reference */
697 if (OI_MEMBER_COUNT(oi
, MEMBER_ALLROUTERS
) == 1)
698 ospf_if_drop_allspfrouters(oi
->ospf
,
701 /* Unset the flag regardless of whether the system call
703 the group succeeded, since it's much safer to assume
705 we are not a member. */
706 OI_MEMBER_LEFT(oi
, MEMBER_ALLROUTERS
);
710 if (((oi
->type
== OSPF_IFTYPE_BROADCAST
)
711 || (oi
->type
== OSPF_IFTYPE_POINTOPOINT
))
712 && ((oi
->state
== ISM_DR
) || (oi
->state
== ISM_Backup
))
713 && (OSPF_IF_PASSIVE_STATUS(oi
) == OSPF_IF_ACTIVE
)) {
714 /* The interface should belong to the OSPF-designated-routers
716 if (!OI_MEMBER_CHECK(oi
, MEMBER_DROUTERS
)
717 && (ospf_if_add_alldrouters(oi
->ospf
, oi
->address
,
720 /* Set the flag only if the system call to join
722 OI_MEMBER_JOINED(oi
, MEMBER_DROUTERS
);
724 /* The interface should NOT belong to the
725 * OSPF-designated-routers group */
726 if (OI_MEMBER_CHECK(oi
, MEMBER_DROUTERS
)) {
727 /* drop only if last reference */
728 if (OI_MEMBER_COUNT(oi
, MEMBER_DROUTERS
) == 1)
729 ospf_if_drop_alldrouters(oi
->ospf
, oi
->address
,
732 /* Unset the flag regardless of whether the system call
734 the group succeeded, since it's much safer to assume
736 we are not a member. */
737 OI_MEMBER_LEFT(oi
, MEMBER_DROUTERS
);
742 int ospf_if_up(struct ospf_interface
*oi
)
747 if (oi
->type
== OSPF_IFTYPE_LOOPBACK
)
748 OSPF_ISM_EVENT_SCHEDULE(oi
, ISM_LoopInd
);
750 ospf_if_stream_set(oi
);
751 OSPF_ISM_EVENT_SCHEDULE(oi
, ISM_InterfaceUp
);
757 int ospf_if_down(struct ospf_interface
*oi
)
762 OSPF_ISM_EVENT_EXECUTE(oi
, ISM_InterfaceDown
);
763 /* delete position in router LSA */
766 /* Shutdown packet reception and sending */
767 ospf_if_stream_unset(oi
);
773 /* Virtual Link related functions. */
775 struct ospf_vl_data
*ospf_vl_data_new(struct ospf_area
*area
,
776 struct in_addr vl_peer
)
778 struct ospf_vl_data
*vl_data
;
780 vl_data
= XCALLOC(MTYPE_OSPF_VL_DATA
, sizeof(struct ospf_vl_data
));
782 vl_data
->vl_peer
.s_addr
= vl_peer
.s_addr
;
783 vl_data
->vl_area_id
= area
->area_id
;
784 vl_data
->vl_area_id_fmt
= area
->area_id_fmt
;
789 void ospf_vl_data_free(struct ospf_vl_data
*vl_data
)
791 XFREE(MTYPE_OSPF_VL_DATA
, vl_data
);
794 u_int vlink_count
= 0;
796 struct ospf_interface
*ospf_vl_new(struct ospf
*ospf
,
797 struct ospf_vl_data
*vl_data
)
799 struct ospf_interface
*voi
;
800 struct interface
*vi
;
801 char ifname
[INTERFACE_NAMSIZ
+ 1];
802 struct ospf_area
*area
;
803 struct in_addr area_id
;
804 struct connected
*co
;
805 struct prefix_ipv4
*p
;
807 if (IS_DEBUG_OSPF_EVENT
)
808 zlog_debug("ospf_vl_new(): Start");
809 if (vlink_count
== OSPF_VL_MAX_COUNT
) {
810 if (IS_DEBUG_OSPF_EVENT
)
812 "ospf_vl_new(): Alarm: "
813 "cannot create more than OSPF_MAX_VL_COUNT virtual links");
817 if (IS_DEBUG_OSPF_EVENT
)
818 zlog_debug("ospf_vl_new(): creating pseudo zebra interface vrf id %u",
821 snprintf(ifname
, sizeof(ifname
), "VLINK%d", vlink_count
);
822 vi
= if_create(ifname
, strnlen(ifname
, sizeof(ifname
)), ospf
->vrf_id
);
824 * if_create sets ZEBRA_INTERFACE_LINKDETECTION
825 * virtual links don't need this.
827 UNSET_FLAG(vi
->status
, ZEBRA_INTERFACE_LINKDETECTION
);
828 co
= connected_new();
830 listnode_add(vi
->connected
, co
);
832 p
= prefix_ipv4_new();
834 p
->prefix
.s_addr
= 0;
837 co
->address
= (struct prefix
*)p
;
839 voi
= ospf_if_new(ospf
, vi
, co
->address
);
841 if (IS_DEBUG_OSPF_EVENT
)
843 "ospf_vl_new(): Alarm: OSPF int structure is not created");
847 voi
->vl_data
= vl_data
;
848 voi
->ifp
->mtu
= OSPF_VL_MTU
;
849 voi
->type
= OSPF_IFTYPE_VIRTUALLINK
;
852 if (IS_DEBUG_OSPF_EVENT
)
853 zlog_debug("ospf_vl_new(): Created name: %s", ifname
);
854 if (IS_DEBUG_OSPF_EVENT
)
855 zlog_debug("ospf_vl_new(): set if->name to %s", vi
->name
);
858 area
= ospf_area_get(ospf
, area_id
);
861 if (IS_DEBUG_OSPF_EVENT
)
863 "ospf_vl_new(): set associated area to the backbone");
865 /* Add pseudo neighbor. */
866 ospf_nbr_self_reset(voi
, voi
->ospf
->router_id
);
868 ospf_area_add_if(voi
->area
, voi
);
870 ospf_if_stream_set(voi
);
872 if (IS_DEBUG_OSPF_EVENT
)
873 zlog_debug("ospf_vl_new(): Stop");
877 static void ospf_vl_if_delete(struct ospf_vl_data
*vl_data
)
879 struct interface
*ifp
= vl_data
->vl_oi
->ifp
;
880 vl_data
->vl_oi
->address
->u
.prefix4
.s_addr
= 0;
881 vl_data
->vl_oi
->address
->prefixlen
= 0;
882 ospf_if_free(vl_data
->vl_oi
);
887 /* Look up vl_data for given peer, optionally qualified to be in the
888 * specified area. NULL area returns first found..
890 struct ospf_vl_data
*ospf_vl_lookup(struct ospf
*ospf
, struct ospf_area
*area
,
891 struct in_addr vl_peer
)
893 struct ospf_vl_data
*vl_data
;
894 struct listnode
*node
;
896 if (IS_DEBUG_OSPF_EVENT
) {
897 zlog_debug("%s: Looking for %s", __func__
, inet_ntoa(vl_peer
));
899 zlog_debug("%s: in area %s", __func__
,
900 inet_ntoa(area
->area_id
));
903 for (ALL_LIST_ELEMENTS_RO(ospf
->vlinks
, node
, vl_data
)) {
904 if (IS_DEBUG_OSPF_EVENT
)
905 zlog_debug("%s: VL %s, peer %s", __func__
,
906 vl_data
->vl_oi
->ifp
->name
,
907 inet_ntoa(vl_data
->vl_peer
));
910 && !IPV4_ADDR_SAME(&vl_data
->vl_area_id
, &area
->area_id
))
913 if (IPV4_ADDR_SAME(&vl_data
->vl_peer
, &vl_peer
))
920 static void ospf_vl_shutdown(struct ospf_vl_data
*vl_data
)
922 struct ospf_interface
*oi
;
924 if ((oi
= vl_data
->vl_oi
) == NULL
)
927 oi
->address
->u
.prefix4
.s_addr
= 0;
928 oi
->address
->prefixlen
= 0;
930 UNSET_FLAG(oi
->ifp
->flags
, IFF_UP
);
931 /* OSPF_ISM_EVENT_SCHEDULE (oi, ISM_InterfaceDown); */
932 OSPF_ISM_EVENT_EXECUTE(oi
, ISM_InterfaceDown
);
935 void ospf_vl_add(struct ospf
*ospf
, struct ospf_vl_data
*vl_data
)
937 listnode_add(ospf
->vlinks
, vl_data
);
938 hook_call(ospf_vl_add
, vl_data
);
941 void ospf_vl_delete(struct ospf
*ospf
, struct ospf_vl_data
*vl_data
)
943 ospf_vl_shutdown(vl_data
);
944 ospf_vl_if_delete(vl_data
);
946 hook_call(ospf_vl_delete
, vl_data
);
947 listnode_delete(ospf
->vlinks
, vl_data
);
949 ospf_vl_data_free(vl_data
);
952 static int ospf_vl_set_params(struct ospf_vl_data
*vl_data
, struct vertex
*v
)
955 struct ospf_interface
*voi
;
956 struct listnode
*node
;
957 struct vertex_parent
*vp
= NULL
;
959 struct router_lsa
*rl
;
961 voi
= vl_data
->vl_oi
;
963 if (voi
->output_cost
!= v
->distance
) {
965 voi
->output_cost
= v
->distance
;
969 for (ALL_LIST_ELEMENTS_RO(v
->parents
, node
, vp
)) {
970 vl_data
->nexthop
.oi
= vp
->nexthop
->oi
;
971 vl_data
->nexthop
.router
= vp
->nexthop
->router
;
973 if (!IPV4_ADDR_SAME(&voi
->address
->u
.prefix4
,
974 &vl_data
->nexthop
.oi
->address
->u
.prefix4
))
977 voi
->address
->u
.prefix4
=
978 vl_data
->nexthop
.oi
->address
->u
.prefix4
;
979 voi
->address
->prefixlen
=
980 vl_data
->nexthop
.oi
->address
->prefixlen
;
982 break; /* We take the first interface. */
985 rl
= (struct router_lsa
*)v
->lsa
;
987 /* use SPF determined backlink index in struct vertex
988 * for virtual link destination address
990 if (vp
&& vp
->backlink
>= 0) {
991 if (!IPV4_ADDR_SAME(&vl_data
->peer_addr
,
992 &rl
->link
[vp
->backlink
].link_data
))
994 vl_data
->peer_addr
= rl
->link
[vp
->backlink
].link_data
;
996 /* This is highly odd, there is no backlink index
997 * there should be due to the ospf_spf_has_link() check
998 * in SPF. Lets warn and try pick a link anyway.
1000 zlog_warn("ospf_vl_set_params: No backlink for %s!",
1001 vl_data
->vl_oi
->ifp
->name
);
1002 for (i
= 0; i
< ntohs(rl
->links
); i
++) {
1003 switch (rl
->link
[i
].type
) {
1004 case LSA_LINK_TYPE_VIRTUALLINK
:
1005 if (IS_DEBUG_OSPF_EVENT
)
1007 "found back link through VL");
1009 case LSA_LINK_TYPE_TRANSIT
:
1010 case LSA_LINK_TYPE_POINTOPOINT
:
1011 if (!IPV4_ADDR_SAME(&vl_data
->peer_addr
,
1012 &rl
->link
[i
].link_data
))
1014 vl_data
->peer_addr
= rl
->link
[i
].link_data
;
1019 if (IS_DEBUG_OSPF_EVENT
)
1020 zlog_debug("%s: %s peer address: %s, cost: %d,%schanged",
1021 __func__
, vl_data
->vl_oi
->ifp
->name
,
1022 inet_ntoa(vl_data
->peer_addr
), voi
->output_cost
,
1023 (changed
? " " : " un"));
1029 void ospf_vl_up_check(struct ospf_area
*area
, struct in_addr rid
,
1032 struct ospf
*ospf
= area
->ospf
;
1033 struct listnode
*node
;
1034 struct ospf_vl_data
*vl_data
;
1035 struct ospf_interface
*oi
;
1037 if (IS_DEBUG_OSPF_EVENT
) {
1038 zlog_debug("ospf_vl_up_check(): Start");
1039 zlog_debug("ospf_vl_up_check(): Router ID is %s",
1041 zlog_debug("ospf_vl_up_check(): Area is %s",
1042 inet_ntoa(area
->area_id
));
1045 for (ALL_LIST_ELEMENTS_RO(ospf
->vlinks
, node
, vl_data
)) {
1046 if (IS_DEBUG_OSPF_EVENT
) {
1047 zlog_debug("%s: considering VL, %s in area %s",
1048 __func__
, vl_data
->vl_oi
->ifp
->name
,
1049 inet_ntoa(vl_data
->vl_area_id
));
1050 zlog_debug("%s: peer ID: %s", __func__
,
1051 inet_ntoa(vl_data
->vl_peer
));
1054 if (IPV4_ADDR_SAME(&vl_data
->vl_peer
, &rid
)
1055 && IPV4_ADDR_SAME(&vl_data
->vl_area_id
, &area
->area_id
)) {
1056 oi
= vl_data
->vl_oi
;
1057 SET_FLAG(vl_data
->flags
, OSPF_VL_FLAG_APPROVED
);
1059 if (IS_DEBUG_OSPF_EVENT
)
1061 "ospf_vl_up_check(): this VL matched");
1063 if (oi
->state
== ISM_Down
) {
1064 if (IS_DEBUG_OSPF_EVENT
)
1066 "ospf_vl_up_check(): VL is down, waking it up");
1067 SET_FLAG(oi
->ifp
->flags
, IFF_UP
);
1068 OSPF_ISM_EVENT_EXECUTE(oi
, ISM_InterfaceUp
);
1071 if (ospf_vl_set_params(vl_data
, v
)) {
1072 if (IS_DEBUG_OSPF(ism
, ISM_EVENTS
))
1074 "ospf_vl_up_check: VL cost change,"
1075 " scheduling router lsa refresh");
1077 ospf_router_lsa_update_area(
1079 else if (IS_DEBUG_OSPF(ism
, ISM_EVENTS
))
1081 "ospf_vl_up_check: VL cost change, no backbone!");
1087 void ospf_vl_unapprove(struct ospf
*ospf
)
1089 struct listnode
*node
;
1090 struct ospf_vl_data
*vl_data
;
1092 for (ALL_LIST_ELEMENTS_RO(ospf
->vlinks
, node
, vl_data
))
1093 UNSET_FLAG(vl_data
->flags
, OSPF_VL_FLAG_APPROVED
);
1096 void ospf_vl_shut_unapproved(struct ospf
*ospf
)
1098 struct listnode
*node
, *nnode
;
1099 struct ospf_vl_data
*vl_data
;
1101 for (ALL_LIST_ELEMENTS(ospf
->vlinks
, node
, nnode
, vl_data
))
1102 if (!CHECK_FLAG(vl_data
->flags
, OSPF_VL_FLAG_APPROVED
))
1103 ospf_vl_shutdown(vl_data
);
1106 int ospf_full_virtual_nbrs(struct ospf_area
*area
)
1108 if (IS_DEBUG_OSPF_EVENT
) {
1110 "counting fully adjacent virtual neighbors in area %s",
1111 inet_ntoa(area
->area_id
));
1112 zlog_debug("there are %d of them", area
->full_vls
);
1115 return area
->full_vls
;
1118 int ospf_vls_in_area(struct ospf_area
*area
)
1120 struct listnode
*node
;
1121 struct ospf_vl_data
*vl_data
;
1124 for (ALL_LIST_ELEMENTS_RO(area
->ospf
->vlinks
, node
, vl_data
))
1125 if (IPV4_ADDR_SAME(&vl_data
->vl_area_id
, &area
->area_id
))
1132 struct crypt_key
*ospf_crypt_key_new()
1134 return XCALLOC(MTYPE_OSPF_CRYPT_KEY
, sizeof(struct crypt_key
));
1137 void ospf_crypt_key_add(struct list
*crypt
, struct crypt_key
*ck
)
1139 listnode_add(crypt
, ck
);
1142 struct crypt_key
*ospf_crypt_key_lookup(struct list
*auth_crypt
, u_char key_id
)
1144 struct listnode
*node
;
1145 struct crypt_key
*ck
;
1147 for (ALL_LIST_ELEMENTS_RO(auth_crypt
, node
, ck
))
1148 if (ck
->key_id
== key_id
)
1154 int ospf_crypt_key_delete(struct list
*auth_crypt
, u_char key_id
)
1156 struct listnode
*node
, *nnode
;
1157 struct crypt_key
*ck
;
1159 for (ALL_LIST_ELEMENTS(auth_crypt
, node
, nnode
, ck
)) {
1160 if (ck
->key_id
== key_id
) {
1161 listnode_delete(auth_crypt
, ck
);
1162 XFREE(MTYPE_OSPF_CRYPT_KEY
, ck
);
1170 u_char
ospf_default_iftype(struct interface
*ifp
)
1172 if (if_is_pointopoint(ifp
))
1173 return OSPF_IFTYPE_POINTOPOINT
;
1174 else if (if_is_loopback(ifp
))
1175 return OSPF_IFTYPE_LOOPBACK
;
1177 return OSPF_IFTYPE_BROADCAST
;
1182 /* Initialize Zebra interface data structure. */
1183 hook_register_prio(if_add
, 0, ospf_if_new_hook
);
1184 hook_register_prio(if_del
, 0, ospf_if_delete_hook
);