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
))
53 DEFINE_HOOK(ospf_if_update
, (struct interface
* ifp
), (ifp
))
54 DEFINE_HOOK(ospf_if_delete
, (struct interface
* ifp
), (ifp
))
56 int ospf_interface_neighbor_count(struct ospf_interface
*oi
)
59 struct route_node
*rn
;
60 struct ospf_neighbor
*nbr
= NULL
;
62 for (rn
= route_top(oi
->nbrs
); rn
; rn
= route_next(rn
)) {
65 /* Do not show myself. */
66 if (nbr
== oi
->nbr_self
)
68 /* Down state is not shown. */
69 if (nbr
->state
== NSM_Down
)
78 int ospf_if_get_output_cost(struct ospf_interface
*oi
)
80 /* If all else fails, use default OSPF cost */
84 /* ifp speed and bw can be 0 in some platforms, use ospf default bw
85 if bw is configured under interface it would be used.
87 if (!oi
->ifp
->bandwidth
&& oi
->ifp
->speed
)
90 bw
= oi
->ifp
->bandwidth
? oi
->ifp
->bandwidth
91 : OSPF_DEFAULT_BANDWIDTH
;
92 refbw
= oi
->ospf
->ref_bandwidth
;
94 /* A specifed ip ospf cost overrides a calculated one. */
95 if (OSPF_IF_PARAM_CONFIGURED(IF_DEF_PARAMS(oi
->ifp
), output_cost_cmd
)
96 || OSPF_IF_PARAM_CONFIGURED(oi
->params
, output_cost_cmd
))
97 cost
= OSPF_IF_PARAM(oi
, output_cost_cmd
);
98 /* See if a cost can be calculated from the zebra processes
99 interface bandwidth field. */
101 cost
= (uint32_t)((double)refbw
/ (double)bw
+ (double)0.5);
104 else if (cost
> 65535)
111 void ospf_if_recalculate_output_cost(struct interface
*ifp
)
114 struct route_node
*rn
;
116 for (rn
= route_top(IF_OIFS(ifp
)); rn
; rn
= route_next(rn
)) {
117 struct ospf_interface
*oi
;
119 if ((oi
= rn
->info
) == NULL
)
122 newcost
= ospf_if_get_output_cost(oi
);
124 /* Is actual output cost changed? */
125 if (oi
->output_cost
!= newcost
) {
126 oi
->output_cost
= newcost
;
127 ospf_router_lsa_update_area(oi
->area
);
132 /* Simulate down/up on the interface. This is needed, for example, when
134 void ospf_if_reset(struct interface
*ifp
)
136 struct route_node
*rn
;
138 for (rn
= route_top(IF_OIFS(ifp
)); rn
; rn
= route_next(rn
)) {
139 struct ospf_interface
*oi
;
141 if ((oi
= rn
->info
) == NULL
)
149 void ospf_if_reset_variables(struct ospf_interface
*oi
)
151 /* Set default values. */
152 /* don't clear this flag. oi->flag = OSPF_IF_DISABLE; */
155 oi
->type
= OSPF_IFTYPE_VIRTUALLINK
;
157 /* preserve network-type */
158 if (oi
->type
!= OSPF_IFTYPE_NBMA
)
159 oi
->type
= OSPF_IFTYPE_BROADCAST
;
161 oi
->state
= ISM_Down
;
163 oi
->crypt_seqnum
= 0;
165 /* This must be short, (less than RxmtInterval)
166 - RFC 2328 Section 13.5 para 3. Set to 1 second to avoid Acks being
167 held back for too long - MAG */
171 /* lookup oi for specified prefix/ifp */
172 struct ospf_interface
*ospf_if_table_lookup(struct interface
*ifp
,
173 struct prefix
*prefix
)
176 struct route_node
*rn
;
177 struct ospf_interface
*rninfo
= NULL
;
180 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
182 /* route_node_get implicitely locks */
183 if ((rn
= route_node_lookup(IF_OIFS(ifp
), &p
))) {
184 rninfo
= (struct ospf_interface
*)rn
->info
;
185 route_unlock_node(rn
);
191 static void ospf_add_to_if(struct interface
*ifp
, struct ospf_interface
*oi
)
193 struct route_node
*rn
;
197 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
200 rn
= route_node_get(IF_OIFS(ifp
), &p
);
201 /* rn->info should either be NULL or equal to this oi
202 * as route_node_get may return an existing node
204 assert(!rn
->info
|| rn
->info
== oi
);
208 static void ospf_delete_from_if(struct interface
*ifp
,
209 struct ospf_interface
*oi
)
211 struct route_node
*rn
;
215 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
217 rn
= route_node_lookup(IF_OIFS(oi
->ifp
), &p
);
221 route_unlock_node(rn
);
222 route_unlock_node(rn
);
225 struct ospf_interface
*ospf_if_new(struct ospf
*ospf
, struct interface
*ifp
,
228 struct ospf_interface
*oi
;
230 oi
= ospf_if_table_lookup(ifp
, p
);
234 oi
= XCALLOC(MTYPE_OSPF_IF
, sizeof(struct ospf_interface
));
236 oi
->obuf
= ospf_fifo_new();
238 /* Set zebra interface pointer. */
242 ospf_add_to_if(ifp
, oi
);
243 listnode_add(ospf
->oiflist
, oi
);
245 /* Initialize neighbor list. */
246 oi
->nbrs
= route_table_init();
248 /* Initialize static neighbor list. */
249 oi
->nbr_nbma
= list_new();
251 /* Initialize Link State Acknowledgment list. */
252 oi
->ls_ack
= list_new();
253 oi
->ls_ack_direct
.ls_ack
= list_new();
255 /* Set default values. */
256 ospf_if_reset_variables(oi
);
258 /* Set pseudo neighbor to Null */
261 oi
->ls_upd_queue
= route_table_init();
262 oi
->t_ls_upd_event
= NULL
;
263 oi
->t_ls_ack_direct
= NULL
;
265 oi
->crypt_seqnum
= time(NULL
);
267 ospf_opaque_type9_lsa_init(oi
);
271 QOBJ_REG(oi
, ospf_interface
);
273 if (IS_DEBUG_OSPF_EVENT
)
274 zlog_debug("%s: ospf interface %s vrf %s id %u created",
275 __PRETTY_FUNCTION__
, ifp
->name
,
276 ospf_vrf_id_to_name(ospf
->vrf_id
), ospf
->vrf_id
);
281 /* Restore an interface to its pre UP state
282 Used from ism_interface_down only */
283 void ospf_if_cleanup(struct ospf_interface
*oi
)
285 struct route_node
*rn
;
286 struct listnode
*node
, *nnode
;
287 struct ospf_neighbor
*nbr
;
288 struct ospf_nbr_nbma
*nbr_nbma
;
289 struct ospf_lsa
*lsa
;
291 /* oi->nbrs and oi->nbr_nbma should be deleted on InterfaceDown event */
292 /* delete all static neighbors attached to this interface */
293 for (ALL_LIST_ELEMENTS(oi
->nbr_nbma
, node
, nnode
, nbr_nbma
)) {
294 OSPF_POLL_TIMER_OFF(nbr_nbma
->t_poll
);
297 nbr_nbma
->nbr
->nbr_nbma
= NULL
;
298 nbr_nbma
->nbr
= NULL
;
303 listnode_delete(oi
->nbr_nbma
, nbr_nbma
);
306 /* send Neighbor event KillNbr to all associated neighbors. */
307 for (rn
= route_top(oi
->nbrs
); rn
; rn
= route_next(rn
))
308 if ((nbr
= rn
->info
) != NULL
)
309 if (nbr
!= oi
->nbr_self
)
310 OSPF_NSM_EVENT_EXECUTE(nbr
, NSM_KillNbr
);
312 /* Cleanup Link State Acknowlegdment list. */
313 for (ALL_LIST_ELEMENTS(oi
->ls_ack
, node
, nnode
, lsa
))
314 ospf_lsa_unlock(&lsa
); /* oi->ls_ack */
315 list_delete_all_node(oi
->ls_ack
);
317 oi
->crypt_seqnum
= 0;
319 /* Empty link state update queue */
320 ospf_ls_upd_queue_empty(oi
);
322 /* Reset pseudo neighbor. */
323 ospf_nbr_self_reset(oi
, oi
->ospf
->router_id
);
326 void ospf_if_free(struct ospf_interface
*oi
)
330 ospf_fifo_free(oi
->obuf
);
332 assert(oi
->state
== ISM_Down
);
334 ospf_opaque_type9_lsa_term(oi
);
338 /* Free Pseudo Neighbour */
339 ospf_nbr_delete(oi
->nbr_self
);
341 route_table_finish(oi
->nbrs
);
342 route_table_finish(oi
->ls_upd_queue
);
344 /* Free any lists that should be freed */
345 list_delete(&oi
->nbr_nbma
);
347 list_delete(&oi
->ls_ack
);
348 list_delete(&oi
->ls_ack_direct
.ls_ack
);
350 if (IS_DEBUG_OSPF_EVENT
)
351 zlog_debug("%s: ospf interface %s vrf %s id %u deleted",
352 __PRETTY_FUNCTION__
, oi
->ifp
->name
,
353 ospf_vrf_id_to_name(oi
->ifp
->vrf_id
),
356 ospf_delete_from_if(oi
->ifp
, oi
);
358 listnode_delete(oi
->ospf
->oiflist
, oi
);
359 listnode_delete(oi
->area
->oiflist
, oi
);
361 thread_cancel_event(master
, oi
);
363 memset(oi
, 0, sizeof(*oi
));
364 XFREE(MTYPE_OSPF_IF
, oi
);
367 int ospf_if_is_up(struct ospf_interface
*oi
)
369 return if_is_up(oi
->ifp
);
372 struct ospf_interface
*ospf_if_exists(struct ospf_interface
*oic
)
374 struct listnode
*node
;
376 struct ospf_interface
*oi
;
385 for (ALL_LIST_ELEMENTS_RO(ospf
->oiflist
, node
, oi
))
392 /* Lookup OSPF interface by router LSA posistion */
393 struct ospf_interface
*ospf_if_lookup_by_lsa_pos(struct ospf_area
*area
,
396 struct listnode
*node
;
397 struct ospf_interface
*oi
;
399 for (ALL_LIST_ELEMENTS_RO(area
->oiflist
, node
, oi
)) {
400 if (lsa_pos
>= oi
->lsa_pos_beg
&& lsa_pos
< oi
->lsa_pos_end
)
406 struct ospf_interface
*ospf_if_lookup_by_local_addr(struct ospf
*ospf
,
407 struct interface
*ifp
,
408 struct in_addr address
)
410 struct listnode
*node
;
411 struct ospf_interface
*oi
;
413 for (ALL_LIST_ELEMENTS_RO(ospf
->oiflist
, node
, oi
))
414 if (oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
) {
415 if (ifp
&& oi
->ifp
!= ifp
)
418 if (IPV4_ADDR_SAME(&address
, &oi
->address
->u
.prefix4
))
425 struct ospf_interface
*ospf_if_lookup_by_prefix(struct ospf
*ospf
,
426 struct prefix_ipv4
*p
)
428 struct listnode
*node
;
429 struct ospf_interface
*oi
;
431 /* Check each Interface. */
432 for (ALL_LIST_ELEMENTS_RO(ospf
->oiflist
, node
, oi
)) {
433 if (oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
) {
436 prefix_copy(&ptmp
, CONNECTED_PREFIX(oi
->connected
));
438 if (prefix_same(&ptmp
, (struct prefix
*)p
))
445 /* determine receiving interface by ifp and source address */
446 struct ospf_interface
*ospf_if_lookup_recv_if(struct ospf
*ospf
,
448 struct interface
*ifp
)
450 struct route_node
*rn
;
451 struct prefix_ipv4 addr
;
452 struct ospf_interface
*oi
, *match
;
454 addr
.family
= AF_INET
;
456 addr
.prefixlen
= IPV4_MAX_BITLEN
;
460 for (rn
= route_top(IF_OIFS(ifp
)); rn
; rn
= route_next(rn
)) {
463 if (!oi
) /* oi can be NULL for PtP aliases */
466 if (oi
->type
== OSPF_IFTYPE_VIRTUALLINK
)
469 if (if_is_loopback(oi
->ifp
) || if_is_vrf(oi
->ifp
))
472 if (CHECK_FLAG(oi
->connected
->flags
, ZEBRA_IFA_UNNUMBERED
))
474 else if (prefix_match(CONNECTED_PREFIX(oi
->connected
),
475 (struct prefix
*)&addr
)) {
476 if ((match
== NULL
) || (match
->address
->prefixlen
477 < oi
->address
->prefixlen
))
485 static void ospf_if_reset_stats(struct ospf_interface
*oi
)
487 oi
->hello_in
= oi
->hello_out
= 0;
488 oi
->db_desc_in
= oi
->db_desc_out
= 0;
489 oi
->ls_req_in
= oi
->ls_req_out
= 0;
490 oi
->ls_upd_in
= oi
->ls_upd_out
= 0;
491 oi
->ls_ack_in
= oi
->ls_ack_out
= 0;
494 void ospf_if_stream_unset(struct ospf_interface
*oi
)
496 struct ospf
*ospf
= oi
->ospf
;
498 /* flush the interface packet queue */
499 ospf_fifo_flush(oi
->obuf
);
500 /*reset protocol stats */
501 ospf_if_reset_stats(oi
);
503 if (oi
->on_write_q
) {
504 listnode_delete(ospf
->oi_write_q
, oi
);
505 if (list_isempty(ospf
->oi_write_q
))
506 OSPF_TIMER_OFF(ospf
->t_write
);
512 static struct ospf_if_params
*ospf_new_if_params(void)
514 struct ospf_if_params
*oip
;
516 oip
= XCALLOC(MTYPE_OSPF_IF_PARAMS
, sizeof(struct ospf_if_params
));
518 UNSET_IF_PARAM(oip
, output_cost_cmd
);
519 UNSET_IF_PARAM(oip
, transmit_delay
);
520 UNSET_IF_PARAM(oip
, retransmit_interval
);
521 UNSET_IF_PARAM(oip
, passive_interface
);
522 UNSET_IF_PARAM(oip
, v_hello
);
523 UNSET_IF_PARAM(oip
, fast_hello
);
524 UNSET_IF_PARAM(oip
, v_wait
);
525 UNSET_IF_PARAM(oip
, priority
);
526 UNSET_IF_PARAM(oip
, type
);
527 UNSET_IF_PARAM(oip
, auth_simple
);
528 UNSET_IF_PARAM(oip
, auth_crypt
);
529 UNSET_IF_PARAM(oip
, auth_type
);
531 oip
->auth_crypt
= list_new();
533 oip
->network_lsa_seqnum
= htonl(OSPF_INITIAL_SEQUENCE_NUMBER
);
538 void ospf_del_if_params(struct ospf_if_params
*oip
)
540 list_delete(&oip
->auth_crypt
);
541 bfd_info_free(&(oip
->bfd_info
));
542 XFREE(MTYPE_OSPF_IF_PARAMS
, oip
);
545 void ospf_free_if_params(struct interface
*ifp
, struct in_addr addr
)
547 struct ospf_if_params
*oip
;
548 struct prefix_ipv4 p
;
549 struct route_node
*rn
;
552 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
554 rn
= route_node_lookup(IF_OIFS_PARAMS(ifp
), (struct prefix
*)&p
);
555 if (!rn
|| !rn
->info
)
559 route_unlock_node(rn
);
561 if (!OSPF_IF_PARAM_CONFIGURED(oip
, output_cost_cmd
)
562 && !OSPF_IF_PARAM_CONFIGURED(oip
, transmit_delay
)
563 && !OSPF_IF_PARAM_CONFIGURED(oip
, retransmit_interval
)
564 && !OSPF_IF_PARAM_CONFIGURED(oip
, passive_interface
)
565 && !OSPF_IF_PARAM_CONFIGURED(oip
, v_hello
)
566 && !OSPF_IF_PARAM_CONFIGURED(oip
, fast_hello
)
567 && !OSPF_IF_PARAM_CONFIGURED(oip
, v_wait
)
568 && !OSPF_IF_PARAM_CONFIGURED(oip
, priority
)
569 && !OSPF_IF_PARAM_CONFIGURED(oip
, type
)
570 && !OSPF_IF_PARAM_CONFIGURED(oip
, auth_simple
)
571 && !OSPF_IF_PARAM_CONFIGURED(oip
, auth_type
)
572 && listcount(oip
->auth_crypt
) == 0
573 && ntohl(oip
->network_lsa_seqnum
) != OSPF_INITIAL_SEQUENCE_NUMBER
) {
574 ospf_del_if_params(oip
);
576 route_unlock_node(rn
);
580 struct ospf_if_params
*ospf_lookup_if_params(struct interface
*ifp
,
583 struct prefix_ipv4 p
;
584 struct route_node
*rn
;
587 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
590 rn
= route_node_lookup(IF_OIFS_PARAMS(ifp
), (struct prefix
*)&p
);
593 route_unlock_node(rn
);
600 struct ospf_if_params
*ospf_get_if_params(struct interface
*ifp
,
603 struct prefix_ipv4 p
;
604 struct route_node
*rn
;
607 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
611 rn
= route_node_get(IF_OIFS_PARAMS(ifp
), (struct prefix
*)&p
);
613 if (rn
->info
== NULL
)
614 rn
->info
= ospf_new_if_params();
616 route_unlock_node(rn
);
621 void ospf_if_update_params(struct interface
*ifp
, struct in_addr addr
)
623 struct route_node
*rn
;
624 struct ospf_interface
*oi
;
626 for (rn
= route_top(IF_OIFS(ifp
)); rn
; rn
= route_next(rn
)) {
627 if ((oi
= rn
->info
) == NULL
)
630 if (IPV4_ADDR_SAME(&oi
->address
->u
.prefix4
, &addr
))
631 oi
->params
= ospf_lookup_if_params(
632 ifp
, oi
->address
->u
.prefix4
);
636 int ospf_if_new_hook(struct interface
*ifp
)
640 ifp
->info
= XCALLOC(MTYPE_OSPF_IF_INFO
, sizeof(struct ospf_if_info
));
642 IF_OIFS(ifp
) = route_table_init();
643 IF_OIFS_PARAMS(ifp
) = route_table_init();
645 IF_DEF_PARAMS(ifp
) = ospf_new_if_params();
647 SET_IF_PARAM(IF_DEF_PARAMS(ifp
), transmit_delay
);
648 IF_DEF_PARAMS(ifp
)->transmit_delay
= OSPF_TRANSMIT_DELAY_DEFAULT
;
650 SET_IF_PARAM(IF_DEF_PARAMS(ifp
), retransmit_interval
);
651 IF_DEF_PARAMS(ifp
)->retransmit_interval
=
652 OSPF_RETRANSMIT_INTERVAL_DEFAULT
;
654 SET_IF_PARAM(IF_DEF_PARAMS(ifp
), priority
);
655 IF_DEF_PARAMS(ifp
)->priority
= OSPF_ROUTER_PRIORITY_DEFAULT
;
657 IF_DEF_PARAMS(ifp
)->mtu_ignore
= OSPF_MTU_IGNORE_DEFAULT
;
659 SET_IF_PARAM(IF_DEF_PARAMS(ifp
), v_hello
);
660 IF_DEF_PARAMS(ifp
)->v_hello
= OSPF_HELLO_INTERVAL_DEFAULT
;
662 SET_IF_PARAM(IF_DEF_PARAMS(ifp
), fast_hello
);
663 IF_DEF_PARAMS(ifp
)->fast_hello
= OSPF_FAST_HELLO_DEFAULT
;
665 SET_IF_PARAM(IF_DEF_PARAMS(ifp
), v_wait
);
666 IF_DEF_PARAMS(ifp
)->v_wait
= OSPF_ROUTER_DEAD_INTERVAL_DEFAULT
;
668 SET_IF_PARAM(IF_DEF_PARAMS(ifp
), auth_simple
);
669 memset(IF_DEF_PARAMS(ifp
)->auth_simple
, 0, OSPF_AUTH_SIMPLE_SIZE
);
671 SET_IF_PARAM(IF_DEF_PARAMS(ifp
), auth_type
);
672 IF_DEF_PARAMS(ifp
)->auth_type
= OSPF_AUTH_NOTSET
;
674 rc
= ospf_opaque_new_if(ifp
);
678 static int ospf_if_delete_hook(struct interface
*ifp
)
681 struct route_node
*rn
;
682 rc
= ospf_opaque_del_if(ifp
);
684 route_table_finish(IF_OIFS(ifp
));
686 for (rn
= route_top(IF_OIFS_PARAMS(ifp
)); rn
; rn
= route_next(rn
))
688 ospf_del_if_params(rn
->info
);
689 route_table_finish(IF_OIFS_PARAMS(ifp
));
691 ospf_del_if_params((struct ospf_if_params
*)IF_DEF_PARAMS(ifp
));
692 XFREE(MTYPE_OSPF_IF_INFO
, ifp
->info
);
698 int ospf_if_is_enable(struct ospf_interface
*oi
)
700 if (!(if_is_loopback(oi
->ifp
) || if_is_vrf(oi
->ifp
)))
701 if (if_is_up(oi
->ifp
))
707 void ospf_if_set_multicast(struct ospf_interface
*oi
)
709 if ((oi
->state
> ISM_Loopback
) && (oi
->type
!= OSPF_IFTYPE_LOOPBACK
)
710 && (oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
)
711 && (OSPF_IF_PASSIVE_STATUS(oi
) == OSPF_IF_ACTIVE
)) {
712 /* The interface should belong to the OSPF-all-routers group. */
713 if (!OI_MEMBER_CHECK(oi
, MEMBER_ALLROUTERS
)
714 && (ospf_if_add_allspfrouters(oi
->ospf
, oi
->address
,
717 /* Set the flag only if the system call to join
719 OI_MEMBER_JOINED(oi
, MEMBER_ALLROUTERS
);
721 /* The interface should NOT belong to the OSPF-all-routers
723 if (OI_MEMBER_CHECK(oi
, MEMBER_ALLROUTERS
)) {
724 /* Only actually drop if this is the last reference */
725 if (OI_MEMBER_COUNT(oi
, MEMBER_ALLROUTERS
) == 1)
726 ospf_if_drop_allspfrouters(oi
->ospf
,
729 /* Unset the flag regardless of whether the system call
731 the group succeeded, since it's much safer to assume
733 we are not a member. */
734 OI_MEMBER_LEFT(oi
, MEMBER_ALLROUTERS
);
738 if (((oi
->type
== OSPF_IFTYPE_BROADCAST
)
739 || (oi
->type
== OSPF_IFTYPE_POINTOPOINT
))
740 && ((oi
->state
== ISM_DR
) || (oi
->state
== ISM_Backup
))
741 && (OSPF_IF_PASSIVE_STATUS(oi
) == OSPF_IF_ACTIVE
)) {
742 /* The interface should belong to the OSPF-designated-routers
744 if (!OI_MEMBER_CHECK(oi
, MEMBER_DROUTERS
)
745 && (ospf_if_add_alldrouters(oi
->ospf
, oi
->address
,
748 /* Set the flag only if the system call to join
750 OI_MEMBER_JOINED(oi
, MEMBER_DROUTERS
);
752 /* The interface should NOT belong to the
753 * OSPF-designated-routers group */
754 if (OI_MEMBER_CHECK(oi
, MEMBER_DROUTERS
)) {
755 /* drop only if last reference */
756 if (OI_MEMBER_COUNT(oi
, MEMBER_DROUTERS
) == 1)
757 ospf_if_drop_alldrouters(oi
->ospf
, oi
->address
,
760 /* Unset the flag regardless of whether the system call
762 the group succeeded, since it's much safer to assume
764 we are not a member. */
765 OI_MEMBER_LEFT(oi
, MEMBER_DROUTERS
);
770 int ospf_if_up(struct ospf_interface
*oi
)
775 if (oi
->type
== OSPF_IFTYPE_LOOPBACK
)
776 OSPF_ISM_EVENT_SCHEDULE(oi
, ISM_LoopInd
);
778 OSPF_ISM_EVENT_SCHEDULE(oi
, ISM_InterfaceUp
);
784 int ospf_if_down(struct ospf_interface
*oi
)
789 OSPF_ISM_EVENT_EXECUTE(oi
, ISM_InterfaceDown
);
790 /* delete position in router LSA */
793 /* Shutdown packet reception and sending */
794 ospf_if_stream_unset(oi
);
800 /* Virtual Link related functions. */
802 struct ospf_vl_data
*ospf_vl_data_new(struct ospf_area
*area
,
803 struct in_addr vl_peer
)
805 struct ospf_vl_data
*vl_data
;
807 vl_data
= XCALLOC(MTYPE_OSPF_VL_DATA
, sizeof(struct ospf_vl_data
));
809 vl_data
->vl_peer
.s_addr
= vl_peer
.s_addr
;
810 vl_data
->vl_area_id
= area
->area_id
;
811 vl_data
->vl_area_id_fmt
= area
->area_id_fmt
;
816 void ospf_vl_data_free(struct ospf_vl_data
*vl_data
)
818 XFREE(MTYPE_OSPF_VL_DATA
, vl_data
);
821 unsigned int vlink_count
= 0;
823 struct ospf_interface
*ospf_vl_new(struct ospf
*ospf
,
824 struct ospf_vl_data
*vl_data
)
826 struct ospf_interface
*voi
;
827 struct interface
*vi
;
828 char ifname
[INTERFACE_NAMSIZ
];
829 struct ospf_area
*area
;
830 struct in_addr area_id
;
831 struct connected
*co
;
832 struct prefix_ipv4
*p
;
834 if (IS_DEBUG_OSPF_EVENT
)
835 zlog_debug("ospf_vl_new(): Start");
836 if (vlink_count
== OSPF_VL_MAX_COUNT
) {
837 if (IS_DEBUG_OSPF_EVENT
)
839 "ospf_vl_new(): Alarm: "
840 "cannot create more than OSPF_MAX_VL_COUNT virtual links");
844 if (IS_DEBUG_OSPF_EVENT
)
846 "ospf_vl_new(): creating pseudo zebra interface vrf id %u",
849 snprintf(ifname
, sizeof(ifname
), "VLINK%u", vlink_count
);
850 vi
= if_create_name(ifname
, ospf
->vrf_id
);
852 * if_create_name sets ZEBRA_INTERFACE_LINKDETECTION
853 * virtual links don't need this.
855 UNSET_FLAG(vi
->status
, ZEBRA_INTERFACE_LINKDETECTION
);
856 co
= connected_new();
858 listnode_add(vi
->connected
, co
);
860 p
= prefix_ipv4_new();
862 p
->prefix
.s_addr
= 0;
865 co
->address
= (struct prefix
*)p
;
867 voi
= ospf_if_new(ospf
, vi
, co
->address
);
869 if (IS_DEBUG_OSPF_EVENT
)
871 "ospf_vl_new(): Alarm: OSPF int structure is not created");
875 voi
->vl_data
= vl_data
;
876 voi
->ifp
->mtu
= OSPF_VL_MTU
;
877 voi
->type
= OSPF_IFTYPE_VIRTUALLINK
;
880 if (IS_DEBUG_OSPF_EVENT
)
881 zlog_debug("ospf_vl_new(): Created name: %s", ifname
);
882 if (IS_DEBUG_OSPF_EVENT
)
883 zlog_debug("ospf_vl_new(): set if->name to %s", vi
->name
);
886 area
= ospf_area_get(ospf
, area_id
);
889 if (IS_DEBUG_OSPF_EVENT
)
891 "ospf_vl_new(): set associated area to the backbone");
893 /* Add pseudo neighbor. */
894 ospf_nbr_self_reset(voi
, voi
->ospf
->router_id
);
896 ospf_area_add_if(voi
->area
, voi
);
898 if (IS_DEBUG_OSPF_EVENT
)
899 zlog_debug("ospf_vl_new(): Stop");
903 static void ospf_vl_if_delete(struct ospf_vl_data
*vl_data
)
905 vl_data
->vl_oi
->address
->u
.prefix4
.s_addr
= 0;
906 vl_data
->vl_oi
->address
->prefixlen
= 0;
907 ospf_if_free(vl_data
->vl_oi
);
908 if_delete(&vl_data
->vl_oi
->ifp
);
912 /* for a defined area, count the number of configured vl
914 int ospf_vl_count(struct ospf
*ospf
, struct ospf_area
*area
)
917 struct ospf_vl_data
*vl_data
;
918 struct listnode
*node
;
920 for (ALL_LIST_ELEMENTS_RO(ospf
->vlinks
, node
, vl_data
)) {
922 && !IPV4_ADDR_SAME(&vl_data
->vl_area_id
, &area
->area_id
))
929 /* Look up vl_data for given peer, optionally qualified to be in the
930 * specified area. NULL area returns first found..
932 struct ospf_vl_data
*ospf_vl_lookup(struct ospf
*ospf
, struct ospf_area
*area
,
933 struct in_addr vl_peer
)
935 struct ospf_vl_data
*vl_data
;
936 struct listnode
*node
;
938 if (IS_DEBUG_OSPF_EVENT
) {
939 zlog_debug("%s: Looking for %s", __func__
, inet_ntoa(vl_peer
));
941 zlog_debug("%s: in area %s", __func__
,
942 inet_ntoa(area
->area_id
));
945 for (ALL_LIST_ELEMENTS_RO(ospf
->vlinks
, node
, vl_data
)) {
946 if (IS_DEBUG_OSPF_EVENT
)
947 zlog_debug("%s: VL %s, peer %s", __func__
,
948 vl_data
->vl_oi
->ifp
->name
,
949 inet_ntoa(vl_data
->vl_peer
));
952 && !IPV4_ADDR_SAME(&vl_data
->vl_area_id
, &area
->area_id
))
955 if (IPV4_ADDR_SAME(&vl_data
->vl_peer
, &vl_peer
))
962 static void ospf_vl_shutdown(struct ospf_vl_data
*vl_data
)
964 struct ospf_interface
*oi
;
966 if ((oi
= vl_data
->vl_oi
) == NULL
)
969 oi
->address
->u
.prefix4
.s_addr
= 0;
970 oi
->address
->prefixlen
= 0;
972 UNSET_FLAG(oi
->ifp
->flags
, IFF_UP
);
973 /* OSPF_ISM_EVENT_SCHEDULE (oi, ISM_InterfaceDown); */
974 OSPF_ISM_EVENT_EXECUTE(oi
, ISM_InterfaceDown
);
977 void ospf_vl_add(struct ospf
*ospf
, struct ospf_vl_data
*vl_data
)
979 listnode_add(ospf
->vlinks
, vl_data
);
980 hook_call(ospf_vl_add
, vl_data
);
983 void ospf_vl_delete(struct ospf
*ospf
, struct ospf_vl_data
*vl_data
)
985 ospf_vl_shutdown(vl_data
);
986 ospf_vl_if_delete(vl_data
);
988 hook_call(ospf_vl_delete
, vl_data
);
989 listnode_delete(ospf
->vlinks
, vl_data
);
991 ospf_vl_data_free(vl_data
);
994 static int ospf_vl_set_params(struct ospf_vl_data
*vl_data
, struct vertex
*v
)
997 struct ospf_interface
*voi
;
998 struct listnode
*node
;
999 struct vertex_parent
*vp
= NULL
;
1001 struct router_lsa
*rl
;
1003 voi
= vl_data
->vl_oi
;
1005 if (voi
->output_cost
!= v
->distance
) {
1007 voi
->output_cost
= v
->distance
;
1011 for (ALL_LIST_ELEMENTS_RO(v
->parents
, node
, vp
)) {
1012 vl_data
->nexthop
.oi
= vp
->nexthop
->oi
;
1013 vl_data
->nexthop
.router
= vp
->nexthop
->router
;
1015 if (!IPV4_ADDR_SAME(&voi
->address
->u
.prefix4
,
1016 &vl_data
->nexthop
.oi
->address
->u
.prefix4
))
1019 voi
->address
->u
.prefix4
=
1020 vl_data
->nexthop
.oi
->address
->u
.prefix4
;
1021 voi
->address
->prefixlen
=
1022 vl_data
->nexthop
.oi
->address
->prefixlen
;
1024 break; /* We take the first interface. */
1027 rl
= (struct router_lsa
*)v
->lsa
;
1029 /* use SPF determined backlink index in struct vertex
1030 * for virtual link destination address
1032 if (vp
&& vp
->backlink
>= 0) {
1033 if (!IPV4_ADDR_SAME(&vl_data
->peer_addr
,
1034 &rl
->link
[vp
->backlink
].link_data
))
1036 vl_data
->peer_addr
= rl
->link
[vp
->backlink
].link_data
;
1038 /* This is highly odd, there is no backlink index
1039 * there should be due to the ospf_spf_has_link() check
1040 * in SPF. Lets warn and try pick a link anyway.
1042 zlog_info("ospf_vl_set_params: No backlink for %s!",
1043 vl_data
->vl_oi
->ifp
->name
);
1044 for (i
= 0; i
< ntohs(rl
->links
); i
++) {
1045 switch (rl
->link
[i
].type
) {
1046 case LSA_LINK_TYPE_VIRTUALLINK
:
1047 if (IS_DEBUG_OSPF_EVENT
)
1049 "found back link through VL");
1051 case LSA_LINK_TYPE_TRANSIT
:
1052 case LSA_LINK_TYPE_POINTOPOINT
:
1053 if (!IPV4_ADDR_SAME(&vl_data
->peer_addr
,
1054 &rl
->link
[i
].link_data
))
1056 vl_data
->peer_addr
= rl
->link
[i
].link_data
;
1061 if (IS_DEBUG_OSPF_EVENT
)
1062 zlog_debug("%s: %s peer address: %s, cost: %d,%schanged",
1063 __func__
, vl_data
->vl_oi
->ifp
->name
,
1064 inet_ntoa(vl_data
->peer_addr
), voi
->output_cost
,
1065 (changed
? " " : " un"));
1071 void ospf_vl_up_check(struct ospf_area
*area
, struct in_addr rid
,
1074 struct ospf
*ospf
= area
->ospf
;
1075 struct listnode
*node
;
1076 struct ospf_vl_data
*vl_data
;
1077 struct ospf_interface
*oi
;
1079 if (IS_DEBUG_OSPF_EVENT
) {
1080 zlog_debug("ospf_vl_up_check(): Start");
1081 zlog_debug("ospf_vl_up_check(): Router ID is %s",
1083 zlog_debug("ospf_vl_up_check(): Area is %s",
1084 inet_ntoa(area
->area_id
));
1087 for (ALL_LIST_ELEMENTS_RO(ospf
->vlinks
, node
, vl_data
)) {
1088 if (IS_DEBUG_OSPF_EVENT
) {
1089 zlog_debug("%s: considering VL, %s in area %s",
1090 __func__
, vl_data
->vl_oi
->ifp
->name
,
1091 inet_ntoa(vl_data
->vl_area_id
));
1092 zlog_debug("%s: peer ID: %s", __func__
,
1093 inet_ntoa(vl_data
->vl_peer
));
1096 if (IPV4_ADDR_SAME(&vl_data
->vl_peer
, &rid
)
1097 && IPV4_ADDR_SAME(&vl_data
->vl_area_id
, &area
->area_id
)) {
1098 oi
= vl_data
->vl_oi
;
1099 SET_FLAG(vl_data
->flags
, OSPF_VL_FLAG_APPROVED
);
1101 if (IS_DEBUG_OSPF_EVENT
)
1103 "ospf_vl_up_check(): this VL matched");
1105 if (oi
->state
== ISM_Down
) {
1106 if (IS_DEBUG_OSPF_EVENT
)
1108 "ospf_vl_up_check(): VL is down, waking it up");
1109 SET_FLAG(oi
->ifp
->flags
, IFF_UP
);
1110 OSPF_ISM_EVENT_EXECUTE(oi
, ISM_InterfaceUp
);
1113 if (ospf_vl_set_params(vl_data
, v
)) {
1114 if (IS_DEBUG_OSPF(ism
, ISM_EVENTS
))
1116 "ospf_vl_up_check: VL cost change,"
1117 " scheduling router lsa refresh");
1119 ospf_router_lsa_update_area(
1121 else if (IS_DEBUG_OSPF(ism
, ISM_EVENTS
))
1123 "ospf_vl_up_check: VL cost change, no backbone!");
1129 void ospf_vl_unapprove(struct ospf
*ospf
)
1131 struct listnode
*node
;
1132 struct ospf_vl_data
*vl_data
;
1134 for (ALL_LIST_ELEMENTS_RO(ospf
->vlinks
, node
, vl_data
))
1135 UNSET_FLAG(vl_data
->flags
, OSPF_VL_FLAG_APPROVED
);
1138 void ospf_vl_shut_unapproved(struct ospf
*ospf
)
1140 struct listnode
*node
, *nnode
;
1141 struct ospf_vl_data
*vl_data
;
1143 for (ALL_LIST_ELEMENTS(ospf
->vlinks
, node
, nnode
, vl_data
))
1144 if (!CHECK_FLAG(vl_data
->flags
, OSPF_VL_FLAG_APPROVED
))
1145 ospf_vl_shutdown(vl_data
);
1148 int ospf_full_virtual_nbrs(struct ospf_area
*area
)
1150 if (IS_DEBUG_OSPF_EVENT
) {
1152 "counting fully adjacent virtual neighbors in area %s",
1153 inet_ntoa(area
->area_id
));
1154 zlog_debug("there are %d of them", area
->full_vls
);
1157 return area
->full_vls
;
1160 int ospf_vls_in_area(struct ospf_area
*area
)
1162 struct listnode
*node
;
1163 struct ospf_vl_data
*vl_data
;
1166 for (ALL_LIST_ELEMENTS_RO(area
->ospf
->vlinks
, node
, vl_data
))
1167 if (IPV4_ADDR_SAME(&vl_data
->vl_area_id
, &area
->area_id
))
1174 struct crypt_key
*ospf_crypt_key_new(void)
1176 return XCALLOC(MTYPE_OSPF_CRYPT_KEY
, sizeof(struct crypt_key
));
1179 void ospf_crypt_key_add(struct list
*crypt
, struct crypt_key
*ck
)
1181 listnode_add(crypt
, ck
);
1184 struct crypt_key
*ospf_crypt_key_lookup(struct list
*auth_crypt
, uint8_t key_id
)
1186 struct listnode
*node
;
1187 struct crypt_key
*ck
;
1189 for (ALL_LIST_ELEMENTS_RO(auth_crypt
, node
, ck
))
1190 if (ck
->key_id
== key_id
)
1196 int ospf_crypt_key_delete(struct list
*auth_crypt
, uint8_t key_id
)
1198 struct listnode
*node
, *nnode
;
1199 struct crypt_key
*ck
;
1201 for (ALL_LIST_ELEMENTS(auth_crypt
, node
, nnode
, ck
)) {
1202 if (ck
->key_id
== key_id
) {
1203 listnode_delete(auth_crypt
, ck
);
1204 XFREE(MTYPE_OSPF_CRYPT_KEY
, ck
);
1212 uint8_t ospf_default_iftype(struct interface
*ifp
)
1214 if (if_is_pointopoint(ifp
))
1215 return OSPF_IFTYPE_POINTOPOINT
;
1216 else if (if_is_loopback(ifp
) || if_is_vrf(ifp
))
1217 return OSPF_IFTYPE_LOOPBACK
;
1219 return OSPF_IFTYPE_BROADCAST
;
1222 void ospf_if_interface(struct interface
*ifp
)
1224 hook_call(ospf_if_update
, ifp
);
1227 static int ospf_ifp_create(struct interface
*ifp
)
1229 struct ospf
*ospf
= NULL
;
1231 if (IS_DEBUG_OSPF(zebra
, ZEBRA_INTERFACE
))
1233 "Zebra: interface add %s vrf %s[%u] index %d flags %llx metric %d mtu %d speed %u",
1234 ifp
->name
, ospf_vrf_id_to_name(ifp
->vrf_id
),
1235 ifp
->vrf_id
, ifp
->ifindex
,
1236 (unsigned long long)ifp
->flags
, ifp
->metric
, ifp
->mtu
,
1241 if (IF_DEF_PARAMS(ifp
)
1242 && !OSPF_IF_PARAM_CONFIGURED(IF_DEF_PARAMS(ifp
), type
)) {
1243 SET_IF_PARAM(IF_DEF_PARAMS(ifp
), type
);
1244 IF_DEF_PARAMS(ifp
)->type
= ospf_default_iftype(ifp
);
1247 ospf
= ospf_lookup_by_vrf_id(ifp
->vrf_id
);
1251 ospf_if_recalculate_output_cost(ifp
);
1253 ospf_if_update(ospf
, ifp
);
1255 hook_call(ospf_if_update
, ifp
);
1260 static int ospf_ifp_up(struct interface
*ifp
)
1262 struct ospf_interface
*oi
;
1263 struct route_node
*rn
;
1265 /* Interface is already up. */
1266 if (if_is_operative(ifp
)) {
1267 /* Temporarily keep ifp values. */
1268 struct interface if_tmp
;
1269 memcpy(&if_tmp
, ifp
, sizeof(struct interface
));
1271 if (IS_DEBUG_OSPF(zebra
, ZEBRA_INTERFACE
))
1273 "Zebra: Interface[%s] state update speed %u -> %u, bw %d -> %d",
1274 ifp
->name
, if_tmp
.speed
, ifp
->speed
,
1275 if_tmp
.bandwidth
, ifp
->bandwidth
);
1277 ospf_if_recalculate_output_cost(ifp
);
1279 if (if_tmp
.mtu
!= ifp
->mtu
) {
1280 if (IS_DEBUG_OSPF(zebra
, ZEBRA_INTERFACE
))
1282 "Zebra: Interface[%s] MTU change %u -> %u.",
1283 ifp
->name
, if_tmp
.mtu
, ifp
->mtu
);
1285 /* Must reset the interface (simulate down/up) when MTU
1292 if (IS_DEBUG_OSPF(zebra
, ZEBRA_INTERFACE
))
1293 zlog_debug("Zebra: Interface[%s] state change to up.",
1296 for (rn
= route_top(IF_OIFS(ifp
)); rn
; rn
= route_next(rn
)) {
1297 if ((oi
= rn
->info
) == NULL
)
1306 static int ospf_ifp_down(struct interface
*ifp
)
1308 struct ospf_interface
*oi
;
1309 struct route_node
*node
;
1311 if (IS_DEBUG_OSPF(zebra
, ZEBRA_INTERFACE
))
1312 zlog_debug("Zebra: Interface[%s] state change to down.",
1315 for (node
= route_top(IF_OIFS(ifp
)); node
; node
= route_next(node
)) {
1316 if ((oi
= node
->info
) == NULL
)
1324 static int ospf_ifp_destroy(struct interface
*ifp
)
1326 struct route_node
*rn
;
1328 if (IS_DEBUG_OSPF(zebra
, ZEBRA_INTERFACE
))
1330 "Zebra: interface delete %s vrf %s[%u] index %d flags %llx metric %d mtu %d",
1331 ifp
->name
, ospf_vrf_id_to_name(ifp
->vrf_id
),
1332 ifp
->vrf_id
, ifp
->ifindex
,
1333 (unsigned long long)ifp
->flags
, ifp
->metric
, ifp
->mtu
);
1335 hook_call(ospf_if_delete
, ifp
);
1337 for (rn
= route_top(IF_OIFS(ifp
)); rn
; rn
= route_next(rn
))
1339 ospf_if_free((struct ospf_interface
*)rn
->info
);
1344 void ospf_if_init(void)
1346 if_zapi_callbacks(ospf_ifp_create
, ospf_ifp_up
,
1347 ospf_ifp_down
, ospf_ifp_destroy
);
1349 /* Initialize Zebra interface data structure. */
1350 hook_register_prio(if_add
, 0, ospf_if_new_hook
);
1351 hook_register_prio(if_del
, 0, ospf_if_delete_hook
);