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_interface_neighbor_count(struct ospf_interface
*oi
)
57 struct route_node
*rn
;
58 struct ospf_neighbor
*nbr
= NULL
;
60 for (rn
= route_top(oi
->nbrs
); rn
; rn
= route_next(rn
)) {
63 /* Do not show myself. */
64 if (nbr
== oi
->nbr_self
)
66 /* Down state is not shown. */
67 if (nbr
->state
== NSM_Down
)
76 int ospf_if_get_output_cost(struct ospf_interface
*oi
)
78 /* If all else fails, use default OSPF cost */
82 /* ifp speed and bw can be 0 in some platforms, use ospf default bw
83 if bw is configured under interface it would be used.
85 if (!oi
->ifp
->bandwidth
&& oi
->ifp
->speed
)
88 bw
= oi
->ifp
->bandwidth
? oi
->ifp
->bandwidth
89 : OSPF_DEFAULT_BANDWIDTH
;
90 refbw
= oi
->ospf
->ref_bandwidth
;
92 /* A specifed ip ospf cost overrides a calculated one. */
93 if (OSPF_IF_PARAM_CONFIGURED(IF_DEF_PARAMS(oi
->ifp
), output_cost_cmd
)
94 || OSPF_IF_PARAM_CONFIGURED(oi
->params
, output_cost_cmd
))
95 cost
= OSPF_IF_PARAM(oi
, output_cost_cmd
);
96 /* See if a cost can be calculated from the zebra processes
97 interface bandwidth field. */
99 cost
= (u_int32_t
)((double)refbw
/ (double)bw
+ (double)0.5);
102 else if (cost
> 65535)
109 void ospf_if_recalculate_output_cost(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
)
120 newcost
= ospf_if_get_output_cost(oi
);
122 /* Is actual output cost changed? */
123 if (oi
->output_cost
!= newcost
) {
124 oi
->output_cost
= newcost
;
125 ospf_router_lsa_update_area(oi
->area
);
130 /* Simulate down/up on the interface. This is needed, for example, when
132 void ospf_if_reset(struct interface
*ifp
)
134 struct route_node
*rn
;
136 for (rn
= route_top(IF_OIFS(ifp
)); rn
; rn
= route_next(rn
)) {
137 struct ospf_interface
*oi
;
139 if ((oi
= rn
->info
) == NULL
)
147 void ospf_if_reset_variables(struct ospf_interface
*oi
)
149 /* Set default values. */
150 /* don't clear this flag. oi->flag = OSPF_IF_DISABLE; */
153 oi
->type
= OSPF_IFTYPE_VIRTUALLINK
;
155 /* preserve network-type */
156 if (oi
->type
!= OSPF_IFTYPE_NBMA
)
157 oi
->type
= OSPF_IFTYPE_BROADCAST
;
159 oi
->state
= ISM_Down
;
161 oi
->crypt_seqnum
= 0;
163 /* This must be short, (less than RxmtInterval)
164 - RFC 2328 Section 13.5 para 3. Set to 1 second to avoid Acks being
165 held back for too long - MAG */
169 /* lookup oi for specified prefix/ifp */
170 struct ospf_interface
*ospf_if_table_lookup(struct interface
*ifp
,
171 struct prefix
*prefix
)
174 struct route_node
*rn
;
175 struct ospf_interface
*rninfo
= NULL
;
178 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
180 /* route_node_get implicitely locks */
181 if ((rn
= route_node_lookup(IF_OIFS(ifp
), &p
))) {
182 rninfo
= (struct ospf_interface
*)rn
->info
;
183 route_unlock_node(rn
);
189 static void ospf_add_to_if(struct interface
*ifp
, struct ospf_interface
*oi
)
191 struct route_node
*rn
;
195 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
198 rn
= route_node_get(IF_OIFS(ifp
), &p
);
199 /* rn->info should either be NULL or equal to this oi
200 * as route_node_get may return an existing node
202 assert(!rn
->info
|| rn
->info
== oi
);
206 static void ospf_delete_from_if(struct interface
*ifp
,
207 struct ospf_interface
*oi
)
209 struct route_node
*rn
;
213 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
215 rn
= route_node_lookup(IF_OIFS(oi
->ifp
), &p
);
219 route_unlock_node(rn
);
220 route_unlock_node(rn
);
223 struct ospf_interface
*ospf_if_new(struct ospf
*ospf
, struct interface
*ifp
,
226 struct ospf_interface
*oi
;
228 if ((oi
= ospf_if_table_lookup(ifp
, p
)) == NULL
) {
229 oi
= XCALLOC(MTYPE_OSPF_IF
, sizeof(struct ospf_interface
));
230 memset(oi
, 0, sizeof(struct ospf_interface
));
234 /* Set zebra interface pointer. */
238 ospf_add_to_if(ifp
, oi
);
239 listnode_add(ospf
->oiflist
, oi
);
241 /* Initialize neighbor list. */
242 oi
->nbrs
= route_table_init();
244 /* Initialize static neighbor list. */
245 oi
->nbr_nbma
= list_new();
247 /* Initialize Link State Acknowledgment list. */
248 oi
->ls_ack
= list_new();
249 oi
->ls_ack_direct
.ls_ack
= list_new();
251 /* Set default values. */
252 ospf_if_reset_variables(oi
);
254 /* Set pseudo neighbor to Null */
257 oi
->ls_upd_queue
= route_table_init();
258 oi
->t_ls_upd_event
= NULL
;
259 oi
->t_ls_ack_direct
= NULL
;
261 oi
->crypt_seqnum
= time(NULL
);
263 ospf_opaque_type9_lsa_init(oi
);
266 QOBJ_REG(oi
, ospf_interface
);
268 if (IS_DEBUG_OSPF_EVENT
)
269 zlog_debug("%s: ospf interface %s vrf %s id %u created",
270 __PRETTY_FUNCTION__
, ifp
->name
,
271 ospf_vrf_id_to_name(ospf
->vrf_id
), ospf
->vrf_id
);
276 /* Restore an interface to its pre UP state
277 Used from ism_interface_down only */
278 void ospf_if_cleanup(struct ospf_interface
*oi
)
280 struct route_node
*rn
;
281 struct listnode
*node
, *nnode
;
282 struct ospf_neighbor
*nbr
;
283 struct ospf_nbr_nbma
*nbr_nbma
;
284 struct ospf_lsa
*lsa
;
286 /* oi->nbrs and oi->nbr_nbma should be deleted on InterfaceDown event */
287 /* delete all static neighbors attached to this interface */
288 for (ALL_LIST_ELEMENTS(oi
->nbr_nbma
, node
, nnode
, nbr_nbma
)) {
289 OSPF_POLL_TIMER_OFF(nbr_nbma
->t_poll
);
292 nbr_nbma
->nbr
->nbr_nbma
= NULL
;
293 nbr_nbma
->nbr
= NULL
;
298 listnode_delete(oi
->nbr_nbma
, nbr_nbma
);
301 /* send Neighbor event KillNbr to all associated neighbors. */
302 for (rn
= route_top(oi
->nbrs
); rn
; rn
= route_next(rn
))
303 if ((nbr
= rn
->info
) != NULL
)
304 if (nbr
!= oi
->nbr_self
)
305 OSPF_NSM_EVENT_EXECUTE(nbr
, NSM_KillNbr
);
307 /* Cleanup Link State Acknowlegdment list. */
308 for (ALL_LIST_ELEMENTS(oi
->ls_ack
, node
, nnode
, lsa
))
309 ospf_lsa_unlock(&lsa
); /* oi->ls_ack */
310 list_delete_all_node(oi
->ls_ack
);
312 oi
->crypt_seqnum
= 0;
314 /* Empty link state update queue */
315 ospf_ls_upd_queue_empty(oi
);
317 /* Reset pseudo neighbor. */
318 ospf_nbr_self_reset(oi
, oi
->ospf
->router_id
);
321 void ospf_if_free(struct ospf_interface
*oi
)
325 assert(oi
->state
== ISM_Down
);
327 ospf_opaque_type9_lsa_term(oi
);
331 /* Free Pseudo Neighbour */
332 ospf_nbr_delete(oi
->nbr_self
);
334 route_table_finish(oi
->nbrs
);
335 route_table_finish(oi
->ls_upd_queue
);
337 /* Free any lists that should be freed */
338 list_delete_and_null(&oi
->nbr_nbma
);
340 list_delete_and_null(&oi
->ls_ack
);
341 list_delete_and_null(&oi
->ls_ack_direct
.ls_ack
);
343 if (IS_DEBUG_OSPF_EVENT
)
344 zlog_debug("%s: ospf interface %s vrf %s id %u deleted",
345 __PRETTY_FUNCTION__
, oi
->ifp
->name
,
346 ospf_vrf_id_to_name(oi
->ifp
->vrf_id
),
349 ospf_delete_from_if(oi
->ifp
, oi
);
351 listnode_delete(oi
->ospf
->oiflist
, oi
);
352 listnode_delete(oi
->area
->oiflist
, oi
);
354 thread_cancel_event(master
, oi
);
356 memset(oi
, 0, sizeof(*oi
));
357 XFREE(MTYPE_OSPF_IF
, oi
);
360 int ospf_if_is_up(struct ospf_interface
*oi
)
362 return if_is_up(oi
->ifp
);
365 struct ospf_interface
*ospf_if_exists(struct ospf_interface
*oic
)
367 struct listnode
*node
;
369 struct ospf_interface
*oi
;
378 for (ALL_LIST_ELEMENTS_RO(ospf
->oiflist
, node
, oi
))
385 /* Lookup OSPF interface by router LSA posistion */
386 struct ospf_interface
*ospf_if_lookup_by_lsa_pos(struct ospf_area
*area
,
389 struct listnode
*node
;
390 struct ospf_interface
*oi
;
392 for (ALL_LIST_ELEMENTS_RO(area
->oiflist
, node
, oi
)) {
393 if (lsa_pos
>= oi
->lsa_pos_beg
&& lsa_pos
< oi
->lsa_pos_end
)
399 struct ospf_interface
*ospf_if_lookup_by_local_addr(struct ospf
*ospf
,
400 struct interface
*ifp
,
401 struct in_addr address
)
403 struct listnode
*node
;
404 struct ospf_interface
*oi
;
406 for (ALL_LIST_ELEMENTS_RO(ospf
->oiflist
, node
, oi
))
407 if (oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
) {
408 if (ifp
&& oi
->ifp
!= ifp
)
411 if (IPV4_ADDR_SAME(&address
, &oi
->address
->u
.prefix4
))
418 struct ospf_interface
*ospf_if_lookup_by_prefix(struct ospf
*ospf
,
419 struct prefix_ipv4
*p
)
421 struct listnode
*node
;
422 struct ospf_interface
*oi
;
424 /* Check each Interface. */
425 for (ALL_LIST_ELEMENTS_RO(ospf
->oiflist
, node
, oi
)) {
426 if (oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
) {
429 prefix_copy(&ptmp
, CONNECTED_PREFIX(oi
->connected
));
431 if (prefix_same(&ptmp
, (struct prefix
*)p
))
438 /* determine receiving interface by ifp and source address */
439 struct ospf_interface
*ospf_if_lookup_recv_if(struct ospf
*ospf
,
441 struct interface
*ifp
)
443 struct route_node
*rn
;
444 struct prefix_ipv4 addr
;
445 struct ospf_interface
*oi
, *match
;
447 addr
.family
= AF_INET
;
449 addr
.prefixlen
= IPV4_MAX_BITLEN
;
453 for (rn
= route_top(IF_OIFS(ifp
)); rn
; rn
= route_next(rn
)) {
456 if (!oi
) /* oi can be NULL for PtP aliases */
459 if (oi
->type
== OSPF_IFTYPE_VIRTUALLINK
)
462 if (if_is_loopback(oi
->ifp
) || if_is_vrf(oi
->ifp
))
465 if (CHECK_FLAG(oi
->connected
->flags
, ZEBRA_IFA_UNNUMBERED
))
467 else if (prefix_match(CONNECTED_PREFIX(oi
->connected
),
468 (struct prefix
*)&addr
)) {
469 if ((match
== NULL
) || (match
->address
->prefixlen
470 < oi
->address
->prefixlen
))
478 static void ospf_if_reset_stats(struct ospf_interface
*oi
)
480 oi
->hello_in
= oi
->hello_out
= 0;
481 oi
->db_desc_in
= oi
->db_desc_out
= 0;
482 oi
->ls_req_in
= oi
->ls_req_out
= 0;
483 oi
->ls_upd_in
= oi
->ls_upd_out
= 0;
484 oi
->ls_ack_in
= oi
->ls_ack_out
= 0;
487 void ospf_if_stream_set(struct ospf_interface
*oi
)
489 /* set output fifo queue. */
490 if (oi
->obuf
== NULL
)
491 oi
->obuf
= ospf_fifo_new();
494 void ospf_if_stream_unset(struct ospf_interface
*oi
)
496 struct ospf
*ospf
= oi
->ospf
;
499 ospf_fifo_free(oi
->obuf
);
502 /*reset protocol stats */
503 ospf_if_reset_stats(oi
);
505 if (oi
->on_write_q
) {
506 listnode_delete(ospf
->oi_write_q
, oi
);
507 if (list_isempty(ospf
->oi_write_q
))
508 OSPF_TIMER_OFF(ospf
->t_write
);
515 static struct ospf_if_params
*ospf_new_if_params(void)
517 struct ospf_if_params
*oip
;
519 oip
= XCALLOC(MTYPE_OSPF_IF_PARAMS
, sizeof(struct ospf_if_params
));
524 UNSET_IF_PARAM(oip
, output_cost_cmd
);
525 UNSET_IF_PARAM(oip
, transmit_delay
);
526 UNSET_IF_PARAM(oip
, retransmit_interval
);
527 UNSET_IF_PARAM(oip
, passive_interface
);
528 UNSET_IF_PARAM(oip
, v_hello
);
529 UNSET_IF_PARAM(oip
, fast_hello
);
530 UNSET_IF_PARAM(oip
, v_wait
);
531 UNSET_IF_PARAM(oip
, priority
);
532 UNSET_IF_PARAM(oip
, type
);
533 UNSET_IF_PARAM(oip
, auth_simple
);
534 UNSET_IF_PARAM(oip
, auth_crypt
);
535 UNSET_IF_PARAM(oip
, auth_type
);
537 oip
->auth_crypt
= list_new();
539 oip
->network_lsa_seqnum
= htonl(OSPF_INITIAL_SEQUENCE_NUMBER
);
544 void ospf_del_if_params(struct ospf_if_params
*oip
)
546 list_delete_and_null(&oip
->auth_crypt
);
547 bfd_info_free(&(oip
->bfd_info
));
548 XFREE(MTYPE_OSPF_IF_PARAMS
, oip
);
551 void ospf_free_if_params(struct interface
*ifp
, struct in_addr addr
)
553 struct ospf_if_params
*oip
;
554 struct prefix_ipv4 p
;
555 struct route_node
*rn
;
558 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
560 rn
= route_node_lookup(IF_OIFS_PARAMS(ifp
), (struct prefix
*)&p
);
561 if (!rn
|| !rn
->info
)
565 route_unlock_node(rn
);
567 if (!OSPF_IF_PARAM_CONFIGURED(oip
, output_cost_cmd
)
568 && !OSPF_IF_PARAM_CONFIGURED(oip
, transmit_delay
)
569 && !OSPF_IF_PARAM_CONFIGURED(oip
, retransmit_interval
)
570 && !OSPF_IF_PARAM_CONFIGURED(oip
, passive_interface
)
571 && !OSPF_IF_PARAM_CONFIGURED(oip
, v_hello
)
572 && !OSPF_IF_PARAM_CONFIGURED(oip
, fast_hello
)
573 && !OSPF_IF_PARAM_CONFIGURED(oip
, v_wait
)
574 && !OSPF_IF_PARAM_CONFIGURED(oip
, priority
)
575 && !OSPF_IF_PARAM_CONFIGURED(oip
, type
)
576 && !OSPF_IF_PARAM_CONFIGURED(oip
, auth_simple
)
577 && !OSPF_IF_PARAM_CONFIGURED(oip
, auth_type
)
578 && listcount(oip
->auth_crypt
) == 0
579 && ntohl(oip
->network_lsa_seqnum
) != OSPF_INITIAL_SEQUENCE_NUMBER
) {
580 ospf_del_if_params(oip
);
582 route_unlock_node(rn
);
586 struct ospf_if_params
*ospf_lookup_if_params(struct interface
*ifp
,
589 struct prefix_ipv4 p
;
590 struct route_node
*rn
;
593 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
596 rn
= route_node_lookup(IF_OIFS_PARAMS(ifp
), (struct prefix
*)&p
);
599 route_unlock_node(rn
);
606 struct ospf_if_params
*ospf_get_if_params(struct interface
*ifp
,
609 struct prefix_ipv4 p
;
610 struct route_node
*rn
;
613 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
617 rn
= route_node_get(IF_OIFS_PARAMS(ifp
), (struct prefix
*)&p
);
619 if (rn
->info
== NULL
)
620 rn
->info
= ospf_new_if_params();
622 route_unlock_node(rn
);
627 void ospf_if_update_params(struct interface
*ifp
, struct in_addr addr
)
629 struct route_node
*rn
;
630 struct ospf_interface
*oi
;
632 for (rn
= route_top(IF_OIFS(ifp
)); rn
; rn
= route_next(rn
)) {
633 if ((oi
= rn
->info
) == NULL
)
636 if (IPV4_ADDR_SAME(&oi
->address
->u
.prefix4
, &addr
))
637 oi
->params
= ospf_lookup_if_params(
638 ifp
, oi
->address
->u
.prefix4
);
642 int ospf_if_new_hook(struct interface
*ifp
)
646 ifp
->info
= XCALLOC(MTYPE_OSPF_IF_INFO
, sizeof(struct ospf_if_info
));
648 IF_OIFS(ifp
) = route_table_init();
649 IF_OIFS_PARAMS(ifp
) = route_table_init();
651 IF_DEF_PARAMS(ifp
) = ospf_new_if_params();
653 SET_IF_PARAM(IF_DEF_PARAMS(ifp
), transmit_delay
);
654 IF_DEF_PARAMS(ifp
)->transmit_delay
= OSPF_TRANSMIT_DELAY_DEFAULT
;
656 SET_IF_PARAM(IF_DEF_PARAMS(ifp
), retransmit_interval
);
657 IF_DEF_PARAMS(ifp
)->retransmit_interval
=
658 OSPF_RETRANSMIT_INTERVAL_DEFAULT
;
660 SET_IF_PARAM(IF_DEF_PARAMS(ifp
), priority
);
661 IF_DEF_PARAMS(ifp
)->priority
= OSPF_ROUTER_PRIORITY_DEFAULT
;
663 IF_DEF_PARAMS(ifp
)->mtu_ignore
= OSPF_MTU_IGNORE_DEFAULT
;
665 SET_IF_PARAM(IF_DEF_PARAMS(ifp
), v_hello
);
666 IF_DEF_PARAMS(ifp
)->v_hello
= OSPF_HELLO_INTERVAL_DEFAULT
;
668 SET_IF_PARAM(IF_DEF_PARAMS(ifp
), fast_hello
);
669 IF_DEF_PARAMS(ifp
)->fast_hello
= OSPF_FAST_HELLO_DEFAULT
;
671 SET_IF_PARAM(IF_DEF_PARAMS(ifp
), v_wait
);
672 IF_DEF_PARAMS(ifp
)->v_wait
= OSPF_ROUTER_DEAD_INTERVAL_DEFAULT
;
674 SET_IF_PARAM(IF_DEF_PARAMS(ifp
), auth_simple
);
675 memset(IF_DEF_PARAMS(ifp
)->auth_simple
, 0, OSPF_AUTH_SIMPLE_SIZE
);
677 SET_IF_PARAM(IF_DEF_PARAMS(ifp
), auth_type
);
678 IF_DEF_PARAMS(ifp
)->auth_type
= OSPF_AUTH_NOTSET
;
680 rc
= ospf_opaque_new_if(ifp
);
684 static int ospf_if_delete_hook(struct interface
*ifp
)
687 struct route_node
*rn
;
688 rc
= ospf_opaque_del_if(ifp
);
690 route_table_finish(IF_OIFS(ifp
));
692 for (rn
= route_top(IF_OIFS_PARAMS(ifp
)); rn
; rn
= route_next(rn
))
694 ospf_del_if_params(rn
->info
);
695 route_table_finish(IF_OIFS_PARAMS(ifp
));
697 ospf_del_if_params((struct ospf_if_params
*)IF_DEF_PARAMS(ifp
));
698 XFREE(MTYPE_OSPF_IF_INFO
, ifp
->info
);
704 int ospf_if_is_enable(struct ospf_interface
*oi
)
706 if (!(if_is_loopback(oi
->ifp
) || if_is_vrf(oi
->ifp
)))
707 if (if_is_up(oi
->ifp
))
713 void ospf_if_set_multicast(struct ospf_interface
*oi
)
715 if ((oi
->state
> ISM_Loopback
) && (oi
->type
!= OSPF_IFTYPE_LOOPBACK
)
716 && (oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
)
717 && (OSPF_IF_PASSIVE_STATUS(oi
) == OSPF_IF_ACTIVE
)) {
718 /* The interface should belong to the OSPF-all-routers group. */
719 if (!OI_MEMBER_CHECK(oi
, MEMBER_ALLROUTERS
)
720 && (ospf_if_add_allspfrouters(oi
->ospf
, oi
->address
,
723 /* Set the flag only if the system call to join
725 OI_MEMBER_JOINED(oi
, MEMBER_ALLROUTERS
);
727 /* The interface should NOT belong to the OSPF-all-routers
729 if (OI_MEMBER_CHECK(oi
, MEMBER_ALLROUTERS
)) {
730 /* Only actually drop if this is the last reference */
731 if (OI_MEMBER_COUNT(oi
, MEMBER_ALLROUTERS
) == 1)
732 ospf_if_drop_allspfrouters(oi
->ospf
,
735 /* Unset the flag regardless of whether the system call
737 the group succeeded, since it's much safer to assume
739 we are not a member. */
740 OI_MEMBER_LEFT(oi
, MEMBER_ALLROUTERS
);
744 if (((oi
->type
== OSPF_IFTYPE_BROADCAST
)
745 || (oi
->type
== OSPF_IFTYPE_POINTOPOINT
))
746 && ((oi
->state
== ISM_DR
) || (oi
->state
== ISM_Backup
))
747 && (OSPF_IF_PASSIVE_STATUS(oi
) == OSPF_IF_ACTIVE
)) {
748 /* The interface should belong to the OSPF-designated-routers
750 if (!OI_MEMBER_CHECK(oi
, MEMBER_DROUTERS
)
751 && (ospf_if_add_alldrouters(oi
->ospf
, oi
->address
,
754 /* Set the flag only if the system call to join
756 OI_MEMBER_JOINED(oi
, MEMBER_DROUTERS
);
758 /* The interface should NOT belong to the
759 * OSPF-designated-routers group */
760 if (OI_MEMBER_CHECK(oi
, MEMBER_DROUTERS
)) {
761 /* drop only if last reference */
762 if (OI_MEMBER_COUNT(oi
, MEMBER_DROUTERS
) == 1)
763 ospf_if_drop_alldrouters(oi
->ospf
, oi
->address
,
766 /* Unset the flag regardless of whether the system call
768 the group succeeded, since it's much safer to assume
770 we are not a member. */
771 OI_MEMBER_LEFT(oi
, MEMBER_DROUTERS
);
776 int ospf_if_up(struct ospf_interface
*oi
)
781 if (oi
->type
== OSPF_IFTYPE_LOOPBACK
)
782 OSPF_ISM_EVENT_SCHEDULE(oi
, ISM_LoopInd
);
784 ospf_if_stream_set(oi
);
785 OSPF_ISM_EVENT_SCHEDULE(oi
, ISM_InterfaceUp
);
791 int ospf_if_down(struct ospf_interface
*oi
)
796 OSPF_ISM_EVENT_EXECUTE(oi
, ISM_InterfaceDown
);
797 /* delete position in router LSA */
800 /* Shutdown packet reception and sending */
801 ospf_if_stream_unset(oi
);
807 /* Virtual Link related functions. */
809 struct ospf_vl_data
*ospf_vl_data_new(struct ospf_area
*area
,
810 struct in_addr vl_peer
)
812 struct ospf_vl_data
*vl_data
;
814 vl_data
= XCALLOC(MTYPE_OSPF_VL_DATA
, sizeof(struct ospf_vl_data
));
816 vl_data
->vl_peer
.s_addr
= vl_peer
.s_addr
;
817 vl_data
->vl_area_id
= area
->area_id
;
818 vl_data
->vl_area_id_fmt
= area
->area_id_fmt
;
823 void ospf_vl_data_free(struct ospf_vl_data
*vl_data
)
825 XFREE(MTYPE_OSPF_VL_DATA
, vl_data
);
828 u_int vlink_count
= 0;
830 struct ospf_interface
*ospf_vl_new(struct ospf
*ospf
,
831 struct ospf_vl_data
*vl_data
)
833 struct ospf_interface
*voi
;
834 struct interface
*vi
;
835 char ifname
[INTERFACE_NAMSIZ
];
836 struct ospf_area
*area
;
837 struct in_addr area_id
;
838 struct connected
*co
;
839 struct prefix_ipv4
*p
;
841 if (IS_DEBUG_OSPF_EVENT
)
842 zlog_debug("ospf_vl_new(): Start");
843 if (vlink_count
== OSPF_VL_MAX_COUNT
) {
844 if (IS_DEBUG_OSPF_EVENT
)
846 "ospf_vl_new(): Alarm: "
847 "cannot create more than OSPF_MAX_VL_COUNT virtual links");
851 if (IS_DEBUG_OSPF_EVENT
)
853 "ospf_vl_new(): creating pseudo zebra interface vrf id %u",
856 snprintf(ifname
, sizeof(ifname
), "VLINK%d", vlink_count
);
857 vi
= if_create(ifname
, ospf
->vrf_id
);
859 * if_create sets ZEBRA_INTERFACE_LINKDETECTION
860 * virtual links don't need this.
862 UNSET_FLAG(vi
->status
, ZEBRA_INTERFACE_LINKDETECTION
);
863 co
= connected_new();
865 listnode_add(vi
->connected
, co
);
867 p
= prefix_ipv4_new();
869 p
->prefix
.s_addr
= 0;
872 co
->address
= (struct prefix
*)p
;
874 voi
= ospf_if_new(ospf
, vi
, co
->address
);
876 if (IS_DEBUG_OSPF_EVENT
)
878 "ospf_vl_new(): Alarm: OSPF int structure is not created");
882 voi
->vl_data
= vl_data
;
883 voi
->ifp
->mtu
= OSPF_VL_MTU
;
884 voi
->type
= OSPF_IFTYPE_VIRTUALLINK
;
887 if (IS_DEBUG_OSPF_EVENT
)
888 zlog_debug("ospf_vl_new(): Created name: %s", ifname
);
889 if (IS_DEBUG_OSPF_EVENT
)
890 zlog_debug("ospf_vl_new(): set if->name to %s", vi
->name
);
893 area
= ospf_area_get(ospf
, area_id
);
896 if (IS_DEBUG_OSPF_EVENT
)
898 "ospf_vl_new(): set associated area to the backbone");
900 /* Add pseudo neighbor. */
901 ospf_nbr_self_reset(voi
, voi
->ospf
->router_id
);
903 ospf_area_add_if(voi
->area
, voi
);
905 ospf_if_stream_set(voi
);
907 if (IS_DEBUG_OSPF_EVENT
)
908 zlog_debug("ospf_vl_new(): Stop");
912 static void ospf_vl_if_delete(struct ospf_vl_data
*vl_data
)
914 struct interface
*ifp
= vl_data
->vl_oi
->ifp
;
915 vl_data
->vl_oi
->address
->u
.prefix4
.s_addr
= 0;
916 vl_data
->vl_oi
->address
->prefixlen
= 0;
917 ospf_if_free(vl_data
->vl_oi
);
922 /* Look up vl_data for given peer, optionally qualified to be in the
923 * specified area. NULL area returns first found..
925 struct ospf_vl_data
*ospf_vl_lookup(struct ospf
*ospf
, struct ospf_area
*area
,
926 struct in_addr vl_peer
)
928 struct ospf_vl_data
*vl_data
;
929 struct listnode
*node
;
931 if (IS_DEBUG_OSPF_EVENT
) {
932 zlog_debug("%s: Looking for %s", __func__
, inet_ntoa(vl_peer
));
934 zlog_debug("%s: in area %s", __func__
,
935 inet_ntoa(area
->area_id
));
938 for (ALL_LIST_ELEMENTS_RO(ospf
->vlinks
, node
, vl_data
)) {
939 if (IS_DEBUG_OSPF_EVENT
)
940 zlog_debug("%s: VL %s, peer %s", __func__
,
941 vl_data
->vl_oi
->ifp
->name
,
942 inet_ntoa(vl_data
->vl_peer
));
945 && !IPV4_ADDR_SAME(&vl_data
->vl_area_id
, &area
->area_id
))
948 if (IPV4_ADDR_SAME(&vl_data
->vl_peer
, &vl_peer
))
955 static void ospf_vl_shutdown(struct ospf_vl_data
*vl_data
)
957 struct ospf_interface
*oi
;
959 if ((oi
= vl_data
->vl_oi
) == NULL
)
962 oi
->address
->u
.prefix4
.s_addr
= 0;
963 oi
->address
->prefixlen
= 0;
965 UNSET_FLAG(oi
->ifp
->flags
, IFF_UP
);
966 /* OSPF_ISM_EVENT_SCHEDULE (oi, ISM_InterfaceDown); */
967 OSPF_ISM_EVENT_EXECUTE(oi
, ISM_InterfaceDown
);
970 void ospf_vl_add(struct ospf
*ospf
, struct ospf_vl_data
*vl_data
)
972 listnode_add(ospf
->vlinks
, vl_data
);
973 hook_call(ospf_vl_add
, vl_data
);
976 void ospf_vl_delete(struct ospf
*ospf
, struct ospf_vl_data
*vl_data
)
978 ospf_vl_shutdown(vl_data
);
979 ospf_vl_if_delete(vl_data
);
981 hook_call(ospf_vl_delete
, vl_data
);
982 listnode_delete(ospf
->vlinks
, vl_data
);
984 ospf_vl_data_free(vl_data
);
987 static int ospf_vl_set_params(struct ospf_vl_data
*vl_data
, struct vertex
*v
)
990 struct ospf_interface
*voi
;
991 struct listnode
*node
;
992 struct vertex_parent
*vp
= NULL
;
994 struct router_lsa
*rl
;
996 voi
= vl_data
->vl_oi
;
998 if (voi
->output_cost
!= v
->distance
) {
1000 voi
->output_cost
= v
->distance
;
1004 for (ALL_LIST_ELEMENTS_RO(v
->parents
, node
, vp
)) {
1005 vl_data
->nexthop
.oi
= vp
->nexthop
->oi
;
1006 vl_data
->nexthop
.router
= vp
->nexthop
->router
;
1008 if (!IPV4_ADDR_SAME(&voi
->address
->u
.prefix4
,
1009 &vl_data
->nexthop
.oi
->address
->u
.prefix4
))
1012 voi
->address
->u
.prefix4
=
1013 vl_data
->nexthop
.oi
->address
->u
.prefix4
;
1014 voi
->address
->prefixlen
=
1015 vl_data
->nexthop
.oi
->address
->prefixlen
;
1017 break; /* We take the first interface. */
1020 rl
= (struct router_lsa
*)v
->lsa
;
1022 /* use SPF determined backlink index in struct vertex
1023 * for virtual link destination address
1025 if (vp
&& vp
->backlink
>= 0) {
1026 if (!IPV4_ADDR_SAME(&vl_data
->peer_addr
,
1027 &rl
->link
[vp
->backlink
].link_data
))
1029 vl_data
->peer_addr
= rl
->link
[vp
->backlink
].link_data
;
1031 /* This is highly odd, there is no backlink index
1032 * there should be due to the ospf_spf_has_link() check
1033 * in SPF. Lets warn and try pick a link anyway.
1035 zlog_warn("ospf_vl_set_params: No backlink for %s!",
1036 vl_data
->vl_oi
->ifp
->name
);
1037 for (i
= 0; i
< ntohs(rl
->links
); i
++) {
1038 switch (rl
->link
[i
].type
) {
1039 case LSA_LINK_TYPE_VIRTUALLINK
:
1040 if (IS_DEBUG_OSPF_EVENT
)
1042 "found back link through VL");
1044 case LSA_LINK_TYPE_TRANSIT
:
1045 case LSA_LINK_TYPE_POINTOPOINT
:
1046 if (!IPV4_ADDR_SAME(&vl_data
->peer_addr
,
1047 &rl
->link
[i
].link_data
))
1049 vl_data
->peer_addr
= rl
->link
[i
].link_data
;
1054 if (IS_DEBUG_OSPF_EVENT
)
1055 zlog_debug("%s: %s peer address: %s, cost: %d,%schanged",
1056 __func__
, vl_data
->vl_oi
->ifp
->name
,
1057 inet_ntoa(vl_data
->peer_addr
), voi
->output_cost
,
1058 (changed
? " " : " un"));
1064 void ospf_vl_up_check(struct ospf_area
*area
, struct in_addr rid
,
1067 struct ospf
*ospf
= area
->ospf
;
1068 struct listnode
*node
;
1069 struct ospf_vl_data
*vl_data
;
1070 struct ospf_interface
*oi
;
1072 if (IS_DEBUG_OSPF_EVENT
) {
1073 zlog_debug("ospf_vl_up_check(): Start");
1074 zlog_debug("ospf_vl_up_check(): Router ID is %s",
1076 zlog_debug("ospf_vl_up_check(): Area is %s",
1077 inet_ntoa(area
->area_id
));
1080 for (ALL_LIST_ELEMENTS_RO(ospf
->vlinks
, node
, vl_data
)) {
1081 if (IS_DEBUG_OSPF_EVENT
) {
1082 zlog_debug("%s: considering VL, %s in area %s",
1083 __func__
, vl_data
->vl_oi
->ifp
->name
,
1084 inet_ntoa(vl_data
->vl_area_id
));
1085 zlog_debug("%s: peer ID: %s", __func__
,
1086 inet_ntoa(vl_data
->vl_peer
));
1089 if (IPV4_ADDR_SAME(&vl_data
->vl_peer
, &rid
)
1090 && IPV4_ADDR_SAME(&vl_data
->vl_area_id
, &area
->area_id
)) {
1091 oi
= vl_data
->vl_oi
;
1092 SET_FLAG(vl_data
->flags
, OSPF_VL_FLAG_APPROVED
);
1094 if (IS_DEBUG_OSPF_EVENT
)
1096 "ospf_vl_up_check(): this VL matched");
1098 if (oi
->state
== ISM_Down
) {
1099 if (IS_DEBUG_OSPF_EVENT
)
1101 "ospf_vl_up_check(): VL is down, waking it up");
1102 SET_FLAG(oi
->ifp
->flags
, IFF_UP
);
1103 OSPF_ISM_EVENT_EXECUTE(oi
, ISM_InterfaceUp
);
1106 if (ospf_vl_set_params(vl_data
, v
)) {
1107 if (IS_DEBUG_OSPF(ism
, ISM_EVENTS
))
1109 "ospf_vl_up_check: VL cost change,"
1110 " scheduling router lsa refresh");
1112 ospf_router_lsa_update_area(
1114 else if (IS_DEBUG_OSPF(ism
, ISM_EVENTS
))
1116 "ospf_vl_up_check: VL cost change, no backbone!");
1122 void ospf_vl_unapprove(struct ospf
*ospf
)
1124 struct listnode
*node
;
1125 struct ospf_vl_data
*vl_data
;
1127 for (ALL_LIST_ELEMENTS_RO(ospf
->vlinks
, node
, vl_data
))
1128 UNSET_FLAG(vl_data
->flags
, OSPF_VL_FLAG_APPROVED
);
1131 void ospf_vl_shut_unapproved(struct ospf
*ospf
)
1133 struct listnode
*node
, *nnode
;
1134 struct ospf_vl_data
*vl_data
;
1136 for (ALL_LIST_ELEMENTS(ospf
->vlinks
, node
, nnode
, vl_data
))
1137 if (!CHECK_FLAG(vl_data
->flags
, OSPF_VL_FLAG_APPROVED
))
1138 ospf_vl_shutdown(vl_data
);
1141 int ospf_full_virtual_nbrs(struct ospf_area
*area
)
1143 if (IS_DEBUG_OSPF_EVENT
) {
1145 "counting fully adjacent virtual neighbors in area %s",
1146 inet_ntoa(area
->area_id
));
1147 zlog_debug("there are %d of them", area
->full_vls
);
1150 return area
->full_vls
;
1153 int ospf_vls_in_area(struct ospf_area
*area
)
1155 struct listnode
*node
;
1156 struct ospf_vl_data
*vl_data
;
1159 for (ALL_LIST_ELEMENTS_RO(area
->ospf
->vlinks
, node
, vl_data
))
1160 if (IPV4_ADDR_SAME(&vl_data
->vl_area_id
, &area
->area_id
))
1167 struct crypt_key
*ospf_crypt_key_new()
1169 return XCALLOC(MTYPE_OSPF_CRYPT_KEY
, sizeof(struct crypt_key
));
1172 void ospf_crypt_key_add(struct list
*crypt
, struct crypt_key
*ck
)
1174 listnode_add(crypt
, ck
);
1177 struct crypt_key
*ospf_crypt_key_lookup(struct list
*auth_crypt
, u_char key_id
)
1179 struct listnode
*node
;
1180 struct crypt_key
*ck
;
1182 for (ALL_LIST_ELEMENTS_RO(auth_crypt
, node
, ck
))
1183 if (ck
->key_id
== key_id
)
1189 int ospf_crypt_key_delete(struct list
*auth_crypt
, u_char key_id
)
1191 struct listnode
*node
, *nnode
;
1192 struct crypt_key
*ck
;
1194 for (ALL_LIST_ELEMENTS(auth_crypt
, node
, nnode
, ck
)) {
1195 if (ck
->key_id
== key_id
) {
1196 listnode_delete(auth_crypt
, ck
);
1197 XFREE(MTYPE_OSPF_CRYPT_KEY
, ck
);
1205 u_char
ospf_default_iftype(struct interface
*ifp
)
1207 if (if_is_pointopoint(ifp
))
1208 return OSPF_IFTYPE_POINTOPOINT
;
1209 else if (if_is_loopback(ifp
) || if_is_vrf(ifp
))
1210 return OSPF_IFTYPE_LOOPBACK
;
1212 return OSPF_IFTYPE_BROADCAST
;
1217 /* Initialize Zebra interface data structure. */
1218 hook_register_prio(if_add
, 0, ospf_if_new_hook
);
1219 hook_register_prio(if_del
, 0, ospf_if_delete_hook
);