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 #include "ospfd/ospf_snmp.h"
52 #endif /* HAVE_SNMP */
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 /* Add pseudo neighbor. */
238 oi
->nbr_self
= ospf_nbr_new (oi
);
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 #ifdef HAVE_OPAQUE_LSA
247 ospf_opaque_type9_lsa_init (oi
);
248 #endif /* HAVE_OPAQUE_LSA */
255 /* Restore an interface to its pre UP state
256 Used from ism_interface_down only */
258 ospf_if_cleanup (struct ospf_interface
*oi
)
260 struct route_node
*rn
;
261 struct listnode
*node
, *nnode
;
262 struct ospf_neighbor
*nbr
;
263 struct ospf_nbr_nbma
*nbr_nbma
;
264 struct ospf_lsa
*lsa
;
266 /* oi->nbrs and oi->nbr_nbma should be deleted on InterfaceDown event */
267 /* delete all static neighbors attached to this interface */
268 for (ALL_LIST_ELEMENTS (oi
->nbr_nbma
, node
, nnode
, nbr_nbma
))
270 OSPF_POLL_TIMER_OFF (nbr_nbma
->t_poll
);
274 nbr_nbma
->nbr
->nbr_nbma
= NULL
;
275 nbr_nbma
->nbr
= NULL
;
280 listnode_delete (oi
->nbr_nbma
, nbr_nbma
);
283 /* send Neighbor event KillNbr to all associated neighbors. */
284 for (rn
= route_top (oi
->nbrs
); rn
; rn
= route_next (rn
))
285 if ((nbr
= rn
->info
) != NULL
)
286 if (nbr
!= oi
->nbr_self
)
287 OSPF_NSM_EVENT_EXECUTE (nbr
, NSM_KillNbr
);
289 /* Cleanup Link State Acknowlegdment list. */
290 for (ALL_LIST_ELEMENTS (oi
->ls_ack
, node
, nnode
, lsa
))
291 ospf_lsa_unlock (&lsa
); /* oi->ls_ack */
292 list_delete_all_node (oi
->ls_ack
);
294 oi
->crypt_seqnum
= 0;
296 /* Empty link state update queue */
297 ospf_ls_upd_queue_empty (oi
);
299 /* Reset pseudo neighbor. */
300 ospf_nbr_delete (oi
->nbr_self
);
301 oi
->nbr_self
= ospf_nbr_new (oi
);
302 ospf_nbr_add_self (oi
);
306 ospf_if_free (struct ospf_interface
*oi
)
310 assert (oi
->state
== ISM_Down
);
312 #ifdef HAVE_OPAQUE_LSA
313 ospf_opaque_type9_lsa_term (oi
);
314 #endif /* HAVE_OPAQUE_LSA */
316 /* Free Pseudo Neighbour */
317 ospf_nbr_delete (oi
->nbr_self
);
319 route_table_finish (oi
->nbrs
);
320 route_table_finish (oi
->ls_upd_queue
);
322 /* Free any lists that should be freed */
323 list_free (oi
->nbr_nbma
);
325 list_free (oi
->ls_ack
);
326 list_free (oi
->ls_ack_direct
.ls_ack
);
328 ospf_delete_from_if (oi
->ifp
, oi
);
330 listnode_delete (oi
->ospf
->oiflist
, oi
);
331 listnode_delete (oi
->area
->oiflist
, oi
);
333 thread_cancel_event (master
, oi
);
335 memset (oi
, 0, sizeof (*oi
));
336 XFREE (MTYPE_OSPF_IF
, oi
);
341 * check if interface with given address is configured and
342 * return it if yes. special treatment for PtP networks.
344 struct ospf_interface
*
345 ospf_if_is_configured (struct ospf
*ospf
, struct in_addr
*address
)
347 struct listnode
*node
, *nnode
;
348 struct ospf_interface
*oi
;
349 struct prefix_ipv4 addr
;
351 addr
.family
= AF_INET
;
352 addr
.prefix
= *address
;
353 addr
.prefixlen
= IPV4_MAX_PREFIXLEN
;
355 for (ALL_LIST_ELEMENTS (ospf
->oiflist
, node
, nnode
, oi
))
356 if (oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
)
358 if (CHECK_FLAG(oi
->connected
->flags
, ZEBRA_IFA_UNNUMBERED
))
360 if (htonl(oi
->ifp
->ifindex
) == address
->s_addr
)
363 else if (oi
->type
== OSPF_IFTYPE_POINTOPOINT
)
365 /* special leniency: match if addr is anywhere on peer subnet */
366 if (prefix_match(CONNECTED_PREFIX(oi
->connected
),
367 (struct prefix
*)&addr
))
372 if (IPV4_ADDR_SAME (address
, &oi
->address
->u
.prefix4
))
380 ospf_if_is_up (struct ospf_interface
*oi
)
382 return if_is_up (oi
->ifp
);
385 struct ospf_interface
*
386 ospf_if_exists (struct ospf_interface
*oic
)
388 struct listnode
*node
;
390 struct ospf_interface
*oi
;
392 if ((ospf
= ospf_lookup ()) == NULL
)
395 for (ALL_LIST_ELEMENTS_RO (ospf
->oiflist
, node
, oi
))
402 /* Lookup OSPF interface by router LSA posistion */
403 struct ospf_interface
*
404 ospf_if_lookup_by_lsa_pos (struct ospf_area
*area
, int lsa_pos
)
406 struct listnode
*node
;
407 struct ospf_interface
*oi
;
409 for (ALL_LIST_ELEMENTS_RO (area
->oiflist
, node
, oi
))
411 if (lsa_pos
>= oi
->lsa_pos_beg
&& lsa_pos
< oi
->lsa_pos_end
)
417 struct ospf_interface
*
418 ospf_if_lookup_by_local_addr (struct ospf
*ospf
,
419 struct interface
*ifp
, struct in_addr address
)
421 struct listnode
*node
;
422 struct ospf_interface
*oi
;
424 for (ALL_LIST_ELEMENTS_RO (ospf
->oiflist
, node
, oi
))
425 if (oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
)
427 if (ifp
&& oi
->ifp
!= ifp
)
430 if (IPV4_ADDR_SAME (&address
, &oi
->address
->u
.prefix4
))
437 struct ospf_interface
*
438 ospf_if_lookup_by_prefix (struct ospf
*ospf
, struct prefix_ipv4
*p
)
440 struct listnode
*node
;
441 struct ospf_interface
*oi
;
443 /* Check each Interface. */
444 for (ALL_LIST_ELEMENTS_RO (ospf
->oiflist
, node
, oi
))
446 if (oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
)
450 prefix_copy (&ptmp
, CONNECTED_PREFIX(oi
->connected
));
452 if (prefix_same (&ptmp
, (struct prefix
*) p
))
459 /* determine receiving interface by ifp and source address */
460 struct ospf_interface
*
461 ospf_if_lookup_recv_if (struct ospf
*ospf
, struct in_addr src
,
462 struct interface
*ifp
)
464 struct route_node
*rn
;
465 struct prefix_ipv4 addr
;
466 struct ospf_interface
*oi
, *match
;
468 addr
.family
= AF_INET
;
470 addr
.prefixlen
= IPV4_MAX_BITLEN
;
474 for (rn
= route_top (IF_OIFS (ifp
)); rn
; rn
= route_next (rn
))
478 if (!oi
) /* oi can be NULL for PtP aliases */
481 if (oi
->type
== OSPF_IFTYPE_VIRTUALLINK
)
484 if (if_is_loopback (oi
->ifp
))
487 if (CHECK_FLAG(oi
->connected
->flags
, ZEBRA_IFA_UNNUMBERED
))
489 else if (prefix_match (CONNECTED_PREFIX(oi
->connected
),
490 (struct prefix
*) &addr
))
492 if ( (match
== NULL
) ||
493 (match
->address
->prefixlen
< oi
->address
->prefixlen
)
503 ospf_if_stream_set (struct ospf_interface
*oi
)
505 /* set output fifo queue. */
506 if (oi
->obuf
== NULL
)
507 oi
->obuf
= ospf_fifo_new ();
511 ospf_if_stream_unset (struct ospf_interface
*oi
)
513 struct ospf
*ospf
= oi
->ospf
;
517 ospf_fifo_free (oi
->obuf
);
522 listnode_delete (ospf
->oi_write_q
, oi
);
523 if (list_isempty(ospf
->oi_write_q
))
524 OSPF_TIMER_OFF (ospf
->t_write
);
531 static struct ospf_if_params
*
532 ospf_new_if_params (void)
534 struct ospf_if_params
*oip
;
536 oip
= XCALLOC (MTYPE_OSPF_IF_PARAMS
, sizeof (struct ospf_if_params
));
541 UNSET_IF_PARAM (oip
, output_cost_cmd
);
542 UNSET_IF_PARAM (oip
, transmit_delay
);
543 UNSET_IF_PARAM (oip
, retransmit_interval
);
544 UNSET_IF_PARAM (oip
, passive_interface
);
545 UNSET_IF_PARAM (oip
, v_hello
);
546 UNSET_IF_PARAM (oip
, fast_hello
);
547 UNSET_IF_PARAM (oip
, v_wait
);
548 UNSET_IF_PARAM (oip
, priority
);
549 UNSET_IF_PARAM (oip
, type
);
550 UNSET_IF_PARAM (oip
, auth_simple
);
551 UNSET_IF_PARAM (oip
, auth_crypt
);
552 UNSET_IF_PARAM (oip
, auth_type
);
554 oip
->auth_crypt
= list_new ();
556 oip
->network_lsa_seqnum
= htonl(OSPF_INITIAL_SEQUENCE_NUMBER
);
562 ospf_del_if_params (struct ospf_if_params
*oip
)
564 list_delete (oip
->auth_crypt
);
565 bfd_info_free(&(oip
->bfd_info
));
566 XFREE (MTYPE_OSPF_IF_PARAMS
, oip
);
570 ospf_free_if_params (struct interface
*ifp
, struct in_addr addr
)
572 struct ospf_if_params
*oip
;
573 struct prefix_ipv4 p
;
574 struct route_node
*rn
;
577 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
579 rn
= route_node_lookup (IF_OIFS_PARAMS (ifp
), (struct prefix
*)&p
);
580 if (!rn
|| !rn
->info
)
584 route_unlock_node (rn
);
586 if (!OSPF_IF_PARAM_CONFIGURED (oip
, output_cost_cmd
) &&
587 !OSPF_IF_PARAM_CONFIGURED (oip
, transmit_delay
) &&
588 !OSPF_IF_PARAM_CONFIGURED (oip
, retransmit_interval
) &&
589 !OSPF_IF_PARAM_CONFIGURED (oip
, passive_interface
) &&
590 !OSPF_IF_PARAM_CONFIGURED (oip
, v_hello
) &&
591 !OSPF_IF_PARAM_CONFIGURED (oip
, fast_hello
) &&
592 !OSPF_IF_PARAM_CONFIGURED (oip
, v_wait
) &&
593 !OSPF_IF_PARAM_CONFIGURED (oip
, priority
) &&
594 !OSPF_IF_PARAM_CONFIGURED (oip
, type
) &&
595 !OSPF_IF_PARAM_CONFIGURED (oip
, auth_simple
) &&
596 !OSPF_IF_PARAM_CONFIGURED (oip
, auth_type
) &&
597 listcount (oip
->auth_crypt
) == 0 &&
598 ntohl (oip
->network_lsa_seqnum
) != OSPF_INITIAL_SEQUENCE_NUMBER
)
600 ospf_del_if_params (oip
);
602 route_unlock_node (rn
);
606 struct ospf_if_params
*
607 ospf_lookup_if_params (struct interface
*ifp
, struct in_addr addr
)
609 struct prefix_ipv4 p
;
610 struct route_node
*rn
;
613 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
616 rn
= route_node_lookup (IF_OIFS_PARAMS (ifp
), (struct prefix
*)&p
);
620 route_unlock_node (rn
);
627 struct ospf_if_params
*
628 ospf_get_if_params (struct interface
*ifp
, struct in_addr addr
)
630 struct prefix_ipv4 p
;
631 struct route_node
*rn
;
634 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
637 rn
= route_node_get (IF_OIFS_PARAMS (ifp
), (struct prefix
*)&p
);
639 if (rn
->info
== NULL
)
640 rn
->info
= ospf_new_if_params ();
642 route_unlock_node (rn
);
648 ospf_if_update_params (struct interface
*ifp
, struct in_addr addr
)
650 struct route_node
*rn
;
651 struct ospf_interface
*oi
;
653 for (rn
= route_top (IF_OIFS (ifp
)); rn
; rn
= route_next (rn
))
655 if ((oi
= rn
->info
) == NULL
)
658 if (IPV4_ADDR_SAME (&oi
->address
->u
.prefix4
, &addr
))
659 oi
->params
= ospf_lookup_if_params (ifp
, oi
->address
->u
.prefix4
);
664 ospf_if_new_hook (struct interface
*ifp
)
668 ifp
->info
= XCALLOC (MTYPE_OSPF_IF_INFO
, sizeof (struct ospf_if_info
));
670 IF_OIFS (ifp
) = route_table_init ();
671 IF_OIFS_PARAMS (ifp
) = route_table_init ();
673 IF_DEF_PARAMS (ifp
) = ospf_new_if_params ();
675 SET_IF_PARAM (IF_DEF_PARAMS (ifp
), transmit_delay
);
676 IF_DEF_PARAMS (ifp
)->transmit_delay
= OSPF_TRANSMIT_DELAY_DEFAULT
;
678 SET_IF_PARAM (IF_DEF_PARAMS (ifp
), retransmit_interval
);
679 IF_DEF_PARAMS (ifp
)->retransmit_interval
= OSPF_RETRANSMIT_INTERVAL_DEFAULT
;
681 SET_IF_PARAM (IF_DEF_PARAMS (ifp
), priority
);
682 IF_DEF_PARAMS (ifp
)->priority
= OSPF_ROUTER_PRIORITY_DEFAULT
;
684 IF_DEF_PARAMS (ifp
)->mtu_ignore
= OSPF_MTU_IGNORE_DEFAULT
;
686 SET_IF_PARAM (IF_DEF_PARAMS (ifp
), v_hello
);
687 IF_DEF_PARAMS (ifp
)->v_hello
= OSPF_HELLO_INTERVAL_DEFAULT
;
689 SET_IF_PARAM (IF_DEF_PARAMS (ifp
), fast_hello
);
690 IF_DEF_PARAMS (ifp
)->fast_hello
= OSPF_FAST_HELLO_DEFAULT
;
692 SET_IF_PARAM (IF_DEF_PARAMS (ifp
), v_wait
);
693 IF_DEF_PARAMS (ifp
)->v_wait
= OSPF_ROUTER_DEAD_INTERVAL_DEFAULT
;
695 SET_IF_PARAM (IF_DEF_PARAMS (ifp
), auth_simple
);
696 memset (IF_DEF_PARAMS (ifp
)->auth_simple
, 0, OSPF_AUTH_SIMPLE_SIZE
);
698 SET_IF_PARAM (IF_DEF_PARAMS (ifp
), auth_type
);
699 IF_DEF_PARAMS (ifp
)->auth_type
= OSPF_AUTH_NOTSET
;
701 #ifdef HAVE_OPAQUE_LSA
702 rc
= ospf_opaque_new_if (ifp
);
703 #endif /* HAVE_OPAQUE_LSA */
708 ospf_if_delete_hook (struct interface
*ifp
)
711 struct route_node
*rn
;
712 #ifdef HAVE_OPAQUE_LSA
713 rc
= ospf_opaque_del_if (ifp
);
714 #endif /* HAVE_OPAQUE_LSA */
716 route_table_finish (IF_OIFS (ifp
));
718 for (rn
= route_top (IF_OIFS_PARAMS (ifp
)); rn
; rn
= route_next (rn
))
720 ospf_del_if_params (rn
->info
);
721 route_table_finish (IF_OIFS_PARAMS (ifp
));
723 ospf_del_if_params ((struct ospf_if_params
*) IF_DEF_PARAMS (ifp
));
724 XFREE (MTYPE_OSPF_IF_INFO
, ifp
->info
);
731 ospf_if_is_enable (struct ospf_interface
*oi
)
733 if (!if_is_loopback (oi
->ifp
))
734 if (if_is_up (oi
->ifp
))
741 ospf_if_set_multicast(struct ospf_interface
*oi
)
743 if ((oi
->state
> ISM_Loopback
) &&
744 (oi
->type
!= OSPF_IFTYPE_LOOPBACK
) &&
745 (oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
) &&
746 (OSPF_IF_PASSIVE_STATUS(oi
) == OSPF_IF_ACTIVE
))
748 /* The interface should belong to the OSPF-all-routers group. */
749 if (!OI_MEMBER_CHECK(oi
, MEMBER_ALLROUTERS
) &&
750 (ospf_if_add_allspfrouters(oi
->ospf
, oi
->address
,
751 oi
->ifp
->ifindex
) >= 0))
752 /* Set the flag only if the system call to join succeeded. */
753 OI_MEMBER_JOINED(oi
, MEMBER_ALLROUTERS
);
757 /* The interface should NOT belong to the OSPF-all-routers group. */
758 if (OI_MEMBER_CHECK(oi
, MEMBER_ALLROUTERS
))
760 /* Only actually drop if this is the last reference */
761 if (OI_MEMBER_COUNT(oi
, MEMBER_ALLROUTERS
) == 1)
762 ospf_if_drop_allspfrouters (oi
->ospf
, oi
->address
,
764 /* Unset the flag regardless of whether the system call to leave
765 the group succeeded, since it's much safer to assume that
766 we are not a member. */
767 OI_MEMBER_LEFT(oi
,MEMBER_ALLROUTERS
);
771 if (((oi
->type
== OSPF_IFTYPE_BROADCAST
) ||
772 (oi
->type
== OSPF_IFTYPE_POINTOPOINT
)) &&
773 ((oi
->state
== ISM_DR
) || (oi
->state
== ISM_Backup
)) &&
774 (OSPF_IF_PASSIVE_STATUS(oi
) == OSPF_IF_ACTIVE
))
776 /* The interface should belong to the OSPF-designated-routers group. */
777 if (!OI_MEMBER_CHECK(oi
, MEMBER_DROUTERS
) &&
778 (ospf_if_add_alldrouters(oi
->ospf
, oi
->address
,
779 oi
->ifp
->ifindex
) >= 0))
780 /* Set the flag only if the system call to join succeeded. */
781 OI_MEMBER_JOINED(oi
, MEMBER_DROUTERS
);
785 /* The interface should NOT belong to the OSPF-designated-routers group */
786 if (OI_MEMBER_CHECK(oi
, MEMBER_DROUTERS
))
788 /* drop only if last reference */
789 if (OI_MEMBER_COUNT(oi
, MEMBER_DROUTERS
) == 1)
790 ospf_if_drop_alldrouters(oi
->ospf
, oi
->address
, oi
->ifp
->ifindex
);
792 /* Unset the flag regardless of whether the system call to leave
793 the group succeeded, since it's much safer to assume that
794 we are not a member. */
795 OI_MEMBER_LEFT(oi
, MEMBER_DROUTERS
);
801 ospf_if_up (struct ospf_interface
*oi
)
806 if (oi
->type
== OSPF_IFTYPE_LOOPBACK
)
807 OSPF_ISM_EVENT_SCHEDULE (oi
, ISM_LoopInd
);
810 struct ospf
*ospf
= ospf_lookup ();
812 ospf_adjust_sndbuflen (ospf
, oi
->ifp
->mtu
);
814 zlog_warn ("%s: ospf_lookup() returned NULL", __func__
);
815 ospf_if_stream_set (oi
);
816 OSPF_ISM_EVENT_SCHEDULE (oi
, ISM_InterfaceUp
);
823 ospf_if_down (struct ospf_interface
*oi
)
828 OSPF_ISM_EVENT_EXECUTE (oi
, ISM_InterfaceDown
);
829 /* delete position in router LSA */
832 /* Shutdown packet reception and sending */
833 ospf_if_stream_unset (oi
);
839 /* Virtual Link related functions. */
841 struct ospf_vl_data
*
842 ospf_vl_data_new (struct ospf_area
*area
, struct in_addr vl_peer
)
844 struct ospf_vl_data
*vl_data
;
846 vl_data
= XCALLOC (MTYPE_OSPF_VL_DATA
, sizeof (struct ospf_vl_data
));
848 vl_data
->vl_peer
.s_addr
= vl_peer
.s_addr
;
849 vl_data
->vl_area_id
= area
->area_id
;
850 vl_data
->format
= area
->format
;
856 ospf_vl_data_free (struct ospf_vl_data
*vl_data
)
858 XFREE (MTYPE_OSPF_VL_DATA
, vl_data
);
861 u_int vlink_count
= 0;
863 struct ospf_interface
*
864 ospf_vl_new (struct ospf
*ospf
, struct ospf_vl_data
*vl_data
)
866 struct ospf_interface
* voi
;
867 struct interface
* vi
;
868 char ifname
[INTERFACE_NAMSIZ
+ 1];
869 struct ospf_area
*area
;
870 struct in_addr area_id
;
871 struct connected
*co
;
872 struct prefix_ipv4
*p
;
874 if (IS_DEBUG_OSPF_EVENT
)
875 zlog_debug ("ospf_vl_new(): Start");
876 if (vlink_count
== OSPF_VL_MAX_COUNT
)
878 if (IS_DEBUG_OSPF_EVENT
)
879 zlog_debug ("ospf_vl_new(): Alarm: "
880 "cannot create more than OSPF_MAX_VL_COUNT virtual links");
884 if (IS_DEBUG_OSPF_EVENT
)
885 zlog_debug ("ospf_vl_new(): creating pseudo zebra interface");
887 snprintf (ifname
, sizeof(ifname
), "VLINK%d", vlink_count
);
888 vi
= if_create (ifname
, strnlen(ifname
, sizeof(ifname
)));
890 * if_create sets ZEBRA_INTERFACE_LINKDETECTION
891 * virtual links don't need this.
893 UNSET_FLAG (vi
->status
, ZEBRA_INTERFACE_LINKDETECTION
);
894 co
= connected_new ();
896 listnode_add (vi
->connected
, co
);
898 p
= prefix_ipv4_new ();
900 p
->prefix
.s_addr
= 0;
903 co
->address
= (struct prefix
*)p
;
905 voi
= ospf_if_new (ospf
, vi
, co
->address
);
908 if (IS_DEBUG_OSPF_EVENT
)
909 zlog_debug ("ospf_vl_new(): Alarm: OSPF int structure is not created");
913 voi
->vl_data
= vl_data
;
914 voi
->ifp
->mtu
= OSPF_VL_MTU
;
915 voi
->type
= OSPF_IFTYPE_VIRTUALLINK
;
918 if (IS_DEBUG_OSPF_EVENT
)
919 zlog_debug ("ospf_vl_new(): Created name: %s", ifname
);
920 if (IS_DEBUG_OSPF_EVENT
)
921 zlog_debug ("ospf_vl_new(): set if->name to %s", vi
->name
);
924 area
= ospf_area_get (ospf
, area_id
, OSPF_AREA_ID_FORMAT_ADDRESS
);
927 if (IS_DEBUG_OSPF_EVENT
)
928 zlog_debug ("ospf_vl_new(): set associated area to the backbone");
930 ospf_nbr_add_self (voi
);
931 ospf_area_add_if (voi
->area
, voi
);
933 ospf_if_stream_set (voi
);
935 if (IS_DEBUG_OSPF_EVENT
)
936 zlog_debug ("ospf_vl_new(): Stop");
941 ospf_vl_if_delete (struct ospf_vl_data
*vl_data
)
943 struct interface
*ifp
= vl_data
->vl_oi
->ifp
;
944 vl_data
->vl_oi
->address
->u
.prefix4
.s_addr
= 0;
945 vl_data
->vl_oi
->address
->prefixlen
= 0;
946 ospf_if_free (vl_data
->vl_oi
);
951 /* Look up vl_data for given peer, optionally qualified to be in the
952 * specified area. NULL area returns first found..
954 struct ospf_vl_data
*
955 ospf_vl_lookup (struct ospf
*ospf
, struct ospf_area
*area
,
956 struct in_addr vl_peer
)
958 struct ospf_vl_data
*vl_data
;
959 struct listnode
*node
;
961 if (IS_DEBUG_OSPF_EVENT
)
963 zlog_debug ("%s: Looking for %s", __func__
, inet_ntoa (vl_peer
));
965 zlog_debug ("%s: in area %s", __func__
, inet_ntoa (area
->area_id
));
968 for (ALL_LIST_ELEMENTS_RO (ospf
->vlinks
, node
, vl_data
))
970 if (IS_DEBUG_OSPF_EVENT
)
971 zlog_debug ("%s: VL %s, peer %s", __func__
,
972 vl_data
->vl_oi
->ifp
->name
,
973 inet_ntoa (vl_data
->vl_peer
));
975 if (area
&& !IPV4_ADDR_SAME (&vl_data
->vl_area_id
, &area
->area_id
))
978 if (IPV4_ADDR_SAME (&vl_data
->vl_peer
, &vl_peer
))
986 ospf_vl_shutdown (struct ospf_vl_data
*vl_data
)
988 struct ospf_interface
*oi
;
990 if ((oi
= vl_data
->vl_oi
) == NULL
)
993 oi
->address
->u
.prefix4
.s_addr
= 0;
994 oi
->address
->prefixlen
= 0;
996 UNSET_FLAG (oi
->ifp
->flags
, IFF_UP
);
997 /* OSPF_ISM_EVENT_SCHEDULE (oi, ISM_InterfaceDown); */
998 OSPF_ISM_EVENT_EXECUTE (oi
, ISM_InterfaceDown
);
1002 ospf_vl_add (struct ospf
*ospf
, struct ospf_vl_data
*vl_data
)
1004 listnode_add (ospf
->vlinks
, vl_data
);
1006 ospf_snmp_vl_add (vl_data
);
1007 #endif /* HAVE_SNMP */
1011 ospf_vl_delete (struct ospf
*ospf
, struct ospf_vl_data
*vl_data
)
1013 ospf_vl_shutdown (vl_data
);
1014 ospf_vl_if_delete (vl_data
);
1017 ospf_snmp_vl_delete (vl_data
);
1018 #endif /* HAVE_SNMP */
1019 listnode_delete (ospf
->vlinks
, vl_data
);
1021 ospf_vl_data_free (vl_data
);
1025 ospf_vl_set_params (struct ospf_vl_data
*vl_data
, struct vertex
*v
)
1028 struct ospf_interface
*voi
;
1029 struct listnode
*node
;
1030 struct vertex_parent
*vp
= NULL
;
1032 struct router_lsa
*rl
;
1034 voi
= vl_data
->vl_oi
;
1036 if (voi
->output_cost
!= v
->distance
)
1039 voi
->output_cost
= v
->distance
;
1043 for (ALL_LIST_ELEMENTS_RO (v
->parents
, node
, vp
))
1045 vl_data
->nexthop
.oi
= vp
->nexthop
->oi
;
1046 vl_data
->nexthop
.router
= vp
->nexthop
->router
;
1048 if (!IPV4_ADDR_SAME(&voi
->address
->u
.prefix4
,
1049 &vl_data
->nexthop
.oi
->address
->u
.prefix4
))
1052 voi
->address
->u
.prefix4
= vl_data
->nexthop
.oi
->address
->u
.prefix4
;
1053 voi
->address
->prefixlen
= vl_data
->nexthop
.oi
->address
->prefixlen
;
1055 break; /* We take the first interface. */
1058 rl
= (struct router_lsa
*)v
->lsa
;
1060 /* use SPF determined backlink index in struct vertex
1061 * for virtual link destination address
1063 if (vp
&& vp
->backlink
>= 0)
1065 if (!IPV4_ADDR_SAME (&vl_data
->peer_addr
,
1066 &rl
->link
[vp
->backlink
].link_data
))
1068 vl_data
->peer_addr
= rl
->link
[vp
->backlink
].link_data
;
1072 /* This is highly odd, there is no backlink index
1073 * there should be due to the ospf_spf_has_link() check
1074 * in SPF. Lets warn and try pick a link anyway.
1076 zlog_warn ("ospf_vl_set_params: No backlink for %s!",
1077 vl_data
->vl_oi
->ifp
->name
);
1078 for (i
= 0; i
< ntohs (rl
->links
); i
++)
1080 switch (rl
->link
[i
].type
)
1082 case LSA_LINK_TYPE_VIRTUALLINK
:
1083 if (IS_DEBUG_OSPF_EVENT
)
1084 zlog_debug ("found back link through VL");
1085 case LSA_LINK_TYPE_TRANSIT
:
1086 case LSA_LINK_TYPE_POINTOPOINT
:
1087 if (!IPV4_ADDR_SAME (&vl_data
->peer_addr
,
1088 &rl
->link
[i
].link_data
))
1090 vl_data
->peer_addr
= rl
->link
[i
].link_data
;
1095 if (IS_DEBUG_OSPF_EVENT
)
1096 zlog_debug ("%s: %s peer address: %s, cost: %d,%schanged", __func__
,
1097 vl_data
->vl_oi
->ifp
->name
,
1098 inet_ntoa(vl_data
->peer_addr
),
1100 (changed
? " " : " un"));
1107 ospf_vl_up_check (struct ospf_area
*area
, struct in_addr rid
,
1110 struct ospf
*ospf
= area
->ospf
;
1111 struct listnode
*node
;
1112 struct ospf_vl_data
*vl_data
;
1113 struct ospf_interface
*oi
;
1115 if (IS_DEBUG_OSPF_EVENT
)
1117 zlog_debug ("ospf_vl_up_check(): Start");
1118 zlog_debug ("ospf_vl_up_check(): Router ID is %s", inet_ntoa (rid
));
1119 zlog_debug ("ospf_vl_up_check(): Area is %s", inet_ntoa (area
->area_id
));
1122 for (ALL_LIST_ELEMENTS_RO (ospf
->vlinks
, node
, vl_data
))
1124 if (IS_DEBUG_OSPF_EVENT
)
1126 zlog_debug ("%s: considering VL, %s in area %s", __func__
,
1127 vl_data
->vl_oi
->ifp
->name
,
1128 inet_ntoa (vl_data
->vl_area_id
));
1129 zlog_debug ("%s: peer ID: %s", __func__
,
1130 inet_ntoa (vl_data
->vl_peer
));
1133 if (IPV4_ADDR_SAME (&vl_data
->vl_peer
, &rid
) &&
1134 IPV4_ADDR_SAME (&vl_data
->vl_area_id
, &area
->area_id
))
1136 oi
= vl_data
->vl_oi
;
1137 SET_FLAG (vl_data
->flags
, OSPF_VL_FLAG_APPROVED
);
1139 if (IS_DEBUG_OSPF_EVENT
)
1140 zlog_debug ("ospf_vl_up_check(): this VL matched");
1142 if (oi
->state
== ISM_Down
)
1144 if (IS_DEBUG_OSPF_EVENT
)
1145 zlog_debug ("ospf_vl_up_check(): VL is down, waking it up");
1146 SET_FLAG (oi
->ifp
->flags
, IFF_UP
);
1147 OSPF_ISM_EVENT_EXECUTE(oi
,ISM_InterfaceUp
);
1150 if (ospf_vl_set_params (vl_data
, v
))
1152 if (IS_DEBUG_OSPF (ism
, ISM_EVENTS
))
1153 zlog_debug ("ospf_vl_up_check: VL cost change,"
1154 " scheduling router lsa refresh");
1156 ospf_router_lsa_update_area (ospf
->backbone
);
1157 else if (IS_DEBUG_OSPF (ism
, ISM_EVENTS
))
1158 zlog_debug ("ospf_vl_up_check: VL cost change, no backbone!");
1165 ospf_vl_unapprove (struct ospf
*ospf
)
1167 struct listnode
*node
;
1168 struct ospf_vl_data
*vl_data
;
1170 for (ALL_LIST_ELEMENTS_RO (ospf
->vlinks
, node
, vl_data
))
1171 UNSET_FLAG (vl_data
->flags
, OSPF_VL_FLAG_APPROVED
);
1175 ospf_vl_shut_unapproved (struct ospf
*ospf
)
1177 struct listnode
*node
, *nnode
;
1178 struct ospf_vl_data
*vl_data
;
1180 for (ALL_LIST_ELEMENTS (ospf
->vlinks
, node
, nnode
, vl_data
))
1181 if (!CHECK_FLAG (vl_data
->flags
, OSPF_VL_FLAG_APPROVED
))
1182 ospf_vl_shutdown (vl_data
);
1186 ospf_full_virtual_nbrs (struct ospf_area
*area
)
1188 if (IS_DEBUG_OSPF_EVENT
)
1190 zlog_debug ("counting fully adjacent virtual neighbors in area %s",
1191 inet_ntoa (area
->area_id
));
1192 zlog_debug ("there are %d of them", area
->full_vls
);
1195 return area
->full_vls
;
1199 ospf_vls_in_area (struct ospf_area
*area
)
1201 struct listnode
*node
;
1202 struct ospf_vl_data
*vl_data
;
1205 for (ALL_LIST_ELEMENTS_RO (area
->ospf
->vlinks
, node
, vl_data
))
1206 if (IPV4_ADDR_SAME (&vl_data
->vl_area_id
, &area
->area_id
))
1214 ospf_crypt_key_new ()
1216 return XCALLOC (MTYPE_OSPF_CRYPT_KEY
, sizeof (struct crypt_key
));
1220 ospf_crypt_key_add (struct list
*crypt
, struct crypt_key
*ck
)
1222 listnode_add (crypt
, ck
);
1226 ospf_crypt_key_lookup (struct list
*auth_crypt
, u_char key_id
)
1228 struct listnode
*node
;
1229 struct crypt_key
*ck
;
1231 for (ALL_LIST_ELEMENTS_RO (auth_crypt
, node
, ck
))
1232 if (ck
->key_id
== key_id
)
1239 ospf_crypt_key_delete (struct list
*auth_crypt
, u_char key_id
)
1241 struct listnode
*node
, *nnode
;
1242 struct crypt_key
*ck
;
1244 for (ALL_LIST_ELEMENTS (auth_crypt
, node
, nnode
, ck
))
1246 if (ck
->key_id
== key_id
)
1248 listnode_delete (auth_crypt
, ck
);
1249 XFREE (MTYPE_OSPF_CRYPT_KEY
, ck
);
1258 ospf_default_iftype(struct interface
*ifp
)
1260 if (if_is_pointopoint (ifp
))
1261 return OSPF_IFTYPE_POINTOPOINT
;
1262 else if (if_is_loopback (ifp
))
1263 return OSPF_IFTYPE_LOOPBACK
;
1265 return OSPF_IFTYPE_BROADCAST
;
1271 /* Initialize Zebra interface data structure. */
1273 om
->iflist
= iflist
;
1274 if_add_hook (IF_NEW_HOOK
, ospf_if_new_hook
);
1275 if_add_hook (IF_DELETE_HOOK
, ospf_if_delete_hook
);