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
))
55 ospf_if_get_output_cost (struct ospf_interface
*oi
)
57 /* If all else fails, use default OSPF cost */
61 bw
= oi
->ifp
->bandwidth
? oi
->ifp
->bandwidth
: OSPF_DEFAULT_BANDWIDTH
;
62 refbw
= oi
->ospf
->ref_bandwidth
;
64 /* A specifed ip ospf cost overrides a calculated one. */
65 if (OSPF_IF_PARAM_CONFIGURED (IF_DEF_PARAMS (oi
->ifp
), output_cost_cmd
) ||
66 OSPF_IF_PARAM_CONFIGURED (oi
->params
, output_cost_cmd
))
67 cost
= OSPF_IF_PARAM (oi
, output_cost_cmd
);
68 /* See if a cost can be calculated from the zebra processes
69 interface bandwidth field. */
72 cost
= (u_int32_t
) ((double)refbw
/ (double)bw
+ (double)0.5);
75 else if (cost
> 65535)
83 ospf_if_recalculate_output_cost (struct interface
*ifp
)
86 struct route_node
*rn
;
88 for (rn
= route_top (IF_OIFS (ifp
)); rn
; rn
= route_next (rn
))
90 struct ospf_interface
*oi
;
92 if ( (oi
= rn
->info
) == NULL
)
95 newcost
= ospf_if_get_output_cost (oi
);
97 /* Is actual output cost changed? */
98 if (oi
->output_cost
!= newcost
)
100 oi
->output_cost
= newcost
;
101 ospf_router_lsa_update_area (oi
->area
);
106 /* Simulate down/up on the interface. This is needed, for example, when
109 ospf_if_reset(struct interface
*ifp
)
111 struct route_node
*rn
;
113 for (rn
= route_top (IF_OIFS (ifp
)); rn
; rn
= route_next (rn
))
115 struct ospf_interface
*oi
;
117 if ( (oi
= rn
->info
) == NULL
)
126 ospf_if_reset_variables (struct ospf_interface
*oi
)
128 /* Set default values. */
129 /* don't clear this flag. oi->flag = OSPF_IF_DISABLE; */
132 oi
->type
= OSPF_IFTYPE_VIRTUALLINK
;
134 /* preserve network-type */
135 if (oi
->type
!= OSPF_IFTYPE_NBMA
)
136 oi
->type
= OSPF_IFTYPE_BROADCAST
;
138 oi
->state
= ISM_Down
;
140 oi
->crypt_seqnum
= 0;
142 /* This must be short, (less than RxmtInterval)
143 - RFC 2328 Section 13.5 para 3. Set to 1 second to avoid Acks being
144 held back for too long - MAG */
148 /* lookup oi for specified prefix/ifp */
149 struct ospf_interface
*
150 ospf_if_table_lookup (struct interface
*ifp
, struct prefix
*prefix
)
153 struct route_node
*rn
;
154 struct ospf_interface
*rninfo
= NULL
;
157 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
159 /* route_node_get implicitely locks */
160 if ((rn
= route_node_lookup (IF_OIFS (ifp
), &p
)))
162 rninfo
= (struct ospf_interface
*) rn
->info
;
163 route_unlock_node (rn
);
170 ospf_add_to_if (struct interface
*ifp
, struct ospf_interface
*oi
)
172 struct route_node
*rn
;
176 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
178 rn
= route_node_get (IF_OIFS (ifp
), &p
);
179 /* rn->info should either be NULL or equal to this oi
180 * as route_node_get may return an existing node
182 assert (!rn
->info
|| rn
->info
== oi
);
187 ospf_delete_from_if (struct interface
*ifp
, struct ospf_interface
*oi
)
189 struct route_node
*rn
;
193 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
195 rn
= route_node_lookup (IF_OIFS (oi
->ifp
), &p
);
199 route_unlock_node (rn
);
200 route_unlock_node (rn
);
203 struct ospf_interface
*
204 ospf_if_new (struct ospf
*ospf
, struct interface
*ifp
, struct prefix
*p
)
206 struct ospf_interface
*oi
;
208 if ((oi
= ospf_if_table_lookup (ifp
, p
)) == NULL
)
210 oi
= XCALLOC (MTYPE_OSPF_IF
, sizeof (struct ospf_interface
));
211 memset (oi
, 0, sizeof (struct ospf_interface
));
216 /* Set zebra interface pointer. */
220 ospf_add_to_if (ifp
, oi
);
221 listnode_add (ospf
->oiflist
, oi
);
223 /* Initialize neighbor list. */
224 oi
->nbrs
= route_table_init ();
226 /* Initialize static neighbor list. */
227 oi
->nbr_nbma
= list_new ();
229 /* Initialize Link State Acknowledgment list. */
230 oi
->ls_ack
= list_new ();
231 oi
->ls_ack_direct
.ls_ack
= list_new ();
233 /* Set default values. */
234 ospf_if_reset_variables (oi
);
236 /* Set pseudo neighbor to Null */
239 oi
->ls_upd_queue
= route_table_init ();
240 oi
->t_ls_upd_event
= NULL
;
241 oi
->t_ls_ack_direct
= NULL
;
243 oi
->crypt_seqnum
= time (NULL
);
245 ospf_opaque_type9_lsa_init (oi
);
248 QOBJ_REG (oi
, ospf_interface
);
253 /* Restore an interface to its pre UP state
254 Used from ism_interface_down only */
256 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
))
268 OSPF_POLL_TIMER_OFF (nbr_nbma
->t_poll
);
272 nbr_nbma
->nbr
->nbr_nbma
= NULL
;
273 nbr_nbma
->nbr
= NULL
;
278 listnode_delete (oi
->nbr_nbma
, nbr_nbma
);
281 /* send Neighbor event KillNbr to all associated neighbors. */
282 for (rn
= route_top (oi
->nbrs
); rn
; rn
= route_next (rn
))
283 if ((nbr
= rn
->info
) != NULL
)
284 if (nbr
!= oi
->nbr_self
)
285 OSPF_NSM_EVENT_EXECUTE (nbr
, NSM_KillNbr
);
287 /* Cleanup Link State Acknowlegdment list. */
288 for (ALL_LIST_ELEMENTS (oi
->ls_ack
, node
, nnode
, lsa
))
289 ospf_lsa_unlock (&lsa
); /* oi->ls_ack */
290 list_delete_all_node (oi
->ls_ack
);
292 oi
->crypt_seqnum
= 0;
294 /* Empty link state update queue */
295 ospf_ls_upd_queue_empty (oi
);
297 /* Reset pseudo neighbor. */
298 ospf_nbr_self_reset (oi
, oi
->ospf
->router_id
);
302 ospf_if_free (struct ospf_interface
*oi
)
306 assert (oi
->state
== ISM_Down
);
308 ospf_opaque_type9_lsa_term (oi
);
312 /* Free Pseudo Neighbour */
313 ospf_nbr_delete (oi
->nbr_self
);
315 route_table_finish (oi
->nbrs
);
316 route_table_finish (oi
->ls_upd_queue
);
318 /* Free any lists that should be freed */
319 list_free (oi
->nbr_nbma
);
321 list_free (oi
->ls_ack
);
322 list_free (oi
->ls_ack_direct
.ls_ack
);
324 ospf_delete_from_if (oi
->ifp
, oi
);
326 listnode_delete (oi
->ospf
->oiflist
, oi
);
327 listnode_delete (oi
->area
->oiflist
, oi
);
329 thread_cancel_event (master
, oi
);
331 memset (oi
, 0, sizeof (*oi
));
332 XFREE (MTYPE_OSPF_IF
, oi
);
336 ospf_if_is_up (struct ospf_interface
*oi
)
338 return if_is_up (oi
->ifp
);
341 struct ospf_interface
*
342 ospf_if_exists (struct ospf_interface
*oic
)
344 struct listnode
*node
;
346 struct ospf_interface
*oi
;
348 if ((ospf
= ospf_lookup ()) == NULL
)
351 for (ALL_LIST_ELEMENTS_RO (ospf
->oiflist
, node
, oi
))
358 /* Lookup OSPF interface by router LSA posistion */
359 struct ospf_interface
*
360 ospf_if_lookup_by_lsa_pos (struct ospf_area
*area
, int lsa_pos
)
362 struct listnode
*node
;
363 struct ospf_interface
*oi
;
365 for (ALL_LIST_ELEMENTS_RO (area
->oiflist
, node
, oi
))
367 if (lsa_pos
>= oi
->lsa_pos_beg
&& lsa_pos
< oi
->lsa_pos_end
)
373 struct ospf_interface
*
374 ospf_if_lookup_by_local_addr (struct ospf
*ospf
,
375 struct interface
*ifp
, struct in_addr address
)
377 struct listnode
*node
;
378 struct ospf_interface
*oi
;
380 for (ALL_LIST_ELEMENTS_RO (ospf
->oiflist
, node
, oi
))
381 if (oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
)
383 if (ifp
&& oi
->ifp
!= ifp
)
386 if (IPV4_ADDR_SAME (&address
, &oi
->address
->u
.prefix4
))
393 struct ospf_interface
*
394 ospf_if_lookup_by_prefix (struct ospf
*ospf
, struct prefix_ipv4
*p
)
396 struct listnode
*node
;
397 struct ospf_interface
*oi
;
399 /* Check each Interface. */
400 for (ALL_LIST_ELEMENTS_RO (ospf
->oiflist
, node
, oi
))
402 if (oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
)
406 prefix_copy (&ptmp
, CONNECTED_PREFIX(oi
->connected
));
408 if (prefix_same (&ptmp
, (struct prefix
*) p
))
415 /* determine receiving interface by ifp and source address */
416 struct ospf_interface
*
417 ospf_if_lookup_recv_if (struct ospf
*ospf
, struct in_addr src
,
418 struct interface
*ifp
)
420 struct route_node
*rn
;
421 struct prefix_ipv4 addr
;
422 struct ospf_interface
*oi
, *match
;
424 addr
.family
= AF_INET
;
426 addr
.prefixlen
= IPV4_MAX_BITLEN
;
430 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
))
448 if ( (match
== NULL
) ||
449 (match
->address
->prefixlen
< oi
->address
->prefixlen
)
459 ospf_if_stream_set (struct ospf_interface
*oi
)
461 /* set output fifo queue. */
462 if (oi
->obuf
== NULL
)
463 oi
->obuf
= ospf_fifo_new ();
467 ospf_if_stream_unset (struct ospf_interface
*oi
)
469 struct ospf
*ospf
= oi
->ospf
;
473 ospf_fifo_free (oi
->obuf
);
478 listnode_delete (ospf
->oi_write_q
, oi
);
479 if (list_isempty(ospf
->oi_write_q
))
480 OSPF_TIMER_OFF (ospf
->t_write
);
487 static struct ospf_if_params
*
488 ospf_new_if_params (void)
490 struct ospf_if_params
*oip
;
492 oip
= XCALLOC (MTYPE_OSPF_IF_PARAMS
, sizeof (struct ospf_if_params
));
497 UNSET_IF_PARAM (oip
, output_cost_cmd
);
498 UNSET_IF_PARAM (oip
, transmit_delay
);
499 UNSET_IF_PARAM (oip
, retransmit_interval
);
500 UNSET_IF_PARAM (oip
, passive_interface
);
501 UNSET_IF_PARAM (oip
, v_hello
);
502 UNSET_IF_PARAM (oip
, fast_hello
);
503 UNSET_IF_PARAM (oip
, v_wait
);
504 UNSET_IF_PARAM (oip
, priority
);
505 UNSET_IF_PARAM (oip
, type
);
506 UNSET_IF_PARAM (oip
, auth_simple
);
507 UNSET_IF_PARAM (oip
, auth_crypt
);
508 UNSET_IF_PARAM (oip
, auth_type
);
510 oip
->auth_crypt
= list_new ();
512 oip
->network_lsa_seqnum
= htonl(OSPF_INITIAL_SEQUENCE_NUMBER
);
518 ospf_del_if_params (struct ospf_if_params
*oip
)
520 list_delete (oip
->auth_crypt
);
521 bfd_info_free(&(oip
->bfd_info
));
522 XFREE (MTYPE_OSPF_IF_PARAMS
, oip
);
526 ospf_free_if_params (struct interface
*ifp
, struct in_addr addr
)
528 struct ospf_if_params
*oip
;
529 struct prefix_ipv4 p
;
530 struct route_node
*rn
;
533 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
535 rn
= route_node_lookup (IF_OIFS_PARAMS (ifp
), (struct prefix
*)&p
);
536 if (!rn
|| !rn
->info
)
540 route_unlock_node (rn
);
542 if (!OSPF_IF_PARAM_CONFIGURED (oip
, output_cost_cmd
) &&
543 !OSPF_IF_PARAM_CONFIGURED (oip
, transmit_delay
) &&
544 !OSPF_IF_PARAM_CONFIGURED (oip
, retransmit_interval
) &&
545 !OSPF_IF_PARAM_CONFIGURED (oip
, passive_interface
) &&
546 !OSPF_IF_PARAM_CONFIGURED (oip
, v_hello
) &&
547 !OSPF_IF_PARAM_CONFIGURED (oip
, fast_hello
) &&
548 !OSPF_IF_PARAM_CONFIGURED (oip
, v_wait
) &&
549 !OSPF_IF_PARAM_CONFIGURED (oip
, priority
) &&
550 !OSPF_IF_PARAM_CONFIGURED (oip
, type
) &&
551 !OSPF_IF_PARAM_CONFIGURED (oip
, auth_simple
) &&
552 !OSPF_IF_PARAM_CONFIGURED (oip
, auth_type
) &&
553 listcount (oip
->auth_crypt
) == 0 &&
554 ntohl (oip
->network_lsa_seqnum
) != OSPF_INITIAL_SEQUENCE_NUMBER
)
556 ospf_del_if_params (oip
);
558 route_unlock_node (rn
);
562 struct ospf_if_params
*
563 ospf_lookup_if_params (struct interface
*ifp
, struct in_addr addr
)
565 struct prefix_ipv4 p
;
566 struct route_node
*rn
;
569 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
572 rn
= route_node_lookup (IF_OIFS_PARAMS (ifp
), (struct prefix
*)&p
);
576 route_unlock_node (rn
);
583 struct ospf_if_params
*
584 ospf_get_if_params (struct interface
*ifp
, struct in_addr addr
)
586 struct prefix_ipv4 p
;
587 struct route_node
*rn
;
590 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
593 rn
= route_node_get (IF_OIFS_PARAMS (ifp
), (struct prefix
*)&p
);
595 if (rn
->info
== NULL
)
596 rn
->info
= ospf_new_if_params ();
598 route_unlock_node (rn
);
604 ospf_if_update_params (struct interface
*ifp
, struct in_addr addr
)
606 struct route_node
*rn
;
607 struct ospf_interface
*oi
;
609 for (rn
= route_top (IF_OIFS (ifp
)); rn
; rn
= route_next (rn
))
611 if ((oi
= rn
->info
) == NULL
)
614 if (IPV4_ADDR_SAME (&oi
->address
->u
.prefix4
, &addr
))
615 oi
->params
= ospf_lookup_if_params (ifp
, oi
->address
->u
.prefix4
);
620 ospf_if_new_hook (struct interface
*ifp
)
624 ifp
->info
= XCALLOC (MTYPE_OSPF_IF_INFO
, sizeof (struct ospf_if_info
));
626 IF_OIFS (ifp
) = route_table_init ();
627 IF_OIFS_PARAMS (ifp
) = route_table_init ();
629 IF_DEF_PARAMS (ifp
) = ospf_new_if_params ();
631 SET_IF_PARAM (IF_DEF_PARAMS (ifp
), transmit_delay
);
632 IF_DEF_PARAMS (ifp
)->transmit_delay
= OSPF_TRANSMIT_DELAY_DEFAULT
;
634 SET_IF_PARAM (IF_DEF_PARAMS (ifp
), retransmit_interval
);
635 IF_DEF_PARAMS (ifp
)->retransmit_interval
= OSPF_RETRANSMIT_INTERVAL_DEFAULT
;
637 SET_IF_PARAM (IF_DEF_PARAMS (ifp
), priority
);
638 IF_DEF_PARAMS (ifp
)->priority
= OSPF_ROUTER_PRIORITY_DEFAULT
;
640 IF_DEF_PARAMS (ifp
)->mtu_ignore
= OSPF_MTU_IGNORE_DEFAULT
;
642 SET_IF_PARAM (IF_DEF_PARAMS (ifp
), v_hello
);
643 IF_DEF_PARAMS (ifp
)->v_hello
= OSPF_HELLO_INTERVAL_DEFAULT
;
645 SET_IF_PARAM (IF_DEF_PARAMS (ifp
), fast_hello
);
646 IF_DEF_PARAMS (ifp
)->fast_hello
= OSPF_FAST_HELLO_DEFAULT
;
648 SET_IF_PARAM (IF_DEF_PARAMS (ifp
), v_wait
);
649 IF_DEF_PARAMS (ifp
)->v_wait
= OSPF_ROUTER_DEAD_INTERVAL_DEFAULT
;
651 SET_IF_PARAM (IF_DEF_PARAMS (ifp
), auth_simple
);
652 memset (IF_DEF_PARAMS (ifp
)->auth_simple
, 0, OSPF_AUTH_SIMPLE_SIZE
);
654 SET_IF_PARAM (IF_DEF_PARAMS (ifp
), auth_type
);
655 IF_DEF_PARAMS (ifp
)->auth_type
= OSPF_AUTH_NOTSET
;
657 rc
= ospf_opaque_new_if (ifp
);
662 ospf_if_delete_hook (struct interface
*ifp
)
665 struct route_node
*rn
;
666 rc
= ospf_opaque_del_if (ifp
);
668 route_table_finish (IF_OIFS (ifp
));
670 for (rn
= route_top (IF_OIFS_PARAMS (ifp
)); rn
; rn
= route_next (rn
))
672 ospf_del_if_params (rn
->info
);
673 route_table_finish (IF_OIFS_PARAMS (ifp
));
675 ospf_del_if_params ((struct ospf_if_params
*) IF_DEF_PARAMS (ifp
));
676 XFREE (MTYPE_OSPF_IF_INFO
, ifp
->info
);
683 ospf_if_is_enable (struct ospf_interface
*oi
)
685 if (!if_is_loopback (oi
->ifp
))
686 if (if_is_up (oi
->ifp
))
693 ospf_if_set_multicast(struct ospf_interface
*oi
)
695 if ((oi
->state
> ISM_Loopback
) &&
696 (oi
->type
!= OSPF_IFTYPE_LOOPBACK
) &&
697 (oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
) &&
698 (OSPF_IF_PASSIVE_STATUS(oi
) == OSPF_IF_ACTIVE
))
700 /* The interface should belong to the OSPF-all-routers group. */
701 if (!OI_MEMBER_CHECK(oi
, MEMBER_ALLROUTERS
) &&
702 (ospf_if_add_allspfrouters(oi
->ospf
, oi
->address
,
703 oi
->ifp
->ifindex
) >= 0))
704 /* Set the flag only if the system call to join succeeded. */
705 OI_MEMBER_JOINED(oi
, MEMBER_ALLROUTERS
);
709 /* The interface should NOT belong to the OSPF-all-routers group. */
710 if (OI_MEMBER_CHECK(oi
, MEMBER_ALLROUTERS
))
712 /* Only actually drop if this is the last reference */
713 if (OI_MEMBER_COUNT(oi
, MEMBER_ALLROUTERS
) == 1)
714 ospf_if_drop_allspfrouters (oi
->ospf
, oi
->address
,
716 /* Unset the flag regardless of whether the system call to leave
717 the group succeeded, since it's much safer to assume that
718 we are not a member. */
719 OI_MEMBER_LEFT(oi
,MEMBER_ALLROUTERS
);
723 if (((oi
->type
== OSPF_IFTYPE_BROADCAST
) ||
724 (oi
->type
== OSPF_IFTYPE_POINTOPOINT
)) &&
725 ((oi
->state
== ISM_DR
) || (oi
->state
== ISM_Backup
)) &&
726 (OSPF_IF_PASSIVE_STATUS(oi
) == OSPF_IF_ACTIVE
))
728 /* The interface should belong to the OSPF-designated-routers group. */
729 if (!OI_MEMBER_CHECK(oi
, MEMBER_DROUTERS
) &&
730 (ospf_if_add_alldrouters(oi
->ospf
, oi
->address
,
731 oi
->ifp
->ifindex
) >= 0))
732 /* Set the flag only if the system call to join succeeded. */
733 OI_MEMBER_JOINED(oi
, MEMBER_DROUTERS
);
737 /* The interface should NOT belong to the OSPF-designated-routers group */
738 if (OI_MEMBER_CHECK(oi
, MEMBER_DROUTERS
))
740 /* drop only if last reference */
741 if (OI_MEMBER_COUNT(oi
, MEMBER_DROUTERS
) == 1)
742 ospf_if_drop_alldrouters(oi
->ospf
, oi
->address
, oi
->ifp
->ifindex
);
744 /* Unset the flag regardless of whether the system call to leave
745 the group succeeded, since it's much safer to assume that
746 we are not a member. */
747 OI_MEMBER_LEFT(oi
, MEMBER_DROUTERS
);
753 ospf_if_up (struct ospf_interface
*oi
)
758 if (oi
->type
== OSPF_IFTYPE_LOOPBACK
)
759 OSPF_ISM_EVENT_SCHEDULE (oi
, ISM_LoopInd
);
762 ospf_if_stream_set (oi
);
763 OSPF_ISM_EVENT_SCHEDULE (oi
, ISM_InterfaceUp
);
770 ospf_if_down (struct ospf_interface
*oi
)
775 OSPF_ISM_EVENT_EXECUTE (oi
, ISM_InterfaceDown
);
776 /* delete position in router LSA */
779 /* Shutdown packet reception and sending */
780 ospf_if_stream_unset (oi
);
786 /* Virtual Link related functions. */
788 struct ospf_vl_data
*
789 ospf_vl_data_new (struct ospf_area
*area
, struct in_addr vl_peer
)
791 struct ospf_vl_data
*vl_data
;
793 vl_data
= XCALLOC (MTYPE_OSPF_VL_DATA
, sizeof (struct ospf_vl_data
));
795 vl_data
->vl_peer
.s_addr
= vl_peer
.s_addr
;
796 vl_data
->vl_area_id
= area
->area_id
;
797 vl_data
->format
= area
->format
;
803 ospf_vl_data_free (struct ospf_vl_data
*vl_data
)
805 XFREE (MTYPE_OSPF_VL_DATA
, vl_data
);
808 u_int vlink_count
= 0;
810 struct ospf_interface
*
811 ospf_vl_new (struct ospf
*ospf
, struct ospf_vl_data
*vl_data
)
813 struct ospf_interface
* voi
;
814 struct interface
* vi
;
815 char ifname
[INTERFACE_NAMSIZ
+ 1];
816 struct ospf_area
*area
;
817 struct in_addr area_id
;
818 struct connected
*co
;
819 struct prefix_ipv4
*p
;
821 if (IS_DEBUG_OSPF_EVENT
)
822 zlog_debug ("ospf_vl_new(): Start");
823 if (vlink_count
== OSPF_VL_MAX_COUNT
)
825 if (IS_DEBUG_OSPF_EVENT
)
826 zlog_debug ("ospf_vl_new(): Alarm: "
827 "cannot create more than OSPF_MAX_VL_COUNT virtual links");
831 if (IS_DEBUG_OSPF_EVENT
)
832 zlog_debug ("ospf_vl_new(): creating pseudo zebra interface");
834 snprintf (ifname
, sizeof(ifname
), "VLINK%d", vlink_count
);
835 vi
= if_create (ifname
, strnlen(ifname
, sizeof(ifname
)), VRF_DEFAULT
);
837 * if_create sets ZEBRA_INTERFACE_LINKDETECTION
838 * virtual links don't need this.
840 UNSET_FLAG (vi
->status
, ZEBRA_INTERFACE_LINKDETECTION
);
841 co
= connected_new ();
843 listnode_add (vi
->connected
, co
);
845 p
= prefix_ipv4_new ();
847 p
->prefix
.s_addr
= 0;
850 co
->address
= (struct prefix
*)p
;
852 voi
= ospf_if_new (ospf
, vi
, co
->address
);
855 if (IS_DEBUG_OSPF_EVENT
)
856 zlog_debug ("ospf_vl_new(): Alarm: OSPF int structure is not created");
860 voi
->vl_data
= vl_data
;
861 voi
->ifp
->mtu
= OSPF_VL_MTU
;
862 voi
->type
= OSPF_IFTYPE_VIRTUALLINK
;
865 if (IS_DEBUG_OSPF_EVENT
)
866 zlog_debug ("ospf_vl_new(): Created name: %s", ifname
);
867 if (IS_DEBUG_OSPF_EVENT
)
868 zlog_debug ("ospf_vl_new(): set if->name to %s", vi
->name
);
871 area
= ospf_area_get (ospf
, area_id
, OSPF_AREA_ID_FORMAT_ADDRESS
);
874 if (IS_DEBUG_OSPF_EVENT
)
875 zlog_debug ("ospf_vl_new(): set associated area to the backbone");
877 /* Add pseudo neighbor. */
878 ospf_nbr_self_reset (voi
, voi
->ospf
->router_id
);
880 ospf_area_add_if (voi
->area
, voi
);
882 ospf_if_stream_set (voi
);
884 if (IS_DEBUG_OSPF_EVENT
)
885 zlog_debug ("ospf_vl_new(): Stop");
890 ospf_vl_if_delete (struct ospf_vl_data
*vl_data
)
892 struct interface
*ifp
= vl_data
->vl_oi
->ifp
;
893 vl_data
->vl_oi
->address
->u
.prefix4
.s_addr
= 0;
894 vl_data
->vl_oi
->address
->prefixlen
= 0;
895 ospf_if_free (vl_data
->vl_oi
);
900 /* Look up vl_data for given peer, optionally qualified to be in the
901 * specified area. NULL area returns first found..
903 struct ospf_vl_data
*
904 ospf_vl_lookup (struct ospf
*ospf
, struct ospf_area
*area
,
905 struct in_addr vl_peer
)
907 struct ospf_vl_data
*vl_data
;
908 struct listnode
*node
;
910 if (IS_DEBUG_OSPF_EVENT
)
912 zlog_debug ("%s: Looking for %s", __func__
, inet_ntoa (vl_peer
));
914 zlog_debug ("%s: in area %s", __func__
, inet_ntoa (area
->area_id
));
917 for (ALL_LIST_ELEMENTS_RO (ospf
->vlinks
, node
, vl_data
))
919 if (IS_DEBUG_OSPF_EVENT
)
920 zlog_debug ("%s: VL %s, peer %s", __func__
,
921 vl_data
->vl_oi
->ifp
->name
,
922 inet_ntoa (vl_data
->vl_peer
));
924 if (area
&& !IPV4_ADDR_SAME (&vl_data
->vl_area_id
, &area
->area_id
))
927 if (IPV4_ADDR_SAME (&vl_data
->vl_peer
, &vl_peer
))
935 ospf_vl_shutdown (struct ospf_vl_data
*vl_data
)
937 struct ospf_interface
*oi
;
939 if ((oi
= vl_data
->vl_oi
) == NULL
)
942 oi
->address
->u
.prefix4
.s_addr
= 0;
943 oi
->address
->prefixlen
= 0;
945 UNSET_FLAG (oi
->ifp
->flags
, IFF_UP
);
946 /* OSPF_ISM_EVENT_SCHEDULE (oi, ISM_InterfaceDown); */
947 OSPF_ISM_EVENT_EXECUTE (oi
, ISM_InterfaceDown
);
951 ospf_vl_add (struct ospf
*ospf
, struct ospf_vl_data
*vl_data
)
953 listnode_add (ospf
->vlinks
, vl_data
);
954 hook_call(ospf_vl_add
, vl_data
);
958 ospf_vl_delete (struct ospf
*ospf
, struct ospf_vl_data
*vl_data
)
960 ospf_vl_shutdown (vl_data
);
961 ospf_vl_if_delete (vl_data
);
963 hook_call(ospf_vl_delete
, vl_data
);
964 listnode_delete (ospf
->vlinks
, vl_data
);
966 ospf_vl_data_free (vl_data
);
970 ospf_vl_set_params (struct ospf_vl_data
*vl_data
, struct vertex
*v
)
973 struct ospf_interface
*voi
;
974 struct listnode
*node
;
975 struct vertex_parent
*vp
= NULL
;
977 struct router_lsa
*rl
;
979 voi
= vl_data
->vl_oi
;
981 if (voi
->output_cost
!= v
->distance
)
984 voi
->output_cost
= v
->distance
;
988 for (ALL_LIST_ELEMENTS_RO (v
->parents
, node
, vp
))
990 vl_data
->nexthop
.oi
= vp
->nexthop
->oi
;
991 vl_data
->nexthop
.router
= vp
->nexthop
->router
;
993 if (!IPV4_ADDR_SAME(&voi
->address
->u
.prefix4
,
994 &vl_data
->nexthop
.oi
->address
->u
.prefix4
))
997 voi
->address
->u
.prefix4
= vl_data
->nexthop
.oi
->address
->u
.prefix4
;
998 voi
->address
->prefixlen
= vl_data
->nexthop
.oi
->address
->prefixlen
;
1000 break; /* We take the first interface. */
1003 rl
= (struct router_lsa
*)v
->lsa
;
1005 /* use SPF determined backlink index in struct vertex
1006 * for virtual link destination address
1008 if (vp
&& vp
->backlink
>= 0)
1010 if (!IPV4_ADDR_SAME (&vl_data
->peer_addr
,
1011 &rl
->link
[vp
->backlink
].link_data
))
1013 vl_data
->peer_addr
= rl
->link
[vp
->backlink
].link_data
;
1017 /* This is highly odd, there is no backlink index
1018 * there should be due to the ospf_spf_has_link() check
1019 * in SPF. Lets warn and try pick a link anyway.
1021 zlog_warn ("ospf_vl_set_params: No backlink for %s!",
1022 vl_data
->vl_oi
->ifp
->name
);
1023 for (i
= 0; i
< ntohs (rl
->links
); i
++)
1025 switch (rl
->link
[i
].type
)
1027 case LSA_LINK_TYPE_VIRTUALLINK
:
1028 if (IS_DEBUG_OSPF_EVENT
)
1029 zlog_debug ("found back link through VL");
1030 case LSA_LINK_TYPE_TRANSIT
:
1031 case LSA_LINK_TYPE_POINTOPOINT
:
1032 if (!IPV4_ADDR_SAME (&vl_data
->peer_addr
,
1033 &rl
->link
[i
].link_data
))
1035 vl_data
->peer_addr
= rl
->link
[i
].link_data
;
1040 if (IS_DEBUG_OSPF_EVENT
)
1041 zlog_debug ("%s: %s peer address: %s, cost: %d,%schanged", __func__
,
1042 vl_data
->vl_oi
->ifp
->name
,
1043 inet_ntoa(vl_data
->peer_addr
),
1045 (changed
? " " : " un"));
1052 ospf_vl_up_check (struct ospf_area
*area
, struct in_addr rid
,
1055 struct ospf
*ospf
= area
->ospf
;
1056 struct listnode
*node
;
1057 struct ospf_vl_data
*vl_data
;
1058 struct ospf_interface
*oi
;
1060 if (IS_DEBUG_OSPF_EVENT
)
1062 zlog_debug ("ospf_vl_up_check(): Start");
1063 zlog_debug ("ospf_vl_up_check(): Router ID is %s", inet_ntoa (rid
));
1064 zlog_debug ("ospf_vl_up_check(): Area is %s", inet_ntoa (area
->area_id
));
1067 for (ALL_LIST_ELEMENTS_RO (ospf
->vlinks
, node
, vl_data
))
1069 if (IS_DEBUG_OSPF_EVENT
)
1071 zlog_debug ("%s: considering VL, %s in area %s", __func__
,
1072 vl_data
->vl_oi
->ifp
->name
,
1073 inet_ntoa (vl_data
->vl_area_id
));
1074 zlog_debug ("%s: peer ID: %s", __func__
,
1075 inet_ntoa (vl_data
->vl_peer
));
1078 if (IPV4_ADDR_SAME (&vl_data
->vl_peer
, &rid
) &&
1079 IPV4_ADDR_SAME (&vl_data
->vl_area_id
, &area
->area_id
))
1081 oi
= vl_data
->vl_oi
;
1082 SET_FLAG (vl_data
->flags
, OSPF_VL_FLAG_APPROVED
);
1084 if (IS_DEBUG_OSPF_EVENT
)
1085 zlog_debug ("ospf_vl_up_check(): this VL matched");
1087 if (oi
->state
== ISM_Down
)
1089 if (IS_DEBUG_OSPF_EVENT
)
1090 zlog_debug ("ospf_vl_up_check(): VL is down, waking it up");
1091 SET_FLAG (oi
->ifp
->flags
, IFF_UP
);
1092 OSPF_ISM_EVENT_EXECUTE(oi
,ISM_InterfaceUp
);
1095 if (ospf_vl_set_params (vl_data
, v
))
1097 if (IS_DEBUG_OSPF (ism
, ISM_EVENTS
))
1098 zlog_debug ("ospf_vl_up_check: VL cost change,"
1099 " scheduling router lsa refresh");
1101 ospf_router_lsa_update_area (ospf
->backbone
);
1102 else if (IS_DEBUG_OSPF (ism
, ISM_EVENTS
))
1103 zlog_debug ("ospf_vl_up_check: VL cost change, no backbone!");
1110 ospf_vl_unapprove (struct ospf
*ospf
)
1112 struct listnode
*node
;
1113 struct ospf_vl_data
*vl_data
;
1115 for (ALL_LIST_ELEMENTS_RO (ospf
->vlinks
, node
, vl_data
))
1116 UNSET_FLAG (vl_data
->flags
, OSPF_VL_FLAG_APPROVED
);
1120 ospf_vl_shut_unapproved (struct ospf
*ospf
)
1122 struct listnode
*node
, *nnode
;
1123 struct ospf_vl_data
*vl_data
;
1125 for (ALL_LIST_ELEMENTS (ospf
->vlinks
, node
, nnode
, vl_data
))
1126 if (!CHECK_FLAG (vl_data
->flags
, OSPF_VL_FLAG_APPROVED
))
1127 ospf_vl_shutdown (vl_data
);
1131 ospf_full_virtual_nbrs (struct ospf_area
*area
)
1133 if (IS_DEBUG_OSPF_EVENT
)
1135 zlog_debug ("counting fully adjacent virtual neighbors in area %s",
1136 inet_ntoa (area
->area_id
));
1137 zlog_debug ("there are %d of them", area
->full_vls
);
1140 return area
->full_vls
;
1144 ospf_vls_in_area (struct ospf_area
*area
)
1146 struct listnode
*node
;
1147 struct ospf_vl_data
*vl_data
;
1150 for (ALL_LIST_ELEMENTS_RO (area
->ospf
->vlinks
, node
, vl_data
))
1151 if (IPV4_ADDR_SAME (&vl_data
->vl_area_id
, &area
->area_id
))
1159 ospf_crypt_key_new ()
1161 return XCALLOC (MTYPE_OSPF_CRYPT_KEY
, sizeof (struct crypt_key
));
1165 ospf_crypt_key_add (struct list
*crypt
, struct crypt_key
*ck
)
1167 listnode_add (crypt
, ck
);
1171 ospf_crypt_key_lookup (struct list
*auth_crypt
, u_char key_id
)
1173 struct listnode
*node
;
1174 struct crypt_key
*ck
;
1176 for (ALL_LIST_ELEMENTS_RO (auth_crypt
, node
, ck
))
1177 if (ck
->key_id
== key_id
)
1184 ospf_crypt_key_delete (struct list
*auth_crypt
, u_char key_id
)
1186 struct listnode
*node
, *nnode
;
1187 struct crypt_key
*ck
;
1189 for (ALL_LIST_ELEMENTS (auth_crypt
, node
, nnode
, ck
))
1191 if (ck
->key_id
== key_id
)
1193 listnode_delete (auth_crypt
, ck
);
1194 XFREE (MTYPE_OSPF_CRYPT_KEY
, ck
);
1203 ospf_default_iftype(struct interface
*ifp
)
1205 if (if_is_pointopoint (ifp
))
1206 return OSPF_IFTYPE_POINTOPOINT
;
1207 else if (if_is_loopback (ifp
))
1208 return OSPF_IFTYPE_LOOPBACK
;
1210 return OSPF_IFTYPE_BROADCAST
;
1216 /* Initialize Zebra interface data structure. */
1217 om
->iflist
= vrf_iflist (VRF_DEFAULT
);
1218 if_add_hook (IF_NEW_HOOK
, ospf_if_new_hook
);
1219 if_add_hook (IF_DELETE_HOOK
, ospf_if_delete_hook
);