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
18 * along with GNU Zebra; see the file COPYING. If not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
37 #include "ospfd/ospfd.h"
38 #include "ospfd/ospf_spf.h"
39 #include "ospfd/ospf_interface.h"
40 #include "ospfd/ospf_ism.h"
41 #include "ospfd/ospf_asbr.h"
42 #include "ospfd/ospf_lsa.h"
43 #include "ospfd/ospf_lsdb.h"
44 #include "ospfd/ospf_neighbor.h"
45 #include "ospfd/ospf_nsm.h"
46 #include "ospfd/ospf_packet.h"
47 #include "ospfd/ospf_abr.h"
48 #include "ospfd/ospf_network.h"
49 #include "ospfd/ospf_dump.h"
51 DEFINE_QOBJ_TYPE(ospf_interface
)
52 DEFINE_HOOK(ospf_vl_add
, (struct ospf_vl_data
*vd
), (vd
))
53 DEFINE_HOOK(ospf_vl_delete
, (struct ospf_vl_data
*vd
), (vd
))
56 ospf_if_get_output_cost (struct ospf_interface
*oi
)
58 /* If all else fails, use default OSPF cost */
62 /* ifp speed and bw can be 0 in some platforms, use ospf default bw
63 if bw is configured under interface it would be used.
65 if (!oi
->ifp
->bandwidth
&& oi
->ifp
->speed
)
68 bw
= oi
->ifp
->bandwidth
? oi
->ifp
->bandwidth
: OSPF_DEFAULT_BANDWIDTH
;
69 refbw
= oi
->ospf
->ref_bandwidth
;
71 /* A specifed ip ospf cost overrides a calculated one. */
72 if (OSPF_IF_PARAM_CONFIGURED (IF_DEF_PARAMS (oi
->ifp
), output_cost_cmd
) ||
73 OSPF_IF_PARAM_CONFIGURED (oi
->params
, output_cost_cmd
))
74 cost
= OSPF_IF_PARAM (oi
, output_cost_cmd
);
75 /* See if a cost can be calculated from the zebra processes
76 interface bandwidth field. */
79 cost
= (u_int32_t
) ((double)refbw
/ (double)bw
+ (double)0.5);
82 else if (cost
> 65535)
90 ospf_if_recalculate_output_cost (struct interface
*ifp
)
93 struct route_node
*rn
;
95 for (rn
= route_top (IF_OIFS (ifp
)); rn
; rn
= route_next (rn
))
97 struct ospf_interface
*oi
;
99 if ( (oi
= rn
->info
) == NULL
)
102 newcost
= ospf_if_get_output_cost (oi
);
104 /* Is actual output cost changed? */
105 if (oi
->output_cost
!= newcost
)
107 oi
->output_cost
= newcost
;
108 ospf_router_lsa_update_area (oi
->area
);
113 /* Simulate down/up on the interface. This is needed, for example, when
116 ospf_if_reset(struct interface
*ifp
)
118 struct route_node
*rn
;
120 for (rn
= route_top (IF_OIFS (ifp
)); rn
; rn
= route_next (rn
))
122 struct ospf_interface
*oi
;
124 if ( (oi
= rn
->info
) == NULL
)
133 ospf_if_reset_variables (struct ospf_interface
*oi
)
135 /* Set default values. */
136 /* don't clear this flag. oi->flag = OSPF_IF_DISABLE; */
139 oi
->type
= OSPF_IFTYPE_VIRTUALLINK
;
141 /* preserve network-type */
142 if (oi
->type
!= OSPF_IFTYPE_NBMA
)
143 oi
->type
= OSPF_IFTYPE_BROADCAST
;
145 oi
->state
= ISM_Down
;
147 oi
->crypt_seqnum
= 0;
149 /* This must be short, (less than RxmtInterval)
150 - RFC 2328 Section 13.5 para 3. Set to 1 second to avoid Acks being
151 held back for too long - MAG */
155 /* lookup oi for specified prefix/ifp */
156 struct ospf_interface
*
157 ospf_if_table_lookup (struct interface
*ifp
, struct prefix
*prefix
)
160 struct route_node
*rn
;
161 struct ospf_interface
*rninfo
= NULL
;
164 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
166 /* route_node_get implicitely locks */
167 if ((rn
= route_node_lookup (IF_OIFS (ifp
), &p
)))
169 rninfo
= (struct ospf_interface
*) rn
->info
;
170 route_unlock_node (rn
);
177 ospf_add_to_if (struct interface
*ifp
, struct ospf_interface
*oi
)
179 struct route_node
*rn
;
183 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
185 rn
= route_node_get (IF_OIFS (ifp
), &p
);
186 /* rn->info should either be NULL or equal to this oi
187 * as route_node_get may return an existing node
189 assert (!rn
->info
|| rn
->info
== oi
);
194 ospf_delete_from_if (struct interface
*ifp
, struct ospf_interface
*oi
)
196 struct route_node
*rn
;
200 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
202 rn
= route_node_lookup (IF_OIFS (oi
->ifp
), &p
);
206 route_unlock_node (rn
);
207 route_unlock_node (rn
);
210 struct ospf_interface
*
211 ospf_if_new (struct ospf
*ospf
, struct interface
*ifp
, struct prefix
*p
)
213 struct ospf_interface
*oi
;
215 if ((oi
= ospf_if_table_lookup (ifp
, p
)) == NULL
)
217 oi
= XCALLOC (MTYPE_OSPF_IF
, sizeof (struct ospf_interface
));
218 memset (oi
, 0, sizeof (struct ospf_interface
));
223 /* Set zebra interface pointer. */
227 ospf_add_to_if (ifp
, oi
);
228 listnode_add (ospf
->oiflist
, oi
);
230 /* Initialize neighbor list. */
231 oi
->nbrs
= route_table_init ();
233 /* Initialize static neighbor list. */
234 oi
->nbr_nbma
= list_new ();
236 /* Initialize Link State Acknowledgment list. */
237 oi
->ls_ack
= list_new ();
238 oi
->ls_ack_direct
.ls_ack
= list_new ();
240 /* Set default values. */
241 ospf_if_reset_variables (oi
);
243 /* Set pseudo neighbor to Null */
246 oi
->ls_upd_queue
= route_table_init ();
247 oi
->t_ls_upd_event
= NULL
;
248 oi
->t_ls_ack_direct
= NULL
;
250 oi
->crypt_seqnum
= time (NULL
);
252 ospf_opaque_type9_lsa_init (oi
);
255 QOBJ_REG (oi
, ospf_interface
);
260 /* Restore an interface to its pre UP state
261 Used from ism_interface_down only */
263 ospf_if_cleanup (struct ospf_interface
*oi
)
265 struct route_node
*rn
;
266 struct listnode
*node
, *nnode
;
267 struct ospf_neighbor
*nbr
;
268 struct ospf_nbr_nbma
*nbr_nbma
;
269 struct ospf_lsa
*lsa
;
271 /* oi->nbrs and oi->nbr_nbma should be deleted on InterfaceDown event */
272 /* delete all static neighbors attached to this interface */
273 for (ALL_LIST_ELEMENTS (oi
->nbr_nbma
, node
, nnode
, nbr_nbma
))
275 OSPF_POLL_TIMER_OFF (nbr_nbma
->t_poll
);
279 nbr_nbma
->nbr
->nbr_nbma
= NULL
;
280 nbr_nbma
->nbr
= NULL
;
285 listnode_delete (oi
->nbr_nbma
, nbr_nbma
);
288 /* send Neighbor event KillNbr to all associated neighbors. */
289 for (rn
= route_top (oi
->nbrs
); rn
; rn
= route_next (rn
))
290 if ((nbr
= rn
->info
) != NULL
)
291 if (nbr
!= oi
->nbr_self
)
292 OSPF_NSM_EVENT_EXECUTE (nbr
, NSM_KillNbr
);
294 /* Cleanup Link State Acknowlegdment list. */
295 for (ALL_LIST_ELEMENTS (oi
->ls_ack
, node
, nnode
, lsa
))
296 ospf_lsa_unlock (&lsa
); /* oi->ls_ack */
297 list_delete_all_node (oi
->ls_ack
);
299 oi
->crypt_seqnum
= 0;
301 /* Empty link state update queue */
302 ospf_ls_upd_queue_empty (oi
);
304 /* Reset pseudo neighbor. */
305 ospf_nbr_self_reset (oi
, oi
->ospf
->router_id
);
309 ospf_if_free (struct ospf_interface
*oi
)
313 assert (oi
->state
== ISM_Down
);
315 ospf_opaque_type9_lsa_term (oi
);
319 /* Free Pseudo Neighbour */
320 ospf_nbr_delete (oi
->nbr_self
);
322 route_table_finish (oi
->nbrs
);
323 route_table_finish (oi
->ls_upd_queue
);
325 /* Free any lists that should be freed */
326 list_free (oi
->nbr_nbma
);
328 list_free (oi
->ls_ack
);
329 list_free (oi
->ls_ack_direct
.ls_ack
);
331 ospf_delete_from_if (oi
->ifp
, oi
);
333 listnode_delete (oi
->ospf
->oiflist
, oi
);
334 listnode_delete (oi
->area
->oiflist
, oi
);
336 thread_cancel_event (master
, oi
);
338 memset (oi
, 0, sizeof (*oi
));
339 XFREE (MTYPE_OSPF_IF
, oi
);
343 ospf_if_is_up (struct ospf_interface
*oi
)
345 return if_is_up (oi
->ifp
);
348 struct ospf_interface
*
349 ospf_if_exists (struct ospf_interface
*oic
)
351 struct listnode
*node
;
353 struct ospf_interface
*oi
;
355 if ((ospf
= ospf_lookup ()) == NULL
)
358 for (ALL_LIST_ELEMENTS_RO (ospf
->oiflist
, node
, oi
))
365 /* Lookup OSPF interface by router LSA posistion */
366 struct ospf_interface
*
367 ospf_if_lookup_by_lsa_pos (struct ospf_area
*area
, int lsa_pos
)
369 struct listnode
*node
;
370 struct ospf_interface
*oi
;
372 for (ALL_LIST_ELEMENTS_RO (area
->oiflist
, node
, oi
))
374 if (lsa_pos
>= oi
->lsa_pos_beg
&& lsa_pos
< oi
->lsa_pos_end
)
380 struct ospf_interface
*
381 ospf_if_lookup_by_local_addr (struct ospf
*ospf
,
382 struct interface
*ifp
, struct in_addr address
)
384 struct listnode
*node
;
385 struct ospf_interface
*oi
;
387 for (ALL_LIST_ELEMENTS_RO (ospf
->oiflist
, node
, oi
))
388 if (oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
)
390 if (ifp
&& oi
->ifp
!= ifp
)
393 if (IPV4_ADDR_SAME (&address
, &oi
->address
->u
.prefix4
))
400 struct ospf_interface
*
401 ospf_if_lookup_by_prefix (struct ospf
*ospf
, struct prefix_ipv4
*p
)
403 struct listnode
*node
;
404 struct ospf_interface
*oi
;
406 /* Check each Interface. */
407 for (ALL_LIST_ELEMENTS_RO (ospf
->oiflist
, node
, oi
))
409 if (oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
)
413 prefix_copy (&ptmp
, CONNECTED_PREFIX(oi
->connected
));
415 if (prefix_same (&ptmp
, (struct prefix
*) p
))
422 /* determine receiving interface by ifp and source address */
423 struct ospf_interface
*
424 ospf_if_lookup_recv_if (struct ospf
*ospf
, struct in_addr src
,
425 struct interface
*ifp
)
427 struct route_node
*rn
;
428 struct prefix_ipv4 addr
;
429 struct ospf_interface
*oi
, *match
;
431 addr
.family
= AF_INET
;
433 addr
.prefixlen
= IPV4_MAX_BITLEN
;
437 for (rn
= route_top (IF_OIFS (ifp
)); rn
; rn
= route_next (rn
))
441 if (!oi
) /* oi can be NULL for PtP aliases */
444 if (oi
->type
== OSPF_IFTYPE_VIRTUALLINK
)
447 if (if_is_loopback (oi
->ifp
))
450 if (CHECK_FLAG(oi
->connected
->flags
, ZEBRA_IFA_UNNUMBERED
))
452 else if (prefix_match (CONNECTED_PREFIX(oi
->connected
),
453 (struct prefix
*) &addr
))
455 if ( (match
== NULL
) ||
456 (match
->address
->prefixlen
< oi
->address
->prefixlen
)
466 ospf_if_stream_set (struct ospf_interface
*oi
)
468 /* set output fifo queue. */
469 if (oi
->obuf
== NULL
)
470 oi
->obuf
= ospf_fifo_new ();
474 ospf_if_stream_unset (struct ospf_interface
*oi
)
476 struct ospf
*ospf
= oi
->ospf
;
480 ospf_fifo_free (oi
->obuf
);
485 listnode_delete (ospf
->oi_write_q
, oi
);
486 if (list_isempty(ospf
->oi_write_q
))
487 OSPF_TIMER_OFF (ospf
->t_write
);
494 static struct ospf_if_params
*
495 ospf_new_if_params (void)
497 struct ospf_if_params
*oip
;
499 oip
= XCALLOC (MTYPE_OSPF_IF_PARAMS
, sizeof (struct ospf_if_params
));
504 UNSET_IF_PARAM (oip
, output_cost_cmd
);
505 UNSET_IF_PARAM (oip
, transmit_delay
);
506 UNSET_IF_PARAM (oip
, retransmit_interval
);
507 UNSET_IF_PARAM (oip
, passive_interface
);
508 UNSET_IF_PARAM (oip
, v_hello
);
509 UNSET_IF_PARAM (oip
, fast_hello
);
510 UNSET_IF_PARAM (oip
, v_wait
);
511 UNSET_IF_PARAM (oip
, priority
);
512 UNSET_IF_PARAM (oip
, type
);
513 UNSET_IF_PARAM (oip
, auth_simple
);
514 UNSET_IF_PARAM (oip
, auth_crypt
);
515 UNSET_IF_PARAM (oip
, auth_type
);
517 oip
->auth_crypt
= list_new ();
519 oip
->network_lsa_seqnum
= htonl(OSPF_INITIAL_SEQUENCE_NUMBER
);
525 ospf_del_if_params (struct ospf_if_params
*oip
)
527 list_delete (oip
->auth_crypt
);
528 bfd_info_free(&(oip
->bfd_info
));
529 XFREE (MTYPE_OSPF_IF_PARAMS
, oip
);
533 ospf_free_if_params (struct interface
*ifp
, struct in_addr addr
)
535 struct ospf_if_params
*oip
;
536 struct prefix_ipv4 p
;
537 struct route_node
*rn
;
540 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
542 rn
= route_node_lookup (IF_OIFS_PARAMS (ifp
), (struct prefix
*)&p
);
543 if (!rn
|| !rn
->info
)
547 route_unlock_node (rn
);
549 if (!OSPF_IF_PARAM_CONFIGURED (oip
, output_cost_cmd
) &&
550 !OSPF_IF_PARAM_CONFIGURED (oip
, transmit_delay
) &&
551 !OSPF_IF_PARAM_CONFIGURED (oip
, retransmit_interval
) &&
552 !OSPF_IF_PARAM_CONFIGURED (oip
, passive_interface
) &&
553 !OSPF_IF_PARAM_CONFIGURED (oip
, v_hello
) &&
554 !OSPF_IF_PARAM_CONFIGURED (oip
, fast_hello
) &&
555 !OSPF_IF_PARAM_CONFIGURED (oip
, v_wait
) &&
556 !OSPF_IF_PARAM_CONFIGURED (oip
, priority
) &&
557 !OSPF_IF_PARAM_CONFIGURED (oip
, type
) &&
558 !OSPF_IF_PARAM_CONFIGURED (oip
, auth_simple
) &&
559 !OSPF_IF_PARAM_CONFIGURED (oip
, auth_type
) &&
560 listcount (oip
->auth_crypt
) == 0 &&
561 ntohl (oip
->network_lsa_seqnum
) != OSPF_INITIAL_SEQUENCE_NUMBER
)
563 ospf_del_if_params (oip
);
565 route_unlock_node (rn
);
569 struct ospf_if_params
*
570 ospf_lookup_if_params (struct interface
*ifp
, struct in_addr addr
)
572 struct prefix_ipv4 p
;
573 struct route_node
*rn
;
576 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
579 rn
= route_node_lookup (IF_OIFS_PARAMS (ifp
), (struct prefix
*)&p
);
583 route_unlock_node (rn
);
590 struct ospf_if_params
*
591 ospf_get_if_params (struct interface
*ifp
, struct in_addr addr
)
593 struct prefix_ipv4 p
;
594 struct route_node
*rn
;
597 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
600 rn
= route_node_get (IF_OIFS_PARAMS (ifp
), (struct prefix
*)&p
);
602 if (rn
->info
== NULL
)
603 rn
->info
= ospf_new_if_params ();
605 route_unlock_node (rn
);
611 ospf_if_update_params (struct interface
*ifp
, struct in_addr addr
)
613 struct route_node
*rn
;
614 struct ospf_interface
*oi
;
616 for (rn
= route_top (IF_OIFS (ifp
)); rn
; rn
= route_next (rn
))
618 if ((oi
= rn
->info
) == NULL
)
621 if (IPV4_ADDR_SAME (&oi
->address
->u
.prefix4
, &addr
))
622 oi
->params
= ospf_lookup_if_params (ifp
, oi
->address
->u
.prefix4
);
627 ospf_if_new_hook (struct interface
*ifp
)
631 ifp
->info
= XCALLOC (MTYPE_OSPF_IF_INFO
, sizeof (struct ospf_if_info
));
633 IF_OIFS (ifp
) = route_table_init ();
634 IF_OIFS_PARAMS (ifp
) = route_table_init ();
636 IF_DEF_PARAMS (ifp
) = ospf_new_if_params ();
638 SET_IF_PARAM (IF_DEF_PARAMS (ifp
), transmit_delay
);
639 IF_DEF_PARAMS (ifp
)->transmit_delay
= OSPF_TRANSMIT_DELAY_DEFAULT
;
641 SET_IF_PARAM (IF_DEF_PARAMS (ifp
), retransmit_interval
);
642 IF_DEF_PARAMS (ifp
)->retransmit_interval
= OSPF_RETRANSMIT_INTERVAL_DEFAULT
;
644 SET_IF_PARAM (IF_DEF_PARAMS (ifp
), priority
);
645 IF_DEF_PARAMS (ifp
)->priority
= OSPF_ROUTER_PRIORITY_DEFAULT
;
647 IF_DEF_PARAMS (ifp
)->mtu_ignore
= OSPF_MTU_IGNORE_DEFAULT
;
649 SET_IF_PARAM (IF_DEF_PARAMS (ifp
), v_hello
);
650 IF_DEF_PARAMS (ifp
)->v_hello
= OSPF_HELLO_INTERVAL_DEFAULT
;
652 SET_IF_PARAM (IF_DEF_PARAMS (ifp
), fast_hello
);
653 IF_DEF_PARAMS (ifp
)->fast_hello
= OSPF_FAST_HELLO_DEFAULT
;
655 SET_IF_PARAM (IF_DEF_PARAMS (ifp
), v_wait
);
656 IF_DEF_PARAMS (ifp
)->v_wait
= OSPF_ROUTER_DEAD_INTERVAL_DEFAULT
;
658 SET_IF_PARAM (IF_DEF_PARAMS (ifp
), auth_simple
);
659 memset (IF_DEF_PARAMS (ifp
)->auth_simple
, 0, OSPF_AUTH_SIMPLE_SIZE
);
661 SET_IF_PARAM (IF_DEF_PARAMS (ifp
), auth_type
);
662 IF_DEF_PARAMS (ifp
)->auth_type
= OSPF_AUTH_NOTSET
;
664 rc
= ospf_opaque_new_if (ifp
);
669 ospf_if_delete_hook (struct interface
*ifp
)
672 struct route_node
*rn
;
673 rc
= ospf_opaque_del_if (ifp
);
675 route_table_finish (IF_OIFS (ifp
));
677 for (rn
= route_top (IF_OIFS_PARAMS (ifp
)); rn
; rn
= route_next (rn
))
679 ospf_del_if_params (rn
->info
);
680 route_table_finish (IF_OIFS_PARAMS (ifp
));
682 ospf_del_if_params ((struct ospf_if_params
*) IF_DEF_PARAMS (ifp
));
683 XFREE (MTYPE_OSPF_IF_INFO
, ifp
->info
);
690 ospf_if_is_enable (struct ospf_interface
*oi
)
692 if (!if_is_loopback (oi
->ifp
))
693 if (if_is_up (oi
->ifp
))
700 ospf_if_set_multicast(struct ospf_interface
*oi
)
702 if ((oi
->state
> ISM_Loopback
) &&
703 (oi
->type
!= OSPF_IFTYPE_LOOPBACK
) &&
704 (oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
) &&
705 (OSPF_IF_PASSIVE_STATUS(oi
) == OSPF_IF_ACTIVE
))
707 /* The interface should belong to the OSPF-all-routers group. */
708 if (!OI_MEMBER_CHECK(oi
, MEMBER_ALLROUTERS
) &&
709 (ospf_if_add_allspfrouters(oi
->ospf
, oi
->address
,
710 oi
->ifp
->ifindex
) >= 0))
711 /* Set the flag only if the system call to join succeeded. */
712 OI_MEMBER_JOINED(oi
, MEMBER_ALLROUTERS
);
716 /* The interface should NOT belong to the OSPF-all-routers group. */
717 if (OI_MEMBER_CHECK(oi
, MEMBER_ALLROUTERS
))
719 /* Only actually drop if this is the last reference */
720 if (OI_MEMBER_COUNT(oi
, MEMBER_ALLROUTERS
) == 1)
721 ospf_if_drop_allspfrouters (oi
->ospf
, oi
->address
,
723 /* Unset the flag regardless of whether the system call to leave
724 the group succeeded, since it's much safer to assume that
725 we are not a member. */
726 OI_MEMBER_LEFT(oi
,MEMBER_ALLROUTERS
);
730 if (((oi
->type
== OSPF_IFTYPE_BROADCAST
) ||
731 (oi
->type
== OSPF_IFTYPE_POINTOPOINT
)) &&
732 ((oi
->state
== ISM_DR
) || (oi
->state
== ISM_Backup
)) &&
733 (OSPF_IF_PASSIVE_STATUS(oi
) == OSPF_IF_ACTIVE
))
735 /* The interface should belong to the OSPF-designated-routers group. */
736 if (!OI_MEMBER_CHECK(oi
, MEMBER_DROUTERS
) &&
737 (ospf_if_add_alldrouters(oi
->ospf
, oi
->address
,
738 oi
->ifp
->ifindex
) >= 0))
739 /* Set the flag only if the system call to join succeeded. */
740 OI_MEMBER_JOINED(oi
, MEMBER_DROUTERS
);
744 /* The interface should NOT belong to the OSPF-designated-routers group */
745 if (OI_MEMBER_CHECK(oi
, MEMBER_DROUTERS
))
747 /* drop only if last reference */
748 if (OI_MEMBER_COUNT(oi
, MEMBER_DROUTERS
) == 1)
749 ospf_if_drop_alldrouters(oi
->ospf
, oi
->address
, oi
->ifp
->ifindex
);
751 /* Unset the flag regardless of whether the system call to leave
752 the group succeeded, since it's much safer to assume that
753 we are not a member. */
754 OI_MEMBER_LEFT(oi
, MEMBER_DROUTERS
);
760 ospf_if_up (struct ospf_interface
*oi
)
765 if (oi
->type
== OSPF_IFTYPE_LOOPBACK
)
766 OSPF_ISM_EVENT_SCHEDULE (oi
, ISM_LoopInd
);
769 ospf_if_stream_set (oi
);
770 OSPF_ISM_EVENT_SCHEDULE (oi
, ISM_InterfaceUp
);
777 ospf_if_down (struct ospf_interface
*oi
)
782 OSPF_ISM_EVENT_EXECUTE (oi
, ISM_InterfaceDown
);
783 /* delete position in router LSA */
786 /* Shutdown packet reception and sending */
787 ospf_if_stream_unset (oi
);
793 /* Virtual Link related functions. */
795 struct ospf_vl_data
*
796 ospf_vl_data_new (struct ospf_area
*area
, struct in_addr vl_peer
)
798 struct ospf_vl_data
*vl_data
;
800 vl_data
= XCALLOC (MTYPE_OSPF_VL_DATA
, sizeof (struct ospf_vl_data
));
802 vl_data
->vl_peer
.s_addr
= vl_peer
.s_addr
;
803 vl_data
->vl_area_id
= area
->area_id
;
804 vl_data
->vl_area_id_fmt
= area
->area_id_fmt
;
810 ospf_vl_data_free (struct ospf_vl_data
*vl_data
)
812 XFREE (MTYPE_OSPF_VL_DATA
, vl_data
);
815 u_int vlink_count
= 0;
817 struct ospf_interface
*
818 ospf_vl_new (struct ospf
*ospf
, struct ospf_vl_data
*vl_data
)
820 struct ospf_interface
* voi
;
821 struct interface
* vi
;
822 char ifname
[INTERFACE_NAMSIZ
+ 1];
823 struct ospf_area
*area
;
824 struct in_addr area_id
;
825 struct connected
*co
;
826 struct prefix_ipv4
*p
;
828 if (IS_DEBUG_OSPF_EVENT
)
829 zlog_debug ("ospf_vl_new(): Start");
830 if (vlink_count
== OSPF_VL_MAX_COUNT
)
832 if (IS_DEBUG_OSPF_EVENT
)
833 zlog_debug ("ospf_vl_new(): Alarm: "
834 "cannot create more than OSPF_MAX_VL_COUNT virtual links");
838 if (IS_DEBUG_OSPF_EVENT
)
839 zlog_debug ("ospf_vl_new(): creating pseudo zebra interface");
841 snprintf (ifname
, sizeof(ifname
), "VLINK%d", vlink_count
);
842 vi
= if_create (ifname
, strnlen(ifname
, sizeof(ifname
)), VRF_DEFAULT
);
844 * if_create sets ZEBRA_INTERFACE_LINKDETECTION
845 * virtual links don't need this.
847 UNSET_FLAG (vi
->status
, ZEBRA_INTERFACE_LINKDETECTION
);
848 co
= connected_new ();
850 listnode_add (vi
->connected
, co
);
852 p
= prefix_ipv4_new ();
854 p
->prefix
.s_addr
= 0;
857 co
->address
= (struct prefix
*)p
;
859 voi
= ospf_if_new (ospf
, vi
, co
->address
);
862 if (IS_DEBUG_OSPF_EVENT
)
863 zlog_debug ("ospf_vl_new(): Alarm: OSPF int structure is not created");
867 voi
->vl_data
= vl_data
;
868 voi
->ifp
->mtu
= OSPF_VL_MTU
;
869 voi
->type
= OSPF_IFTYPE_VIRTUALLINK
;
872 if (IS_DEBUG_OSPF_EVENT
)
873 zlog_debug ("ospf_vl_new(): Created name: %s", ifname
);
874 if (IS_DEBUG_OSPF_EVENT
)
875 zlog_debug ("ospf_vl_new(): set if->name to %s", vi
->name
);
878 area
= ospf_area_get (ospf
, area_id
);
881 if (IS_DEBUG_OSPF_EVENT
)
882 zlog_debug ("ospf_vl_new(): set associated area to the backbone");
884 /* Add pseudo neighbor. */
885 ospf_nbr_self_reset (voi
, voi
->ospf
->router_id
);
887 ospf_area_add_if (voi
->area
, voi
);
889 ospf_if_stream_set (voi
);
891 if (IS_DEBUG_OSPF_EVENT
)
892 zlog_debug ("ospf_vl_new(): Stop");
897 ospf_vl_if_delete (struct ospf_vl_data
*vl_data
)
899 struct interface
*ifp
= vl_data
->vl_oi
->ifp
;
900 vl_data
->vl_oi
->address
->u
.prefix4
.s_addr
= 0;
901 vl_data
->vl_oi
->address
->prefixlen
= 0;
902 ospf_if_free (vl_data
->vl_oi
);
907 /* Look up vl_data for given peer, optionally qualified to be in the
908 * specified area. NULL area returns first found..
910 struct ospf_vl_data
*
911 ospf_vl_lookup (struct ospf
*ospf
, struct ospf_area
*area
,
912 struct in_addr vl_peer
)
914 struct ospf_vl_data
*vl_data
;
915 struct listnode
*node
;
917 if (IS_DEBUG_OSPF_EVENT
)
919 zlog_debug ("%s: Looking for %s", __func__
, inet_ntoa (vl_peer
));
921 zlog_debug ("%s: in area %s", __func__
, inet_ntoa (area
->area_id
));
924 for (ALL_LIST_ELEMENTS_RO (ospf
->vlinks
, node
, vl_data
))
926 if (IS_DEBUG_OSPF_EVENT
)
927 zlog_debug ("%s: VL %s, peer %s", __func__
,
928 vl_data
->vl_oi
->ifp
->name
,
929 inet_ntoa (vl_data
->vl_peer
));
931 if (area
&& !IPV4_ADDR_SAME (&vl_data
->vl_area_id
, &area
->area_id
))
934 if (IPV4_ADDR_SAME (&vl_data
->vl_peer
, &vl_peer
))
942 ospf_vl_shutdown (struct ospf_vl_data
*vl_data
)
944 struct ospf_interface
*oi
;
946 if ((oi
= vl_data
->vl_oi
) == NULL
)
949 oi
->address
->u
.prefix4
.s_addr
= 0;
950 oi
->address
->prefixlen
= 0;
952 UNSET_FLAG (oi
->ifp
->flags
, IFF_UP
);
953 /* OSPF_ISM_EVENT_SCHEDULE (oi, ISM_InterfaceDown); */
954 OSPF_ISM_EVENT_EXECUTE (oi
, ISM_InterfaceDown
);
958 ospf_vl_add (struct ospf
*ospf
, struct ospf_vl_data
*vl_data
)
960 listnode_add (ospf
->vlinks
, vl_data
);
961 hook_call(ospf_vl_add
, vl_data
);
965 ospf_vl_delete (struct ospf
*ospf
, struct ospf_vl_data
*vl_data
)
967 ospf_vl_shutdown (vl_data
);
968 ospf_vl_if_delete (vl_data
);
970 hook_call(ospf_vl_delete
, vl_data
);
971 listnode_delete (ospf
->vlinks
, vl_data
);
973 ospf_vl_data_free (vl_data
);
977 ospf_vl_set_params (struct ospf_vl_data
*vl_data
, struct vertex
*v
)
980 struct ospf_interface
*voi
;
981 struct listnode
*node
;
982 struct vertex_parent
*vp
= NULL
;
984 struct router_lsa
*rl
;
986 voi
= vl_data
->vl_oi
;
988 if (voi
->output_cost
!= v
->distance
)
991 voi
->output_cost
= v
->distance
;
995 for (ALL_LIST_ELEMENTS_RO (v
->parents
, node
, vp
))
997 vl_data
->nexthop
.oi
= vp
->nexthop
->oi
;
998 vl_data
->nexthop
.router
= vp
->nexthop
->router
;
1000 if (!IPV4_ADDR_SAME(&voi
->address
->u
.prefix4
,
1001 &vl_data
->nexthop
.oi
->address
->u
.prefix4
))
1004 voi
->address
->u
.prefix4
= vl_data
->nexthop
.oi
->address
->u
.prefix4
;
1005 voi
->address
->prefixlen
= vl_data
->nexthop
.oi
->address
->prefixlen
;
1007 break; /* We take the first interface. */
1010 rl
= (struct router_lsa
*)v
->lsa
;
1012 /* use SPF determined backlink index in struct vertex
1013 * for virtual link destination address
1015 if (vp
&& vp
->backlink
>= 0)
1017 if (!IPV4_ADDR_SAME (&vl_data
->peer_addr
,
1018 &rl
->link
[vp
->backlink
].link_data
))
1020 vl_data
->peer_addr
= rl
->link
[vp
->backlink
].link_data
;
1024 /* This is highly odd, there is no backlink index
1025 * there should be due to the ospf_spf_has_link() check
1026 * in SPF. Lets warn and try pick a link anyway.
1028 zlog_warn ("ospf_vl_set_params: No backlink for %s!",
1029 vl_data
->vl_oi
->ifp
->name
);
1030 for (i
= 0; i
< ntohs (rl
->links
); i
++)
1032 switch (rl
->link
[i
].type
)
1034 case LSA_LINK_TYPE_VIRTUALLINK
:
1035 if (IS_DEBUG_OSPF_EVENT
)
1036 zlog_debug ("found back link through VL");
1037 case LSA_LINK_TYPE_TRANSIT
:
1038 case LSA_LINK_TYPE_POINTOPOINT
:
1039 if (!IPV4_ADDR_SAME (&vl_data
->peer_addr
,
1040 &rl
->link
[i
].link_data
))
1042 vl_data
->peer_addr
= rl
->link
[i
].link_data
;
1047 if (IS_DEBUG_OSPF_EVENT
)
1048 zlog_debug ("%s: %s peer address: %s, cost: %d,%schanged", __func__
,
1049 vl_data
->vl_oi
->ifp
->name
,
1050 inet_ntoa(vl_data
->peer_addr
),
1052 (changed
? " " : " un"));
1059 ospf_vl_up_check (struct ospf_area
*area
, struct in_addr rid
,
1062 struct ospf
*ospf
= area
->ospf
;
1063 struct listnode
*node
;
1064 struct ospf_vl_data
*vl_data
;
1065 struct ospf_interface
*oi
;
1067 if (IS_DEBUG_OSPF_EVENT
)
1069 zlog_debug ("ospf_vl_up_check(): Start");
1070 zlog_debug ("ospf_vl_up_check(): Router ID is %s", inet_ntoa (rid
));
1071 zlog_debug ("ospf_vl_up_check(): Area is %s", inet_ntoa (area
->area_id
));
1074 for (ALL_LIST_ELEMENTS_RO (ospf
->vlinks
, node
, vl_data
))
1076 if (IS_DEBUG_OSPF_EVENT
)
1078 zlog_debug ("%s: considering VL, %s in area %s", __func__
,
1079 vl_data
->vl_oi
->ifp
->name
,
1080 inet_ntoa (vl_data
->vl_area_id
));
1081 zlog_debug ("%s: peer ID: %s", __func__
,
1082 inet_ntoa (vl_data
->vl_peer
));
1085 if (IPV4_ADDR_SAME (&vl_data
->vl_peer
, &rid
) &&
1086 IPV4_ADDR_SAME (&vl_data
->vl_area_id
, &area
->area_id
))
1088 oi
= vl_data
->vl_oi
;
1089 SET_FLAG (vl_data
->flags
, OSPF_VL_FLAG_APPROVED
);
1091 if (IS_DEBUG_OSPF_EVENT
)
1092 zlog_debug ("ospf_vl_up_check(): this VL matched");
1094 if (oi
->state
== ISM_Down
)
1096 if (IS_DEBUG_OSPF_EVENT
)
1097 zlog_debug ("ospf_vl_up_check(): VL is down, waking it up");
1098 SET_FLAG (oi
->ifp
->flags
, IFF_UP
);
1099 OSPF_ISM_EVENT_EXECUTE(oi
,ISM_InterfaceUp
);
1102 if (ospf_vl_set_params (vl_data
, v
))
1104 if (IS_DEBUG_OSPF (ism
, ISM_EVENTS
))
1105 zlog_debug ("ospf_vl_up_check: VL cost change,"
1106 " scheduling router lsa refresh");
1108 ospf_router_lsa_update_area (ospf
->backbone
);
1109 else if (IS_DEBUG_OSPF (ism
, ISM_EVENTS
))
1110 zlog_debug ("ospf_vl_up_check: VL cost change, no backbone!");
1117 ospf_vl_unapprove (struct ospf
*ospf
)
1119 struct listnode
*node
;
1120 struct ospf_vl_data
*vl_data
;
1122 for (ALL_LIST_ELEMENTS_RO (ospf
->vlinks
, node
, vl_data
))
1123 UNSET_FLAG (vl_data
->flags
, OSPF_VL_FLAG_APPROVED
);
1127 ospf_vl_shut_unapproved (struct ospf
*ospf
)
1129 struct listnode
*node
, *nnode
;
1130 struct ospf_vl_data
*vl_data
;
1132 for (ALL_LIST_ELEMENTS (ospf
->vlinks
, node
, nnode
, vl_data
))
1133 if (!CHECK_FLAG (vl_data
->flags
, OSPF_VL_FLAG_APPROVED
))
1134 ospf_vl_shutdown (vl_data
);
1138 ospf_full_virtual_nbrs (struct ospf_area
*area
)
1140 if (IS_DEBUG_OSPF_EVENT
)
1142 zlog_debug ("counting fully adjacent virtual neighbors in area %s",
1143 inet_ntoa (area
->area_id
));
1144 zlog_debug ("there are %d of them", area
->full_vls
);
1147 return area
->full_vls
;
1151 ospf_vls_in_area (struct ospf_area
*area
)
1153 struct listnode
*node
;
1154 struct ospf_vl_data
*vl_data
;
1157 for (ALL_LIST_ELEMENTS_RO (area
->ospf
->vlinks
, node
, vl_data
))
1158 if (IPV4_ADDR_SAME (&vl_data
->vl_area_id
, &area
->area_id
))
1166 ospf_crypt_key_new ()
1168 return XCALLOC (MTYPE_OSPF_CRYPT_KEY
, sizeof (struct crypt_key
));
1172 ospf_crypt_key_add (struct list
*crypt
, struct crypt_key
*ck
)
1174 listnode_add (crypt
, ck
);
1178 ospf_crypt_key_lookup (struct list
*auth_crypt
, u_char key_id
)
1180 struct listnode
*node
;
1181 struct crypt_key
*ck
;
1183 for (ALL_LIST_ELEMENTS_RO (auth_crypt
, node
, ck
))
1184 if (ck
->key_id
== key_id
)
1191 ospf_crypt_key_delete (struct list
*auth_crypt
, u_char key_id
)
1193 struct listnode
*node
, *nnode
;
1194 struct crypt_key
*ck
;
1196 for (ALL_LIST_ELEMENTS (auth_crypt
, node
, nnode
, ck
))
1198 if (ck
->key_id
== key_id
)
1200 listnode_delete (auth_crypt
, ck
);
1201 XFREE (MTYPE_OSPF_CRYPT_KEY
, ck
);
1210 ospf_default_iftype(struct interface
*ifp
)
1212 if (if_is_pointopoint (ifp
))
1213 return OSPF_IFTYPE_POINTOPOINT
;
1214 else if (if_is_loopback (ifp
))
1215 return OSPF_IFTYPE_LOOPBACK
;
1217 return OSPF_IFTYPE_BROADCAST
;
1223 /* Initialize Zebra interface data structure. */
1224 om
->iflist
= vrf_iflist (VRF_DEFAULT
);
1225 if_add_hook (IF_NEW_HOOK
, ospf_if_new_hook
);
1226 if_add_hook (IF_DELETE_HOOK
, ospf_if_delete_hook
);