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.
35 #include "ospfd/ospfd.h"
36 #include "ospfd/ospf_spf.h"
37 #include "ospfd/ospf_interface.h"
38 #include "ospfd/ospf_ism.h"
39 #include "ospfd/ospf_asbr.h"
40 #include "ospfd/ospf_lsa.h"
41 #include "ospfd/ospf_lsdb.h"
42 #include "ospfd/ospf_neighbor.h"
43 #include "ospfd/ospf_nsm.h"
44 #include "ospfd/ospf_packet.h"
45 #include "ospfd/ospf_abr.h"
46 #include "ospfd/ospf_network.h"
47 #include "ospfd/ospf_dump.h"
49 #include "ospfd/ospf_snmp.h"
50 #endif /* HAVE_SNMP */
54 ospf_if_get_output_cost (struct ospf_interface
*oi
)
56 /* If all else fails, use default OSPF cost */
60 bw
= oi
->ifp
->bandwidth
? oi
->ifp
->bandwidth
: OSPF_DEFAULT_BANDWIDTH
;
61 refbw
= oi
->ospf
->ref_bandwidth
;
63 /* A specifed ip ospf cost overrides a calculated one. */
64 if (OSPF_IF_PARAM_CONFIGURED (IF_DEF_PARAMS (oi
->ifp
), output_cost_cmd
) ||
65 OSPF_IF_PARAM_CONFIGURED (oi
->params
, output_cost_cmd
))
66 cost
= OSPF_IF_PARAM (oi
, output_cost_cmd
);
67 /* See if a cost can be calculated from the zebra processes
68 interface bandwidth field. */
71 cost
= (u_int32_t
) ((double)refbw
/ (double)bw
+ (double)0.5);
74 else if (cost
> 65535)
82 ospf_if_recalculate_output_cost (struct interface
*ifp
)
85 struct route_node
*rn
;
87 for (rn
= route_top (IF_OIFS (ifp
)); rn
; rn
= route_next (rn
))
89 struct ospf_interface
*oi
;
91 if ( (oi
= rn
->info
) == NULL
)
94 newcost
= ospf_if_get_output_cost (oi
);
96 /* Is actual output cost changed? */
97 if (oi
->output_cost
!= newcost
)
99 oi
->output_cost
= newcost
;
100 ospf_router_lsa_update_area (oi
->area
);
105 /* Simulate down/up on the interface. This is needed, for example, when
108 ospf_if_reset(struct interface
*ifp
)
110 struct route_node
*rn
;
112 for (rn
= route_top (IF_OIFS (ifp
)); rn
; rn
= route_next (rn
))
114 struct ospf_interface
*oi
;
116 if ( (oi
= rn
->info
) == NULL
)
125 ospf_if_reset_variables (struct ospf_interface
*oi
)
127 /* Set default values. */
128 /* don't clear this flag. oi->flag = OSPF_IF_DISABLE; */
131 oi
->type
= OSPF_IFTYPE_VIRTUALLINK
;
133 /* preserve network-type */
134 if (oi
->type
!= OSPF_IFTYPE_NBMA
)
135 oi
->type
= OSPF_IFTYPE_BROADCAST
;
137 oi
->state
= ISM_Down
;
139 oi
->crypt_seqnum
= 0;
141 /* This must be short, (less than RxmtInterval)
142 - RFC 2328 Section 13.5 para 3. Set to 1 second to avoid Acks being
143 held back for too long - MAG */
147 /* lookup oi for specified prefix/ifp */
148 struct ospf_interface
*
149 ospf_if_table_lookup (struct interface
*ifp
, struct prefix
*prefix
)
152 struct route_node
*rn
;
153 struct ospf_interface
*rninfo
= NULL
;
156 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
158 /* route_node_get implicitely locks */
159 if ((rn
= route_node_lookup (IF_OIFS (ifp
), &p
)))
161 rninfo
= (struct ospf_interface
*) rn
->info
;
162 route_unlock_node (rn
);
169 ospf_add_to_if (struct interface
*ifp
, struct ospf_interface
*oi
)
171 struct route_node
*rn
;
175 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
177 rn
= route_node_get (IF_OIFS (ifp
), &p
);
178 /* rn->info should either be NULL or equal to this oi
179 * as route_node_get may return an existing node
181 assert (!rn
->info
|| rn
->info
== oi
);
186 ospf_delete_from_if (struct interface
*ifp
, struct ospf_interface
*oi
)
188 struct route_node
*rn
;
192 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
194 rn
= route_node_lookup (IF_OIFS (oi
->ifp
), &p
);
198 route_unlock_node (rn
);
199 route_unlock_node (rn
);
202 struct ospf_interface
*
203 ospf_if_new (struct ospf
*ospf
, struct interface
*ifp
, struct prefix
*p
)
205 struct ospf_interface
*oi
;
207 if ((oi
= ospf_if_table_lookup (ifp
, p
)) == NULL
)
209 oi
= XCALLOC (MTYPE_OSPF_IF
, sizeof (struct ospf_interface
));
210 memset (oi
, 0, sizeof (struct ospf_interface
));
215 /* Set zebra interface pointer. */
219 ospf_add_to_if (ifp
, oi
);
220 listnode_add (ospf
->oiflist
, oi
);
222 /* Initialize neighbor list. */
223 oi
->nbrs
= route_table_init ();
225 /* Initialize static neighbor list. */
226 oi
->nbr_nbma
= list_new ();
228 /* Initialize Link State Acknowledgment list. */
229 oi
->ls_ack
= list_new ();
230 oi
->ls_ack_direct
.ls_ack
= list_new ();
232 /* Set default values. */
233 ospf_if_reset_variables (oi
);
235 /* Add pseudo neighbor. */
236 oi
->nbr_self
= ospf_nbr_new (oi
);
238 oi
->ls_upd_queue
= route_table_init ();
239 oi
->t_ls_upd_event
= NULL
;
240 oi
->t_ls_ack_direct
= NULL
;
242 oi
->crypt_seqnum
= time (NULL
);
244 #ifdef HAVE_OPAQUE_LSA
245 ospf_opaque_type9_lsa_init (oi
);
246 #endif /* HAVE_OPAQUE_LSA */
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_delete (oi
->nbr_self
);
299 oi
->nbr_self
= ospf_nbr_new (oi
);
300 ospf_nbr_add_self (oi
);
304 ospf_if_free (struct ospf_interface
*oi
)
308 assert (oi
->state
== ISM_Down
);
310 #ifdef HAVE_OPAQUE_LSA
311 ospf_opaque_type9_lsa_term (oi
);
312 #endif /* HAVE_OPAQUE_LSA */
314 /* Free Pseudo Neighbour */
315 ospf_nbr_delete (oi
->nbr_self
);
317 route_table_finish (oi
->nbrs
);
318 route_table_finish (oi
->ls_upd_queue
);
320 /* Free any lists that should be freed */
321 list_free (oi
->nbr_nbma
);
323 list_free (oi
->ls_ack
);
324 list_free (oi
->ls_ack_direct
.ls_ack
);
326 ospf_delete_from_if (oi
->ifp
, oi
);
328 listnode_delete (oi
->ospf
->oiflist
, oi
);
329 listnode_delete (oi
->area
->oiflist
, oi
);
331 thread_cancel_event (master
, oi
);
333 memset (oi
, 0, sizeof (*oi
));
334 XFREE (MTYPE_OSPF_IF
, oi
);
339 * check if interface with given address is configured and
340 * return it if yes. special treatment for PtP networks.
342 struct ospf_interface
*
343 ospf_if_is_configured (struct ospf
*ospf
, struct in_addr
*address
)
345 struct listnode
*node
, *nnode
;
346 struct ospf_interface
*oi
;
347 struct prefix_ipv4 addr
;
349 addr
.family
= AF_INET
;
350 addr
.prefix
= *address
;
351 addr
.prefixlen
= IPV4_MAX_PREFIXLEN
;
353 for (ALL_LIST_ELEMENTS (ospf
->oiflist
, node
, nnode
, oi
))
354 if (oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
)
356 if (oi
->type
== OSPF_IFTYPE_POINTOPOINT
)
358 /* special leniency: match if addr is anywhere on peer subnet */
359 if (prefix_match(CONNECTED_PREFIX(oi
->connected
),
360 (struct prefix
*)&addr
))
365 if (IPV4_ADDR_SAME (address
, &oi
->address
->u
.prefix4
))
373 ospf_if_is_up (struct ospf_interface
*oi
)
375 return if_is_up (oi
->ifp
);
378 struct ospf_interface
*
379 ospf_if_exists (struct ospf_interface
*oic
)
381 struct listnode
*node
;
383 struct ospf_interface
*oi
;
385 if ((ospf
= ospf_lookup ()) == NULL
)
388 for (ALL_LIST_ELEMENTS_RO (ospf
->oiflist
, node
, oi
))
395 struct ospf_interface
*
396 ospf_if_lookup_by_local_addr (struct ospf
*ospf
,
397 struct interface
*ifp
, struct in_addr address
)
399 struct listnode
*node
;
400 struct ospf_interface
*oi
;
402 for (ALL_LIST_ELEMENTS_RO (ospf
->oiflist
, node
, oi
))
403 if (oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
)
405 if (ifp
&& oi
->ifp
!= ifp
)
408 if (IPV4_ADDR_SAME (&address
, &oi
->address
->u
.prefix4
))
415 struct ospf_interface
*
416 ospf_if_lookup_by_prefix (struct ospf
*ospf
, struct prefix_ipv4
*p
)
418 struct listnode
*node
;
419 struct ospf_interface
*oi
;
421 /* Check each Interface. */
422 for (ALL_LIST_ELEMENTS_RO (ospf
->oiflist
, node
, oi
))
424 if (oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
)
428 prefix_copy (&ptmp
, CONNECTED_PREFIX(oi
->connected
));
430 if (prefix_same (&ptmp
, (struct prefix
*) p
))
437 /* determine receiving interface by ifp and source address */
438 struct ospf_interface
*
439 ospf_if_lookup_recv_if (struct ospf
*ospf
, struct in_addr src
,
440 struct interface
*ifp
)
442 struct route_node
*rn
;
443 struct prefix_ipv4 addr
;
444 struct ospf_interface
*oi
, *match
;
446 addr
.family
= AF_INET
;
448 addr
.prefixlen
= IPV4_MAX_BITLEN
;
452 for (rn
= route_top (IF_OIFS (ifp
)); rn
; rn
= route_next (rn
))
456 if (!oi
) /* oi can be NULL for PtP aliases */
459 if (oi
->type
== OSPF_IFTYPE_VIRTUALLINK
)
462 if (if_is_loopback (oi
->ifp
))
465 if (prefix_match (CONNECTED_PREFIX(oi
->connected
),
466 (struct prefix
*) &addr
))
468 if ( (match
== NULL
) ||
469 (match
->address
->prefixlen
< oi
->address
->prefixlen
)
479 ospf_if_stream_set (struct ospf_interface
*oi
)
481 /* set output fifo queue. */
482 if (oi
->obuf
== NULL
)
483 oi
->obuf
= ospf_fifo_new ();
487 ospf_if_stream_unset (struct ospf_interface
*oi
)
489 struct ospf
*ospf
= oi
->ospf
;
493 ospf_fifo_free (oi
->obuf
);
498 listnode_delete (ospf
->oi_write_q
, oi
);
499 if (list_isempty(ospf
->oi_write_q
))
500 OSPF_TIMER_OFF (ospf
->t_write
);
507 static struct ospf_if_params
*
508 ospf_new_if_params (void)
510 struct ospf_if_params
*oip
;
512 oip
= XCALLOC (MTYPE_OSPF_IF_PARAMS
, sizeof (struct ospf_if_params
));
517 UNSET_IF_PARAM (oip
, output_cost_cmd
);
518 UNSET_IF_PARAM (oip
, transmit_delay
);
519 UNSET_IF_PARAM (oip
, retransmit_interval
);
520 UNSET_IF_PARAM (oip
, passive_interface
);
521 UNSET_IF_PARAM (oip
, v_hello
);
522 UNSET_IF_PARAM (oip
, fast_hello
);
523 UNSET_IF_PARAM (oip
, v_wait
);
524 UNSET_IF_PARAM (oip
, priority
);
525 UNSET_IF_PARAM (oip
, type
);
526 UNSET_IF_PARAM (oip
, auth_simple
);
527 UNSET_IF_PARAM (oip
, auth_crypt
);
528 UNSET_IF_PARAM (oip
, auth_type
);
530 oip
->auth_crypt
= list_new ();
532 oip
->network_lsa_seqnum
= htonl(OSPF_INITIAL_SEQUENCE_NUMBER
);
538 ospf_del_if_params (struct ospf_if_params
*oip
)
540 list_delete (oip
->auth_crypt
);
541 XFREE (MTYPE_OSPF_IF_PARAMS
, oip
);
545 ospf_free_if_params (struct interface
*ifp
, struct in_addr addr
)
547 struct ospf_if_params
*oip
;
548 struct prefix_ipv4 p
;
549 struct route_node
*rn
;
552 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
554 rn
= route_node_lookup (IF_OIFS_PARAMS (ifp
), (struct prefix
*)&p
);
555 if (!rn
|| !rn
->info
)
559 route_unlock_node (rn
);
561 if (!OSPF_IF_PARAM_CONFIGURED (oip
, output_cost_cmd
) &&
562 !OSPF_IF_PARAM_CONFIGURED (oip
, transmit_delay
) &&
563 !OSPF_IF_PARAM_CONFIGURED (oip
, retransmit_interval
) &&
564 !OSPF_IF_PARAM_CONFIGURED (oip
, passive_interface
) &&
565 !OSPF_IF_PARAM_CONFIGURED (oip
, v_hello
) &&
566 !OSPF_IF_PARAM_CONFIGURED (oip
, fast_hello
) &&
567 !OSPF_IF_PARAM_CONFIGURED (oip
, v_wait
) &&
568 !OSPF_IF_PARAM_CONFIGURED (oip
, priority
) &&
569 !OSPF_IF_PARAM_CONFIGURED (oip
, type
) &&
570 !OSPF_IF_PARAM_CONFIGURED (oip
, auth_simple
) &&
571 !OSPF_IF_PARAM_CONFIGURED (oip
, auth_type
) &&
572 listcount (oip
->auth_crypt
) == 0 &&
573 ntohl (oip
->network_lsa_seqnum
) != OSPF_INITIAL_SEQUENCE_NUMBER
)
575 ospf_del_if_params (oip
);
577 route_unlock_node (rn
);
581 struct ospf_if_params
*
582 ospf_lookup_if_params (struct interface
*ifp
, struct in_addr addr
)
584 struct prefix_ipv4 p
;
585 struct route_node
*rn
;
588 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
591 rn
= route_node_lookup (IF_OIFS_PARAMS (ifp
), (struct prefix
*)&p
);
595 route_unlock_node (rn
);
602 struct ospf_if_params
*
603 ospf_get_if_params (struct interface
*ifp
, struct in_addr addr
)
605 struct prefix_ipv4 p
;
606 struct route_node
*rn
;
609 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
612 rn
= route_node_get (IF_OIFS_PARAMS (ifp
), (struct prefix
*)&p
);
614 if (rn
->info
== NULL
)
615 rn
->info
= ospf_new_if_params ();
617 route_unlock_node (rn
);
623 ospf_if_update_params (struct interface
*ifp
, struct in_addr addr
)
625 struct route_node
*rn
;
626 struct ospf_interface
*oi
;
628 for (rn
= route_top (IF_OIFS (ifp
)); rn
; rn
= route_next (rn
))
630 if ((oi
= rn
->info
) == NULL
)
633 if (IPV4_ADDR_SAME (&oi
->address
->u
.prefix4
, &addr
))
634 oi
->params
= ospf_lookup_if_params (ifp
, oi
->address
->u
.prefix4
);
639 ospf_if_new_hook (struct interface
*ifp
)
643 ifp
->info
= XCALLOC (MTYPE_OSPF_IF_INFO
, sizeof (struct ospf_if_info
));
645 IF_OIFS (ifp
) = route_table_init ();
646 IF_OIFS_PARAMS (ifp
) = route_table_init ();
648 IF_DEF_PARAMS (ifp
) = ospf_new_if_params ();
650 SET_IF_PARAM (IF_DEF_PARAMS (ifp
), transmit_delay
);
651 IF_DEF_PARAMS (ifp
)->transmit_delay
= OSPF_TRANSMIT_DELAY_DEFAULT
;
653 SET_IF_PARAM (IF_DEF_PARAMS (ifp
), retransmit_interval
);
654 IF_DEF_PARAMS (ifp
)->retransmit_interval
= OSPF_RETRANSMIT_INTERVAL_DEFAULT
;
656 SET_IF_PARAM (IF_DEF_PARAMS (ifp
), priority
);
657 IF_DEF_PARAMS (ifp
)->priority
= OSPF_ROUTER_PRIORITY_DEFAULT
;
659 IF_DEF_PARAMS (ifp
)->mtu_ignore
= OSPF_MTU_IGNORE_DEFAULT
;
661 SET_IF_PARAM (IF_DEF_PARAMS (ifp
), v_hello
);
662 IF_DEF_PARAMS (ifp
)->v_hello
= OSPF_HELLO_INTERVAL_DEFAULT
;
664 SET_IF_PARAM (IF_DEF_PARAMS (ifp
), fast_hello
);
665 IF_DEF_PARAMS (ifp
)->fast_hello
= OSPF_FAST_HELLO_DEFAULT
;
667 SET_IF_PARAM (IF_DEF_PARAMS (ifp
), v_wait
);
668 IF_DEF_PARAMS (ifp
)->v_wait
= OSPF_ROUTER_DEAD_INTERVAL_DEFAULT
;
670 SET_IF_PARAM (IF_DEF_PARAMS (ifp
), auth_simple
);
671 memset (IF_DEF_PARAMS (ifp
)->auth_simple
, 0, OSPF_AUTH_SIMPLE_SIZE
);
673 SET_IF_PARAM (IF_DEF_PARAMS (ifp
), auth_type
);
674 IF_DEF_PARAMS (ifp
)->auth_type
= OSPF_AUTH_NOTSET
;
676 #ifdef HAVE_OPAQUE_LSA
677 rc
= ospf_opaque_new_if (ifp
);
678 #endif /* HAVE_OPAQUE_LSA */
683 ospf_if_delete_hook (struct interface
*ifp
)
686 struct route_node
*rn
;
687 #ifdef HAVE_OPAQUE_LSA
688 rc
= ospf_opaque_del_if (ifp
);
689 #endif /* HAVE_OPAQUE_LSA */
691 route_table_finish (IF_OIFS (ifp
));
693 for (rn
= route_top (IF_OIFS_PARAMS (ifp
)); rn
; rn
= route_next (rn
))
695 ospf_del_if_params (rn
->info
);
696 route_table_finish (IF_OIFS_PARAMS (ifp
));
698 ospf_del_if_params ((struct ospf_if_params
*) IF_DEF_PARAMS (ifp
));
699 XFREE (MTYPE_OSPF_IF_INFO
, ifp
->info
);
706 ospf_if_is_enable (struct ospf_interface
*oi
)
708 if (!if_is_loopback (oi
->ifp
))
709 if (if_is_up (oi
->ifp
))
716 ospf_if_set_multicast(struct ospf_interface
*oi
)
718 if ((oi
->state
> ISM_Loopback
) &&
719 (oi
->type
!= OSPF_IFTYPE_LOOPBACK
) &&
720 (oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
) &&
721 (OSPF_IF_PASSIVE_STATUS(oi
) == OSPF_IF_ACTIVE
))
723 /* The interface should belong to the OSPF-all-routers group. */
724 if (!OI_MEMBER_CHECK(oi
, MEMBER_ALLROUTERS
) &&
725 (ospf_if_add_allspfrouters(oi
->ospf
, oi
->address
,
726 oi
->ifp
->ifindex
) >= 0))
727 /* Set the flag only if the system call to join succeeded. */
728 OI_MEMBER_JOINED(oi
, MEMBER_ALLROUTERS
);
732 /* The interface should NOT belong to the OSPF-all-routers group. */
733 if (OI_MEMBER_CHECK(oi
, MEMBER_ALLROUTERS
))
735 /* Only actually drop if this is the last reference */
736 if (OI_MEMBER_COUNT(oi
, MEMBER_ALLROUTERS
) == 1)
737 ospf_if_drop_allspfrouters (oi
->ospf
, oi
->address
,
739 /* Unset the flag regardless of whether the system call to leave
740 the group succeeded, since it's much safer to assume that
741 we are not a member. */
742 OI_MEMBER_LEFT(oi
,MEMBER_ALLROUTERS
);
746 if (((oi
->type
== OSPF_IFTYPE_BROADCAST
) ||
747 (oi
->type
== OSPF_IFTYPE_POINTOPOINT
)) &&
748 ((oi
->state
== ISM_DR
) || (oi
->state
== ISM_Backup
)) &&
749 (OSPF_IF_PASSIVE_STATUS(oi
) == OSPF_IF_ACTIVE
))
751 /* The interface should belong to the OSPF-designated-routers group. */
752 if (!OI_MEMBER_CHECK(oi
, MEMBER_DROUTERS
) &&
753 (ospf_if_add_alldrouters(oi
->ospf
, oi
->address
,
754 oi
->ifp
->ifindex
) >= 0))
755 /* Set the flag only if the system call to join succeeded. */
756 OI_MEMBER_JOINED(oi
, MEMBER_DROUTERS
);
760 /* The interface should NOT belong to the OSPF-designated-routers group */
761 if (OI_MEMBER_CHECK(oi
, MEMBER_DROUTERS
))
763 /* drop only if last reference */
764 if (OI_MEMBER_COUNT(oi
, MEMBER_DROUTERS
) == 1)
765 ospf_if_drop_alldrouters(oi
->ospf
, oi
->address
, oi
->ifp
->ifindex
);
767 /* Unset the flag regardless of whether the system call to leave
768 the group succeeded, since it's much safer to assume that
769 we are not a member. */
770 OI_MEMBER_LEFT(oi
, MEMBER_DROUTERS
);
776 ospf_if_up (struct ospf_interface
*oi
)
781 if (oi
->type
== OSPF_IFTYPE_LOOPBACK
)
782 OSPF_ISM_EVENT_SCHEDULE (oi
, ISM_LoopInd
);
785 struct ospf
*ospf
= ospf_lookup ();
787 ospf_adjust_sndbuflen (ospf
, oi
->ifp
->mtu
);
789 zlog_warn ("%s: ospf_lookup() returned NULL", __func__
);
790 ospf_if_stream_set (oi
);
791 OSPF_ISM_EVENT_SCHEDULE (oi
, ISM_InterfaceUp
);
798 ospf_if_down (struct ospf_interface
*oi
)
803 OSPF_ISM_EVENT_EXECUTE (oi
, ISM_InterfaceDown
);
804 /* Shutdown packet reception and sending */
805 ospf_if_stream_unset (oi
);
811 /* Virtual Link related functions. */
813 struct ospf_vl_data
*
814 ospf_vl_data_new (struct ospf_area
*area
, struct in_addr vl_peer
)
816 struct ospf_vl_data
*vl_data
;
818 vl_data
= XCALLOC (MTYPE_OSPF_VL_DATA
, sizeof (struct ospf_vl_data
));
820 vl_data
->vl_peer
.s_addr
= vl_peer
.s_addr
;
821 vl_data
->vl_area_id
= area
->area_id
;
822 vl_data
->format
= area
->format
;
828 ospf_vl_data_free (struct ospf_vl_data
*vl_data
)
830 XFREE (MTYPE_OSPF_VL_DATA
, vl_data
);
833 u_int vlink_count
= 0;
835 struct ospf_interface
*
836 ospf_vl_new (struct ospf
*ospf
, struct ospf_vl_data
*vl_data
)
838 struct ospf_interface
* voi
;
839 struct interface
* vi
;
840 char ifname
[INTERFACE_NAMSIZ
+ 1];
841 struct ospf_area
*area
;
842 struct in_addr area_id
;
843 struct connected
*co
;
844 struct prefix_ipv4
*p
;
846 if (IS_DEBUG_OSPF_EVENT
)
847 zlog_debug ("ospf_vl_new(): Start");
848 if (vlink_count
== OSPF_VL_MAX_COUNT
)
850 if (IS_DEBUG_OSPF_EVENT
)
851 zlog_debug ("ospf_vl_new(): Alarm: "
852 "cannot create more than OSPF_MAX_VL_COUNT virtual links");
856 if (IS_DEBUG_OSPF_EVENT
)
857 zlog_debug ("ospf_vl_new(): creating pseudo zebra interface");
859 snprintf (ifname
, sizeof(ifname
), "VLINK%d", vlink_count
);
860 vi
= if_create (ifname
, strnlen(ifname
, sizeof(ifname
)));
861 co
= connected_new ();
863 listnode_add (vi
->connected
, co
);
865 p
= prefix_ipv4_new ();
867 p
->prefix
.s_addr
= 0;
870 co
->address
= (struct prefix
*)p
;
872 voi
= ospf_if_new (ospf
, vi
, co
->address
);
875 if (IS_DEBUG_OSPF_EVENT
)
876 zlog_debug ("ospf_vl_new(): Alarm: OSPF int structure is not created");
880 voi
->vl_data
= vl_data
;
881 voi
->ifp
->mtu
= OSPF_VL_MTU
;
882 voi
->type
= OSPF_IFTYPE_VIRTUALLINK
;
885 if (IS_DEBUG_OSPF_EVENT
)
886 zlog_debug ("ospf_vl_new(): Created name: %s", ifname
);
887 if (IS_DEBUG_OSPF_EVENT
)
888 zlog_debug ("ospf_vl_new(): set if->name to %s", vi
->name
);
891 area
= ospf_area_get (ospf
, area_id
, OSPF_AREA_ID_FORMAT_ADDRESS
);
894 if (IS_DEBUG_OSPF_EVENT
)
895 zlog_debug ("ospf_vl_new(): set associated area to the backbone");
897 ospf_nbr_add_self (voi
);
898 ospf_area_add_if (voi
->area
, voi
);
900 ospf_if_stream_set (voi
);
902 if (IS_DEBUG_OSPF_EVENT
)
903 zlog_debug ("ospf_vl_new(): Stop");
908 ospf_vl_if_delete (struct ospf_vl_data
*vl_data
)
910 struct interface
*ifp
= vl_data
->vl_oi
->ifp
;
911 vl_data
->vl_oi
->address
->u
.prefix4
.s_addr
= 0;
912 vl_data
->vl_oi
->address
->prefixlen
= 0;
913 ospf_if_free (vl_data
->vl_oi
);
918 /* Look up vl_data for given peer, optionally qualified to be in the
919 * specified area. NULL area returns first found..
921 struct ospf_vl_data
*
922 ospf_vl_lookup (struct ospf
*ospf
, struct ospf_area
*area
,
923 struct in_addr vl_peer
)
925 struct ospf_vl_data
*vl_data
;
926 struct listnode
*node
;
928 if (IS_DEBUG_OSPF_EVENT
)
930 zlog_debug ("%s: Looking for %s", __func__
, inet_ntoa (vl_peer
));
932 zlog_debug ("%s: in area %s", __func__
, inet_ntoa (area
->area_id
));
935 for (ALL_LIST_ELEMENTS_RO (ospf
->vlinks
, node
, vl_data
))
937 if (IS_DEBUG_OSPF_EVENT
)
938 zlog_debug ("%s: VL %s, peer %s", __func__
,
939 vl_data
->vl_oi
->ifp
->name
,
940 inet_ntoa (vl_data
->vl_peer
));
942 if (area
&& !IPV4_ADDR_SAME (&vl_data
->vl_area_id
, &area
->area_id
))
945 if (IPV4_ADDR_SAME (&vl_data
->vl_peer
, &vl_peer
))
953 ospf_vl_shutdown (struct ospf_vl_data
*vl_data
)
955 struct ospf_interface
*oi
;
957 if ((oi
= vl_data
->vl_oi
) == NULL
)
960 oi
->address
->u
.prefix4
.s_addr
= 0;
961 oi
->address
->prefixlen
= 0;
963 UNSET_FLAG (oi
->ifp
->flags
, IFF_UP
);
964 /* OSPF_ISM_EVENT_SCHEDULE (oi, ISM_InterfaceDown); */
965 OSPF_ISM_EVENT_EXECUTE (oi
, ISM_InterfaceDown
);
969 ospf_vl_add (struct ospf
*ospf
, struct ospf_vl_data
*vl_data
)
971 listnode_add (ospf
->vlinks
, vl_data
);
973 ospf_snmp_vl_add (vl_data
);
974 #endif /* HAVE_SNMP */
978 ospf_vl_delete (struct ospf
*ospf
, struct ospf_vl_data
*vl_data
)
980 ospf_vl_shutdown (vl_data
);
981 ospf_vl_if_delete (vl_data
);
984 ospf_snmp_vl_delete (vl_data
);
985 #endif /* HAVE_SNMP */
986 listnode_delete (ospf
->vlinks
, vl_data
);
988 ospf_vl_data_free (vl_data
);
992 ospf_vl_set_params (struct ospf_vl_data
*vl_data
, struct vertex
*v
)
995 struct ospf_interface
*voi
;
996 struct listnode
*node
;
997 struct vertex_parent
*vp
= NULL
;
999 struct router_lsa
*rl
;
1001 voi
= vl_data
->vl_oi
;
1003 if (voi
->output_cost
!= v
->distance
)
1006 voi
->output_cost
= v
->distance
;
1010 for (ALL_LIST_ELEMENTS_RO (v
->parents
, node
, vp
))
1012 vl_data
->nexthop
.oi
= vp
->nexthop
->oi
;
1013 vl_data
->nexthop
.router
= vp
->nexthop
->router
;
1015 if (!IPV4_ADDR_SAME(&voi
->address
->u
.prefix4
,
1016 &vl_data
->nexthop
.oi
->address
->u
.prefix4
))
1019 voi
->address
->u
.prefix4
= vl_data
->nexthop
.oi
->address
->u
.prefix4
;
1020 voi
->address
->prefixlen
= vl_data
->nexthop
.oi
->address
->prefixlen
;
1022 break; /* We take the first interface. */
1025 rl
= (struct router_lsa
*)v
->lsa
;
1027 /* use SPF determined backlink index in struct vertex
1028 * for virtual link destination address
1030 if (vp
&& vp
->backlink
>= 0)
1032 if (!IPV4_ADDR_SAME (&vl_data
->peer_addr
,
1033 &rl
->link
[vp
->backlink
].link_data
))
1035 vl_data
->peer_addr
= rl
->link
[vp
->backlink
].link_data
;
1039 /* This is highly odd, there is no backlink index
1040 * there should be due to the ospf_spf_has_link() check
1041 * in SPF. Lets warn and try pick a link anyway.
1043 zlog_warn ("ospf_vl_set_params: No backlink for %s!",
1044 vl_data
->vl_oi
->ifp
->name
);
1045 for (i
= 0; i
< ntohs (rl
->links
); i
++)
1047 switch (rl
->link
[i
].type
)
1049 case LSA_LINK_TYPE_VIRTUALLINK
:
1050 if (IS_DEBUG_OSPF_EVENT
)
1051 zlog_debug ("found back link through VL");
1052 case LSA_LINK_TYPE_TRANSIT
:
1053 case LSA_LINK_TYPE_POINTOPOINT
:
1054 if (!IPV4_ADDR_SAME (&vl_data
->peer_addr
,
1055 &rl
->link
[i
].link_data
))
1057 vl_data
->peer_addr
= rl
->link
[i
].link_data
;
1062 if (IS_DEBUG_OSPF_EVENT
)
1063 zlog_debug ("%s: %s peer address: %s, cost: %d,%schanged", __func__
,
1064 vl_data
->vl_oi
->ifp
->name
,
1065 inet_ntoa(vl_data
->peer_addr
),
1067 (changed
? " " : " un"));
1074 ospf_vl_up_check (struct ospf_area
*area
, struct in_addr rid
,
1077 struct ospf
*ospf
= area
->ospf
;
1078 struct listnode
*node
;
1079 struct ospf_vl_data
*vl_data
;
1080 struct ospf_interface
*oi
;
1082 if (IS_DEBUG_OSPF_EVENT
)
1084 zlog_debug ("ospf_vl_up_check(): Start");
1085 zlog_debug ("ospf_vl_up_check(): Router ID is %s", inet_ntoa (rid
));
1086 zlog_debug ("ospf_vl_up_check(): Area is %s", inet_ntoa (area
->area_id
));
1089 for (ALL_LIST_ELEMENTS_RO (ospf
->vlinks
, node
, vl_data
))
1091 if (IS_DEBUG_OSPF_EVENT
)
1093 zlog_debug ("%s: considering VL, %s in area %s", __func__
,
1094 vl_data
->vl_oi
->ifp
->name
,
1095 inet_ntoa (vl_data
->vl_area_id
));
1096 zlog_debug ("%s: peer ID: %s", __func__
,
1097 inet_ntoa (vl_data
->vl_peer
));
1100 if (IPV4_ADDR_SAME (&vl_data
->vl_peer
, &rid
) &&
1101 IPV4_ADDR_SAME (&vl_data
->vl_area_id
, &area
->area_id
))
1103 oi
= vl_data
->vl_oi
;
1104 SET_FLAG (vl_data
->flags
, OSPF_VL_FLAG_APPROVED
);
1106 if (IS_DEBUG_OSPF_EVENT
)
1107 zlog_debug ("ospf_vl_up_check(): this VL matched");
1109 if (oi
->state
== ISM_Down
)
1111 if (IS_DEBUG_OSPF_EVENT
)
1112 zlog_debug ("ospf_vl_up_check(): VL is down, waking it up");
1113 SET_FLAG (oi
->ifp
->flags
, IFF_UP
);
1114 OSPF_ISM_EVENT_EXECUTE(oi
,ISM_InterfaceUp
);
1117 if (ospf_vl_set_params (vl_data
, v
))
1119 if (IS_DEBUG_OSPF (ism
, ISM_EVENTS
))
1120 zlog_debug ("ospf_vl_up_check: VL cost change,"
1121 " scheduling router lsa refresh");
1123 ospf_router_lsa_update_area (ospf
->backbone
);
1124 else if (IS_DEBUG_OSPF (ism
, ISM_EVENTS
))
1125 zlog_debug ("ospf_vl_up_check: VL cost change, no backbone!");
1132 ospf_vl_unapprove (struct ospf
*ospf
)
1134 struct listnode
*node
;
1135 struct ospf_vl_data
*vl_data
;
1137 for (ALL_LIST_ELEMENTS_RO (ospf
->vlinks
, node
, vl_data
))
1138 UNSET_FLAG (vl_data
->flags
, OSPF_VL_FLAG_APPROVED
);
1142 ospf_vl_shut_unapproved (struct ospf
*ospf
)
1144 struct listnode
*node
, *nnode
;
1145 struct ospf_vl_data
*vl_data
;
1147 for (ALL_LIST_ELEMENTS (ospf
->vlinks
, node
, nnode
, vl_data
))
1148 if (!CHECK_FLAG (vl_data
->flags
, OSPF_VL_FLAG_APPROVED
))
1149 ospf_vl_shutdown (vl_data
);
1153 ospf_full_virtual_nbrs (struct ospf_area
*area
)
1155 if (IS_DEBUG_OSPF_EVENT
)
1157 zlog_debug ("counting fully adjacent virtual neighbors in area %s",
1158 inet_ntoa (area
->area_id
));
1159 zlog_debug ("there are %d of them", area
->full_vls
);
1162 return area
->full_vls
;
1166 ospf_vls_in_area (struct ospf_area
*area
)
1168 struct listnode
*node
;
1169 struct ospf_vl_data
*vl_data
;
1172 for (ALL_LIST_ELEMENTS_RO (area
->ospf
->vlinks
, node
, vl_data
))
1173 if (IPV4_ADDR_SAME (&vl_data
->vl_area_id
, &area
->area_id
))
1181 ospf_crypt_key_new ()
1183 return XCALLOC (MTYPE_OSPF_CRYPT_KEY
, sizeof (struct crypt_key
));
1187 ospf_crypt_key_add (struct list
*crypt
, struct crypt_key
*ck
)
1189 listnode_add (crypt
, ck
);
1193 ospf_crypt_key_lookup (struct list
*auth_crypt
, u_char key_id
)
1195 struct listnode
*node
;
1196 struct crypt_key
*ck
;
1198 for (ALL_LIST_ELEMENTS_RO (auth_crypt
, node
, ck
))
1199 if (ck
->key_id
== key_id
)
1206 ospf_crypt_key_delete (struct list
*auth_crypt
, u_char key_id
)
1208 struct listnode
*node
, *nnode
;
1209 struct crypt_key
*ck
;
1211 for (ALL_LIST_ELEMENTS (auth_crypt
, node
, nnode
, ck
))
1213 if (ck
->key_id
== key_id
)
1215 listnode_delete (auth_crypt
, ck
);
1216 XFREE (MTYPE_OSPF_CRYPT_KEY
, ck
);
1225 ospf_default_iftype(struct interface
*ifp
)
1227 if (if_is_pointopoint (ifp
))
1228 return OSPF_IFTYPE_POINTOPOINT
;
1229 else if (if_is_loopback (ifp
))
1230 return OSPF_IFTYPE_LOOPBACK
;
1232 return OSPF_IFTYPE_BROADCAST
;
1238 /* Initialize Zebra interface data structure. */
1240 om
->iflist
= iflist
;
1241 if_add_hook (IF_NEW_HOOK
, ospf_if_new_hook
);
1242 if_add_hook (IF_DELETE_HOOK
, ospf_if_delete_hook
);