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 bw
= oi
->ifp
->bandwidth
? oi
->ifp
->bandwidth
: OSPF_DEFAULT_BANDWIDTH
;
63 refbw
= oi
->ospf
->ref_bandwidth
;
65 /* A specifed ip ospf cost overrides a calculated one. */
66 if (OSPF_IF_PARAM_CONFIGURED (IF_DEF_PARAMS (oi
->ifp
), output_cost_cmd
) ||
67 OSPF_IF_PARAM_CONFIGURED (oi
->params
, output_cost_cmd
))
68 cost
= OSPF_IF_PARAM (oi
, output_cost_cmd
);
69 /* See if a cost can be calculated from the zebra processes
70 interface bandwidth field. */
73 cost
= (u_int32_t
) ((double)refbw
/ (double)bw
+ (double)0.5);
76 else if (cost
> 65535)
84 ospf_if_recalculate_output_cost (struct interface
*ifp
)
87 struct route_node
*rn
;
89 for (rn
= route_top (IF_OIFS (ifp
)); rn
; rn
= route_next (rn
))
91 struct ospf_interface
*oi
;
93 if ( (oi
= rn
->info
) == NULL
)
96 newcost
= ospf_if_get_output_cost (oi
);
98 /* Is actual output cost changed? */
99 if (oi
->output_cost
!= newcost
)
101 oi
->output_cost
= newcost
;
102 ospf_router_lsa_update_area (oi
->area
);
107 /* Simulate down/up on the interface. This is needed, for example, when
110 ospf_if_reset(struct interface
*ifp
)
112 struct route_node
*rn
;
114 for (rn
= route_top (IF_OIFS (ifp
)); rn
; rn
= route_next (rn
))
116 struct ospf_interface
*oi
;
118 if ( (oi
= rn
->info
) == NULL
)
127 ospf_if_reset_variables (struct ospf_interface
*oi
)
129 /* Set default values. */
130 /* don't clear this flag. oi->flag = OSPF_IF_DISABLE; */
133 oi
->type
= OSPF_IFTYPE_VIRTUALLINK
;
135 /* preserve network-type */
136 if (oi
->type
!= OSPF_IFTYPE_NBMA
)
137 oi
->type
= OSPF_IFTYPE_BROADCAST
;
139 oi
->state
= ISM_Down
;
141 oi
->crypt_seqnum
= 0;
143 /* This must be short, (less than RxmtInterval)
144 - RFC 2328 Section 13.5 para 3. Set to 1 second to avoid Acks being
145 held back for too long - MAG */
149 /* lookup oi for specified prefix/ifp */
150 struct ospf_interface
*
151 ospf_if_table_lookup (struct interface
*ifp
, struct prefix
*prefix
)
154 struct route_node
*rn
;
155 struct ospf_interface
*rninfo
= NULL
;
158 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
160 /* route_node_get implicitely locks */
161 if ((rn
= route_node_lookup (IF_OIFS (ifp
), &p
)))
163 rninfo
= (struct ospf_interface
*) rn
->info
;
164 route_unlock_node (rn
);
171 ospf_add_to_if (struct interface
*ifp
, struct ospf_interface
*oi
)
173 struct route_node
*rn
;
177 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
179 rn
= route_node_get (IF_OIFS (ifp
), &p
);
180 /* rn->info should either be NULL or equal to this oi
181 * as route_node_get may return an existing node
183 assert (!rn
->info
|| rn
->info
== oi
);
188 ospf_delete_from_if (struct interface
*ifp
, struct ospf_interface
*oi
)
190 struct route_node
*rn
;
194 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
196 rn
= route_node_lookup (IF_OIFS (oi
->ifp
), &p
);
200 route_unlock_node (rn
);
201 route_unlock_node (rn
);
204 struct ospf_interface
*
205 ospf_if_new (struct ospf
*ospf
, struct interface
*ifp
, struct prefix
*p
)
207 struct ospf_interface
*oi
;
209 if ((oi
= ospf_if_table_lookup (ifp
, p
)) == NULL
)
211 oi
= XCALLOC (MTYPE_OSPF_IF
, sizeof (struct ospf_interface
));
212 memset (oi
, 0, sizeof (struct ospf_interface
));
217 /* Set zebra interface pointer. */
221 ospf_add_to_if (ifp
, oi
);
222 listnode_add (ospf
->oiflist
, oi
);
224 /* Initialize neighbor list. */
225 oi
->nbrs
= route_table_init ();
227 /* Initialize static neighbor list. */
228 oi
->nbr_nbma
= list_new ();
230 /* Initialize Link State Acknowledgment list. */
231 oi
->ls_ack
= list_new ();
232 oi
->ls_ack_direct
.ls_ack
= list_new ();
234 /* Set default values. */
235 ospf_if_reset_variables (oi
);
237 /* Set pseudo neighbor to Null */
240 oi
->ls_upd_queue
= route_table_init ();
241 oi
->t_ls_upd_event
= NULL
;
242 oi
->t_ls_ack_direct
= NULL
;
244 oi
->crypt_seqnum
= time (NULL
);
246 ospf_opaque_type9_lsa_init (oi
);
249 QOBJ_REG (oi
, ospf_interface
);
254 /* Restore an interface to its pre UP state
255 Used from ism_interface_down only */
257 ospf_if_cleanup (struct ospf_interface
*oi
)
259 struct route_node
*rn
;
260 struct listnode
*node
, *nnode
;
261 struct ospf_neighbor
*nbr
;
262 struct ospf_nbr_nbma
*nbr_nbma
;
263 struct ospf_lsa
*lsa
;
265 /* oi->nbrs and oi->nbr_nbma should be deleted on InterfaceDown event */
266 /* delete all static neighbors attached to this interface */
267 for (ALL_LIST_ELEMENTS (oi
->nbr_nbma
, node
, nnode
, nbr_nbma
))
269 OSPF_POLL_TIMER_OFF (nbr_nbma
->t_poll
);
273 nbr_nbma
->nbr
->nbr_nbma
= NULL
;
274 nbr_nbma
->nbr
= NULL
;
279 listnode_delete (oi
->nbr_nbma
, nbr_nbma
);
282 /* send Neighbor event KillNbr to all associated neighbors. */
283 for (rn
= route_top (oi
->nbrs
); rn
; rn
= route_next (rn
))
284 if ((nbr
= rn
->info
) != NULL
)
285 if (nbr
!= oi
->nbr_self
)
286 OSPF_NSM_EVENT_EXECUTE (nbr
, NSM_KillNbr
);
288 /* Cleanup Link State Acknowlegdment list. */
289 for (ALL_LIST_ELEMENTS (oi
->ls_ack
, node
, nnode
, lsa
))
290 ospf_lsa_unlock (&lsa
); /* oi->ls_ack */
291 list_delete_all_node (oi
->ls_ack
);
293 oi
->crypt_seqnum
= 0;
295 /* Empty link state update queue */
296 ospf_ls_upd_queue_empty (oi
);
298 /* Reset pseudo neighbor. */
299 ospf_nbr_self_reset (oi
, oi
->ospf
->router_id
);
303 ospf_if_free (struct ospf_interface
*oi
)
307 assert (oi
->state
== ISM_Down
);
309 ospf_opaque_type9_lsa_term (oi
);
313 /* Free Pseudo Neighbour */
314 ospf_nbr_delete (oi
->nbr_self
);
316 route_table_finish (oi
->nbrs
);
317 route_table_finish (oi
->ls_upd_queue
);
319 /* Free any lists that should be freed */
320 list_free (oi
->nbr_nbma
);
322 list_free (oi
->ls_ack
);
323 list_free (oi
->ls_ack_direct
.ls_ack
);
325 ospf_delete_from_if (oi
->ifp
, oi
);
327 listnode_delete (oi
->ospf
->oiflist
, oi
);
328 listnode_delete (oi
->area
->oiflist
, oi
);
330 thread_cancel_event (master
, oi
);
332 memset (oi
, 0, sizeof (*oi
));
333 XFREE (MTYPE_OSPF_IF
, oi
);
337 ospf_if_is_up (struct ospf_interface
*oi
)
339 return if_is_up (oi
->ifp
);
342 struct ospf_interface
*
343 ospf_if_exists (struct ospf_interface
*oic
)
345 struct listnode
*node
;
347 struct ospf_interface
*oi
;
349 if ((ospf
= ospf_lookup ()) == NULL
)
352 for (ALL_LIST_ELEMENTS_RO (ospf
->oiflist
, node
, oi
))
359 /* Lookup OSPF interface by router LSA posistion */
360 struct ospf_interface
*
361 ospf_if_lookup_by_lsa_pos (struct ospf_area
*area
, int lsa_pos
)
363 struct listnode
*node
;
364 struct ospf_interface
*oi
;
366 for (ALL_LIST_ELEMENTS_RO (area
->oiflist
, node
, oi
))
368 if (lsa_pos
>= oi
->lsa_pos_beg
&& lsa_pos
< oi
->lsa_pos_end
)
374 struct ospf_interface
*
375 ospf_if_lookup_by_local_addr (struct ospf
*ospf
,
376 struct interface
*ifp
, struct in_addr address
)
378 struct listnode
*node
;
379 struct ospf_interface
*oi
;
381 for (ALL_LIST_ELEMENTS_RO (ospf
->oiflist
, node
, oi
))
382 if (oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
)
384 if (ifp
&& oi
->ifp
!= ifp
)
387 if (IPV4_ADDR_SAME (&address
, &oi
->address
->u
.prefix4
))
394 struct ospf_interface
*
395 ospf_if_lookup_by_prefix (struct ospf
*ospf
, struct prefix_ipv4
*p
)
397 struct listnode
*node
;
398 struct ospf_interface
*oi
;
400 /* Check each Interface. */
401 for (ALL_LIST_ELEMENTS_RO (ospf
->oiflist
, node
, oi
))
403 if (oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
)
407 prefix_copy (&ptmp
, CONNECTED_PREFIX(oi
->connected
));
409 if (prefix_same (&ptmp
, (struct prefix
*) p
))
416 /* determine receiving interface by ifp and source address */
417 struct ospf_interface
*
418 ospf_if_lookup_recv_if (struct ospf
*ospf
, struct in_addr src
,
419 struct interface
*ifp
)
421 struct route_node
*rn
;
422 struct prefix_ipv4 addr
;
423 struct ospf_interface
*oi
, *match
;
425 addr
.family
= AF_INET
;
427 addr
.prefixlen
= IPV4_MAX_BITLEN
;
431 for (rn
= route_top (IF_OIFS (ifp
)); rn
; rn
= route_next (rn
))
435 if (!oi
) /* oi can be NULL for PtP aliases */
438 if (oi
->type
== OSPF_IFTYPE_VIRTUALLINK
)
441 if (if_is_loopback (oi
->ifp
))
444 if (CHECK_FLAG(oi
->connected
->flags
, ZEBRA_IFA_UNNUMBERED
))
446 else if (prefix_match (CONNECTED_PREFIX(oi
->connected
),
447 (struct prefix
*) &addr
))
449 if ( (match
== NULL
) ||
450 (match
->address
->prefixlen
< oi
->address
->prefixlen
)
460 ospf_if_stream_set (struct ospf_interface
*oi
)
462 /* set output fifo queue. */
463 if (oi
->obuf
== NULL
)
464 oi
->obuf
= ospf_fifo_new ();
468 ospf_if_stream_unset (struct ospf_interface
*oi
)
470 struct ospf
*ospf
= oi
->ospf
;
474 ospf_fifo_free (oi
->obuf
);
479 listnode_delete (ospf
->oi_write_q
, oi
);
480 if (list_isempty(ospf
->oi_write_q
))
481 OSPF_TIMER_OFF (ospf
->t_write
);
488 static struct ospf_if_params
*
489 ospf_new_if_params (void)
491 struct ospf_if_params
*oip
;
493 oip
= XCALLOC (MTYPE_OSPF_IF_PARAMS
, sizeof (struct ospf_if_params
));
498 UNSET_IF_PARAM (oip
, output_cost_cmd
);
499 UNSET_IF_PARAM (oip
, transmit_delay
);
500 UNSET_IF_PARAM (oip
, retransmit_interval
);
501 UNSET_IF_PARAM (oip
, passive_interface
);
502 UNSET_IF_PARAM (oip
, v_hello
);
503 UNSET_IF_PARAM (oip
, fast_hello
);
504 UNSET_IF_PARAM (oip
, v_wait
);
505 UNSET_IF_PARAM (oip
, priority
);
506 UNSET_IF_PARAM (oip
, type
);
507 UNSET_IF_PARAM (oip
, auth_simple
);
508 UNSET_IF_PARAM (oip
, auth_crypt
);
509 UNSET_IF_PARAM (oip
, auth_type
);
511 oip
->auth_crypt
= list_new ();
513 oip
->network_lsa_seqnum
= htonl(OSPF_INITIAL_SEQUENCE_NUMBER
);
519 ospf_del_if_params (struct ospf_if_params
*oip
)
521 list_delete (oip
->auth_crypt
);
522 bfd_info_free(&(oip
->bfd_info
));
523 XFREE (MTYPE_OSPF_IF_PARAMS
, oip
);
527 ospf_free_if_params (struct interface
*ifp
, struct in_addr addr
)
529 struct ospf_if_params
*oip
;
530 struct prefix_ipv4 p
;
531 struct route_node
*rn
;
534 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
536 rn
= route_node_lookup (IF_OIFS_PARAMS (ifp
), (struct prefix
*)&p
);
537 if (!rn
|| !rn
->info
)
541 route_unlock_node (rn
);
543 if (!OSPF_IF_PARAM_CONFIGURED (oip
, output_cost_cmd
) &&
544 !OSPF_IF_PARAM_CONFIGURED (oip
, transmit_delay
) &&
545 !OSPF_IF_PARAM_CONFIGURED (oip
, retransmit_interval
) &&
546 !OSPF_IF_PARAM_CONFIGURED (oip
, passive_interface
) &&
547 !OSPF_IF_PARAM_CONFIGURED (oip
, v_hello
) &&
548 !OSPF_IF_PARAM_CONFIGURED (oip
, fast_hello
) &&
549 !OSPF_IF_PARAM_CONFIGURED (oip
, v_wait
) &&
550 !OSPF_IF_PARAM_CONFIGURED (oip
, priority
) &&
551 !OSPF_IF_PARAM_CONFIGURED (oip
, type
) &&
552 !OSPF_IF_PARAM_CONFIGURED (oip
, auth_simple
) &&
553 !OSPF_IF_PARAM_CONFIGURED (oip
, auth_type
) &&
554 listcount (oip
->auth_crypt
) == 0 &&
555 ntohl (oip
->network_lsa_seqnum
) != OSPF_INITIAL_SEQUENCE_NUMBER
)
557 ospf_del_if_params (oip
);
559 route_unlock_node (rn
);
563 struct ospf_if_params
*
564 ospf_lookup_if_params (struct interface
*ifp
, struct in_addr addr
)
566 struct prefix_ipv4 p
;
567 struct route_node
*rn
;
570 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
573 rn
= route_node_lookup (IF_OIFS_PARAMS (ifp
), (struct prefix
*)&p
);
577 route_unlock_node (rn
);
584 struct ospf_if_params
*
585 ospf_get_if_params (struct interface
*ifp
, struct in_addr addr
)
587 struct prefix_ipv4 p
;
588 struct route_node
*rn
;
591 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
594 rn
= route_node_get (IF_OIFS_PARAMS (ifp
), (struct prefix
*)&p
);
596 if (rn
->info
== NULL
)
597 rn
->info
= ospf_new_if_params ();
599 route_unlock_node (rn
);
605 ospf_if_update_params (struct interface
*ifp
, struct in_addr addr
)
607 struct route_node
*rn
;
608 struct ospf_interface
*oi
;
610 for (rn
= route_top (IF_OIFS (ifp
)); rn
; rn
= route_next (rn
))
612 if ((oi
= rn
->info
) == NULL
)
615 if (IPV4_ADDR_SAME (&oi
->address
->u
.prefix4
, &addr
))
616 oi
->params
= ospf_lookup_if_params (ifp
, oi
->address
->u
.prefix4
);
621 ospf_if_new_hook (struct interface
*ifp
)
625 ifp
->info
= XCALLOC (MTYPE_OSPF_IF_INFO
, sizeof (struct ospf_if_info
));
627 IF_OIFS (ifp
) = route_table_init ();
628 IF_OIFS_PARAMS (ifp
) = route_table_init ();
630 IF_DEF_PARAMS (ifp
) = ospf_new_if_params ();
632 SET_IF_PARAM (IF_DEF_PARAMS (ifp
), transmit_delay
);
633 IF_DEF_PARAMS (ifp
)->transmit_delay
= OSPF_TRANSMIT_DELAY_DEFAULT
;
635 SET_IF_PARAM (IF_DEF_PARAMS (ifp
), retransmit_interval
);
636 IF_DEF_PARAMS (ifp
)->retransmit_interval
= OSPF_RETRANSMIT_INTERVAL_DEFAULT
;
638 SET_IF_PARAM (IF_DEF_PARAMS (ifp
), priority
);
639 IF_DEF_PARAMS (ifp
)->priority
= OSPF_ROUTER_PRIORITY_DEFAULT
;
641 IF_DEF_PARAMS (ifp
)->mtu_ignore
= OSPF_MTU_IGNORE_DEFAULT
;
643 SET_IF_PARAM (IF_DEF_PARAMS (ifp
), v_hello
);
644 IF_DEF_PARAMS (ifp
)->v_hello
= OSPF_HELLO_INTERVAL_DEFAULT
;
646 SET_IF_PARAM (IF_DEF_PARAMS (ifp
), fast_hello
);
647 IF_DEF_PARAMS (ifp
)->fast_hello
= OSPF_FAST_HELLO_DEFAULT
;
649 SET_IF_PARAM (IF_DEF_PARAMS (ifp
), v_wait
);
650 IF_DEF_PARAMS (ifp
)->v_wait
= OSPF_ROUTER_DEAD_INTERVAL_DEFAULT
;
652 SET_IF_PARAM (IF_DEF_PARAMS (ifp
), auth_simple
);
653 memset (IF_DEF_PARAMS (ifp
)->auth_simple
, 0, OSPF_AUTH_SIMPLE_SIZE
);
655 SET_IF_PARAM (IF_DEF_PARAMS (ifp
), auth_type
);
656 IF_DEF_PARAMS (ifp
)->auth_type
= OSPF_AUTH_NOTSET
;
658 rc
= ospf_opaque_new_if (ifp
);
663 ospf_if_delete_hook (struct interface
*ifp
)
666 struct route_node
*rn
;
667 rc
= ospf_opaque_del_if (ifp
);
669 route_table_finish (IF_OIFS (ifp
));
671 for (rn
= route_top (IF_OIFS_PARAMS (ifp
)); rn
; rn
= route_next (rn
))
673 ospf_del_if_params (rn
->info
);
674 route_table_finish (IF_OIFS_PARAMS (ifp
));
676 ospf_del_if_params ((struct ospf_if_params
*) IF_DEF_PARAMS (ifp
));
677 XFREE (MTYPE_OSPF_IF_INFO
, ifp
->info
);
684 ospf_if_is_enable (struct ospf_interface
*oi
)
686 if (!if_is_loopback (oi
->ifp
))
687 if (if_is_up (oi
->ifp
))
694 ospf_if_set_multicast(struct ospf_interface
*oi
)
696 if ((oi
->state
> ISM_Loopback
) &&
697 (oi
->type
!= OSPF_IFTYPE_LOOPBACK
) &&
698 (oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
) &&
699 (OSPF_IF_PASSIVE_STATUS(oi
) == OSPF_IF_ACTIVE
))
701 /* The interface should belong to the OSPF-all-routers group. */
702 if (!OI_MEMBER_CHECK(oi
, MEMBER_ALLROUTERS
) &&
703 (ospf_if_add_allspfrouters(oi
->ospf
, oi
->address
,
704 oi
->ifp
->ifindex
) >= 0))
705 /* Set the flag only if the system call to join succeeded. */
706 OI_MEMBER_JOINED(oi
, MEMBER_ALLROUTERS
);
710 /* The interface should NOT belong to the OSPF-all-routers group. */
711 if (OI_MEMBER_CHECK(oi
, MEMBER_ALLROUTERS
))
713 /* Only actually drop if this is the last reference */
714 if (OI_MEMBER_COUNT(oi
, MEMBER_ALLROUTERS
) == 1)
715 ospf_if_drop_allspfrouters (oi
->ospf
, oi
->address
,
717 /* Unset the flag regardless of whether the system call to leave
718 the group succeeded, since it's much safer to assume that
719 we are not a member. */
720 OI_MEMBER_LEFT(oi
,MEMBER_ALLROUTERS
);
724 if (((oi
->type
== OSPF_IFTYPE_BROADCAST
) ||
725 (oi
->type
== OSPF_IFTYPE_POINTOPOINT
)) &&
726 ((oi
->state
== ISM_DR
) || (oi
->state
== ISM_Backup
)) &&
727 (OSPF_IF_PASSIVE_STATUS(oi
) == OSPF_IF_ACTIVE
))
729 /* The interface should belong to the OSPF-designated-routers group. */
730 if (!OI_MEMBER_CHECK(oi
, MEMBER_DROUTERS
) &&
731 (ospf_if_add_alldrouters(oi
->ospf
, oi
->address
,
732 oi
->ifp
->ifindex
) >= 0))
733 /* Set the flag only if the system call to join succeeded. */
734 OI_MEMBER_JOINED(oi
, MEMBER_DROUTERS
);
738 /* The interface should NOT belong to the OSPF-designated-routers group */
739 if (OI_MEMBER_CHECK(oi
, MEMBER_DROUTERS
))
741 /* drop only if last reference */
742 if (OI_MEMBER_COUNT(oi
, MEMBER_DROUTERS
) == 1)
743 ospf_if_drop_alldrouters(oi
->ospf
, oi
->address
, oi
->ifp
->ifindex
);
745 /* Unset the flag regardless of whether the system call to leave
746 the group succeeded, since it's much safer to assume that
747 we are not a member. */
748 OI_MEMBER_LEFT(oi
, MEMBER_DROUTERS
);
754 ospf_if_up (struct ospf_interface
*oi
)
759 if (oi
->type
== OSPF_IFTYPE_LOOPBACK
)
760 OSPF_ISM_EVENT_SCHEDULE (oi
, ISM_LoopInd
);
763 ospf_if_stream_set (oi
);
764 OSPF_ISM_EVENT_SCHEDULE (oi
, ISM_InterfaceUp
);
771 ospf_if_down (struct ospf_interface
*oi
)
776 OSPF_ISM_EVENT_EXECUTE (oi
, ISM_InterfaceDown
);
777 /* delete position in router LSA */
780 /* Shutdown packet reception and sending */
781 ospf_if_stream_unset (oi
);
787 /* Virtual Link related functions. */
789 struct ospf_vl_data
*
790 ospf_vl_data_new (struct ospf_area
*area
, struct in_addr vl_peer
)
792 struct ospf_vl_data
*vl_data
;
794 vl_data
= XCALLOC (MTYPE_OSPF_VL_DATA
, sizeof (struct ospf_vl_data
));
796 vl_data
->vl_peer
.s_addr
= vl_peer
.s_addr
;
797 vl_data
->vl_area_id
= area
->area_id
;
798 vl_data
->format
= area
->format
;
804 ospf_vl_data_free (struct ospf_vl_data
*vl_data
)
806 XFREE (MTYPE_OSPF_VL_DATA
, vl_data
);
809 u_int vlink_count
= 0;
811 struct ospf_interface
*
812 ospf_vl_new (struct ospf
*ospf
, struct ospf_vl_data
*vl_data
)
814 struct ospf_interface
* voi
;
815 struct interface
* vi
;
816 char ifname
[INTERFACE_NAMSIZ
+ 1];
817 struct ospf_area
*area
;
818 struct in_addr area_id
;
819 struct connected
*co
;
820 struct prefix_ipv4
*p
;
822 if (IS_DEBUG_OSPF_EVENT
)
823 zlog_debug ("ospf_vl_new(): Start");
824 if (vlink_count
== OSPF_VL_MAX_COUNT
)
826 if (IS_DEBUG_OSPF_EVENT
)
827 zlog_debug ("ospf_vl_new(): Alarm: "
828 "cannot create more than OSPF_MAX_VL_COUNT virtual links");
832 if (IS_DEBUG_OSPF_EVENT
)
833 zlog_debug ("ospf_vl_new(): creating pseudo zebra interface");
835 snprintf (ifname
, sizeof(ifname
), "VLINK%d", vlink_count
);
836 vi
= if_create (ifname
, strnlen(ifname
, sizeof(ifname
)), VRF_DEFAULT
);
838 * if_create sets ZEBRA_INTERFACE_LINKDETECTION
839 * virtual links don't need this.
841 UNSET_FLAG (vi
->status
, ZEBRA_INTERFACE_LINKDETECTION
);
842 co
= connected_new ();
844 listnode_add (vi
->connected
, co
);
846 p
= prefix_ipv4_new ();
848 p
->prefix
.s_addr
= 0;
851 co
->address
= (struct prefix
*)p
;
853 voi
= ospf_if_new (ospf
, vi
, co
->address
);
856 if (IS_DEBUG_OSPF_EVENT
)
857 zlog_debug ("ospf_vl_new(): Alarm: OSPF int structure is not created");
861 voi
->vl_data
= vl_data
;
862 voi
->ifp
->mtu
= OSPF_VL_MTU
;
863 voi
->type
= OSPF_IFTYPE_VIRTUALLINK
;
866 if (IS_DEBUG_OSPF_EVENT
)
867 zlog_debug ("ospf_vl_new(): Created name: %s", ifname
);
868 if (IS_DEBUG_OSPF_EVENT
)
869 zlog_debug ("ospf_vl_new(): set if->name to %s", vi
->name
);
872 area
= ospf_area_get (ospf
, area_id
, OSPF_AREA_ID_FORMAT_ADDRESS
);
875 if (IS_DEBUG_OSPF_EVENT
)
876 zlog_debug ("ospf_vl_new(): set associated area to the backbone");
878 /* Add pseudo neighbor. */
879 ospf_nbr_self_reset (voi
, voi
->ospf
->router_id
);
881 ospf_area_add_if (voi
->area
, voi
);
883 ospf_if_stream_set (voi
);
885 if (IS_DEBUG_OSPF_EVENT
)
886 zlog_debug ("ospf_vl_new(): Stop");
891 ospf_vl_if_delete (struct ospf_vl_data
*vl_data
)
893 struct interface
*ifp
= vl_data
->vl_oi
->ifp
;
894 vl_data
->vl_oi
->address
->u
.prefix4
.s_addr
= 0;
895 vl_data
->vl_oi
->address
->prefixlen
= 0;
896 ospf_if_free (vl_data
->vl_oi
);
901 /* Look up vl_data for given peer, optionally qualified to be in the
902 * specified area. NULL area returns first found..
904 struct ospf_vl_data
*
905 ospf_vl_lookup (struct ospf
*ospf
, struct ospf_area
*area
,
906 struct in_addr vl_peer
)
908 struct ospf_vl_data
*vl_data
;
909 struct listnode
*node
;
911 if (IS_DEBUG_OSPF_EVENT
)
913 zlog_debug ("%s: Looking for %s", __func__
, inet_ntoa (vl_peer
));
915 zlog_debug ("%s: in area %s", __func__
, inet_ntoa (area
->area_id
));
918 for (ALL_LIST_ELEMENTS_RO (ospf
->vlinks
, node
, vl_data
))
920 if (IS_DEBUG_OSPF_EVENT
)
921 zlog_debug ("%s: VL %s, peer %s", __func__
,
922 vl_data
->vl_oi
->ifp
->name
,
923 inet_ntoa (vl_data
->vl_peer
));
925 if (area
&& !IPV4_ADDR_SAME (&vl_data
->vl_area_id
, &area
->area_id
))
928 if (IPV4_ADDR_SAME (&vl_data
->vl_peer
, &vl_peer
))
936 ospf_vl_shutdown (struct ospf_vl_data
*vl_data
)
938 struct ospf_interface
*oi
;
940 if ((oi
= vl_data
->vl_oi
) == NULL
)
943 oi
->address
->u
.prefix4
.s_addr
= 0;
944 oi
->address
->prefixlen
= 0;
946 UNSET_FLAG (oi
->ifp
->flags
, IFF_UP
);
947 /* OSPF_ISM_EVENT_SCHEDULE (oi, ISM_InterfaceDown); */
948 OSPF_ISM_EVENT_EXECUTE (oi
, ISM_InterfaceDown
);
952 ospf_vl_add (struct ospf
*ospf
, struct ospf_vl_data
*vl_data
)
954 listnode_add (ospf
->vlinks
, vl_data
);
955 hook_call(ospf_vl_add
, vl_data
);
959 ospf_vl_delete (struct ospf
*ospf
, struct ospf_vl_data
*vl_data
)
961 ospf_vl_shutdown (vl_data
);
962 ospf_vl_if_delete (vl_data
);
964 hook_call(ospf_vl_delete
, vl_data
);
965 listnode_delete (ospf
->vlinks
, vl_data
);
967 ospf_vl_data_free (vl_data
);
971 ospf_vl_set_params (struct ospf_vl_data
*vl_data
, struct vertex
*v
)
974 struct ospf_interface
*voi
;
975 struct listnode
*node
;
976 struct vertex_parent
*vp
= NULL
;
978 struct router_lsa
*rl
;
980 voi
= vl_data
->vl_oi
;
982 if (voi
->output_cost
!= v
->distance
)
985 voi
->output_cost
= v
->distance
;
989 for (ALL_LIST_ELEMENTS_RO (v
->parents
, node
, vp
))
991 vl_data
->nexthop
.oi
= vp
->nexthop
->oi
;
992 vl_data
->nexthop
.router
= vp
->nexthop
->router
;
994 if (!IPV4_ADDR_SAME(&voi
->address
->u
.prefix4
,
995 &vl_data
->nexthop
.oi
->address
->u
.prefix4
))
998 voi
->address
->u
.prefix4
= vl_data
->nexthop
.oi
->address
->u
.prefix4
;
999 voi
->address
->prefixlen
= vl_data
->nexthop
.oi
->address
->prefixlen
;
1001 break; /* We take the first interface. */
1004 rl
= (struct router_lsa
*)v
->lsa
;
1006 /* use SPF determined backlink index in struct vertex
1007 * for virtual link destination address
1009 if (vp
&& vp
->backlink
>= 0)
1011 if (!IPV4_ADDR_SAME (&vl_data
->peer_addr
,
1012 &rl
->link
[vp
->backlink
].link_data
))
1014 vl_data
->peer_addr
= rl
->link
[vp
->backlink
].link_data
;
1018 /* This is highly odd, there is no backlink index
1019 * there should be due to the ospf_spf_has_link() check
1020 * in SPF. Lets warn and try pick a link anyway.
1022 zlog_warn ("ospf_vl_set_params: No backlink for %s!",
1023 vl_data
->vl_oi
->ifp
->name
);
1024 for (i
= 0; i
< ntohs (rl
->links
); i
++)
1026 switch (rl
->link
[i
].type
)
1028 case LSA_LINK_TYPE_VIRTUALLINK
:
1029 if (IS_DEBUG_OSPF_EVENT
)
1030 zlog_debug ("found back link through VL");
1031 case LSA_LINK_TYPE_TRANSIT
:
1032 case LSA_LINK_TYPE_POINTOPOINT
:
1033 if (!IPV4_ADDR_SAME (&vl_data
->peer_addr
,
1034 &rl
->link
[i
].link_data
))
1036 vl_data
->peer_addr
= rl
->link
[i
].link_data
;
1041 if (IS_DEBUG_OSPF_EVENT
)
1042 zlog_debug ("%s: %s peer address: %s, cost: %d,%schanged", __func__
,
1043 vl_data
->vl_oi
->ifp
->name
,
1044 inet_ntoa(vl_data
->peer_addr
),
1046 (changed
? " " : " un"));
1053 ospf_vl_up_check (struct ospf_area
*area
, struct in_addr rid
,
1056 struct ospf
*ospf
= area
->ospf
;
1057 struct listnode
*node
;
1058 struct ospf_vl_data
*vl_data
;
1059 struct ospf_interface
*oi
;
1061 if (IS_DEBUG_OSPF_EVENT
)
1063 zlog_debug ("ospf_vl_up_check(): Start");
1064 zlog_debug ("ospf_vl_up_check(): Router ID is %s", inet_ntoa (rid
));
1065 zlog_debug ("ospf_vl_up_check(): Area is %s", inet_ntoa (area
->area_id
));
1068 for (ALL_LIST_ELEMENTS_RO (ospf
->vlinks
, node
, vl_data
))
1070 if (IS_DEBUG_OSPF_EVENT
)
1072 zlog_debug ("%s: considering VL, %s in area %s", __func__
,
1073 vl_data
->vl_oi
->ifp
->name
,
1074 inet_ntoa (vl_data
->vl_area_id
));
1075 zlog_debug ("%s: peer ID: %s", __func__
,
1076 inet_ntoa (vl_data
->vl_peer
));
1079 if (IPV4_ADDR_SAME (&vl_data
->vl_peer
, &rid
) &&
1080 IPV4_ADDR_SAME (&vl_data
->vl_area_id
, &area
->area_id
))
1082 oi
= vl_data
->vl_oi
;
1083 SET_FLAG (vl_data
->flags
, OSPF_VL_FLAG_APPROVED
);
1085 if (IS_DEBUG_OSPF_EVENT
)
1086 zlog_debug ("ospf_vl_up_check(): this VL matched");
1088 if (oi
->state
== ISM_Down
)
1090 if (IS_DEBUG_OSPF_EVENT
)
1091 zlog_debug ("ospf_vl_up_check(): VL is down, waking it up");
1092 SET_FLAG (oi
->ifp
->flags
, IFF_UP
);
1093 OSPF_ISM_EVENT_EXECUTE(oi
,ISM_InterfaceUp
);
1096 if (ospf_vl_set_params (vl_data
, v
))
1098 if (IS_DEBUG_OSPF (ism
, ISM_EVENTS
))
1099 zlog_debug ("ospf_vl_up_check: VL cost change,"
1100 " scheduling router lsa refresh");
1102 ospf_router_lsa_update_area (ospf
->backbone
);
1103 else if (IS_DEBUG_OSPF (ism
, ISM_EVENTS
))
1104 zlog_debug ("ospf_vl_up_check: VL cost change, no backbone!");
1111 ospf_vl_unapprove (struct ospf
*ospf
)
1113 struct listnode
*node
;
1114 struct ospf_vl_data
*vl_data
;
1116 for (ALL_LIST_ELEMENTS_RO (ospf
->vlinks
, node
, vl_data
))
1117 UNSET_FLAG (vl_data
->flags
, OSPF_VL_FLAG_APPROVED
);
1121 ospf_vl_shut_unapproved (struct ospf
*ospf
)
1123 struct listnode
*node
, *nnode
;
1124 struct ospf_vl_data
*vl_data
;
1126 for (ALL_LIST_ELEMENTS (ospf
->vlinks
, node
, nnode
, vl_data
))
1127 if (!CHECK_FLAG (vl_data
->flags
, OSPF_VL_FLAG_APPROVED
))
1128 ospf_vl_shutdown (vl_data
);
1132 ospf_full_virtual_nbrs (struct ospf_area
*area
)
1134 if (IS_DEBUG_OSPF_EVENT
)
1136 zlog_debug ("counting fully adjacent virtual neighbors in area %s",
1137 inet_ntoa (area
->area_id
));
1138 zlog_debug ("there are %d of them", area
->full_vls
);
1141 return area
->full_vls
;
1145 ospf_vls_in_area (struct ospf_area
*area
)
1147 struct listnode
*node
;
1148 struct ospf_vl_data
*vl_data
;
1151 for (ALL_LIST_ELEMENTS_RO (area
->ospf
->vlinks
, node
, vl_data
))
1152 if (IPV4_ADDR_SAME (&vl_data
->vl_area_id
, &area
->area_id
))
1160 ospf_crypt_key_new ()
1162 return XCALLOC (MTYPE_OSPF_CRYPT_KEY
, sizeof (struct crypt_key
));
1166 ospf_crypt_key_add (struct list
*crypt
, struct crypt_key
*ck
)
1168 listnode_add (crypt
, ck
);
1172 ospf_crypt_key_lookup (struct list
*auth_crypt
, u_char key_id
)
1174 struct listnode
*node
;
1175 struct crypt_key
*ck
;
1177 for (ALL_LIST_ELEMENTS_RO (auth_crypt
, node
, ck
))
1178 if (ck
->key_id
== key_id
)
1185 ospf_crypt_key_delete (struct list
*auth_crypt
, u_char key_id
)
1187 struct listnode
*node
, *nnode
;
1188 struct crypt_key
*ck
;
1190 for (ALL_LIST_ELEMENTS (auth_crypt
, node
, nnode
, ck
))
1192 if (ck
->key_id
== key_id
)
1194 listnode_delete (auth_crypt
, ck
);
1195 XFREE (MTYPE_OSPF_CRYPT_KEY
, ck
);
1204 ospf_default_iftype(struct interface
*ifp
)
1206 if (if_is_pointopoint (ifp
))
1207 return OSPF_IFTYPE_POINTOPOINT
;
1208 else if (if_is_loopback (ifp
))
1209 return OSPF_IFTYPE_LOOPBACK
;
1211 return OSPF_IFTYPE_BROADCAST
;
1217 /* Initialize Zebra interface data structure. */
1218 om
->iflist
= vrf_iflist (VRF_DEFAULT
);
1219 if_add_hook (IF_NEW_HOOK
, ospf_if_new_hook
);
1220 if_add_hook (IF_DELETE_HOOK
, ospf_if_delete_hook
);