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 /* 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
);
253 /* Restore an interface to its pre UP state
254 Used from ism_interface_down only */
256 ospf_if_cleanup (struct ospf_interface
*oi
)
258 struct route_node
*rn
;
259 struct listnode
*node
, *nnode
;
260 struct ospf_neighbor
*nbr
;
261 struct ospf_nbr_nbma
*nbr_nbma
;
262 struct ospf_lsa
*lsa
;
264 /* oi->nbrs and oi->nbr_nbma should be deleted on InterfaceDown event */
265 /* delete all static neighbors attached to this interface */
266 for (ALL_LIST_ELEMENTS (oi
->nbr_nbma
, node
, nnode
, nbr_nbma
))
268 OSPF_POLL_TIMER_OFF (nbr_nbma
->t_poll
);
272 nbr_nbma
->nbr
->nbr_nbma
= NULL
;
273 nbr_nbma
->nbr
= NULL
;
278 listnode_delete (oi
->nbr_nbma
, nbr_nbma
);
281 /* send Neighbor event KillNbr to all associated neighbors. */
282 for (rn
= route_top (oi
->nbrs
); rn
; rn
= route_next (rn
))
283 if ((nbr
= rn
->info
) != NULL
)
284 if (nbr
!= oi
->nbr_self
)
285 OSPF_NSM_EVENT_EXECUTE (nbr
, NSM_KillNbr
);
287 /* Cleanup Link State Acknowlegdment list. */
288 for (ALL_LIST_ELEMENTS (oi
->ls_ack
, node
, nnode
, lsa
))
289 ospf_lsa_unlock (&lsa
); /* oi->ls_ack */
290 list_delete_all_node (oi
->ls_ack
);
292 oi
->crypt_seqnum
= 0;
294 /* Empty link state update queue */
295 ospf_ls_upd_queue_empty (oi
);
297 /* Reset pseudo neighbor. */
298 ospf_nbr_self_reset (oi
, oi
->ospf
->router_id
);
302 ospf_if_free (struct ospf_interface
*oi
)
306 assert (oi
->state
== ISM_Down
);
308 ospf_opaque_type9_lsa_term (oi
);
310 /* Free Pseudo Neighbour */
311 ospf_nbr_delete (oi
->nbr_self
);
313 route_table_finish (oi
->nbrs
);
314 route_table_finish (oi
->ls_upd_queue
);
316 /* Free any lists that should be freed */
317 list_free (oi
->nbr_nbma
);
319 list_free (oi
->ls_ack
);
320 list_free (oi
->ls_ack_direct
.ls_ack
);
322 ospf_delete_from_if (oi
->ifp
, oi
);
324 listnode_delete (oi
->ospf
->oiflist
, oi
);
325 listnode_delete (oi
->area
->oiflist
, oi
);
327 thread_cancel_event (master
, oi
);
329 memset (oi
, 0, sizeof (*oi
));
330 XFREE (MTYPE_OSPF_IF
, oi
);
335 * check if interface with given address is configured and
336 * return it if yes. special treatment for PtP networks.
338 struct ospf_interface
*
339 ospf_if_is_configured (struct ospf
*ospf
, struct in_addr
*address
)
341 struct listnode
*node
, *nnode
;
342 struct ospf_interface
*oi
;
343 struct prefix_ipv4 addr
;
345 addr
.family
= AF_INET
;
346 addr
.prefix
= *address
;
347 addr
.prefixlen
= IPV4_MAX_PREFIXLEN
;
349 for (ALL_LIST_ELEMENTS (ospf
->oiflist
, node
, nnode
, oi
))
350 if (oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
)
352 if (CHECK_FLAG(oi
->connected
->flags
, ZEBRA_IFA_UNNUMBERED
))
354 if (htonl(oi
->ifp
->ifindex
) == address
->s_addr
)
357 else if (oi
->type
== OSPF_IFTYPE_POINTOPOINT
)
359 /* special leniency: match if addr is anywhere on peer subnet */
360 if (prefix_match(CONNECTED_PREFIX(oi
->connected
),
361 (struct prefix
*)&addr
))
366 if (IPV4_ADDR_SAME (address
, &oi
->address
->u
.prefix4
))
374 ospf_if_is_up (struct ospf_interface
*oi
)
376 return if_is_up (oi
->ifp
);
379 struct ospf_interface
*
380 ospf_if_exists (struct ospf_interface
*oic
)
382 struct listnode
*node
;
384 struct ospf_interface
*oi
;
386 if ((ospf
= ospf_lookup ()) == NULL
)
389 for (ALL_LIST_ELEMENTS_RO (ospf
->oiflist
, node
, oi
))
396 /* Lookup OSPF interface by router LSA posistion */
397 struct ospf_interface
*
398 ospf_if_lookup_by_lsa_pos (struct ospf_area
*area
, int lsa_pos
)
400 struct listnode
*node
;
401 struct ospf_interface
*oi
;
403 for (ALL_LIST_ELEMENTS_RO (area
->oiflist
, node
, oi
))
405 if (lsa_pos
>= oi
->lsa_pos_beg
&& lsa_pos
< oi
->lsa_pos_end
)
411 struct ospf_interface
*
412 ospf_if_lookup_by_local_addr (struct ospf
*ospf
,
413 struct interface
*ifp
, struct in_addr address
)
415 struct listnode
*node
;
416 struct ospf_interface
*oi
;
418 for (ALL_LIST_ELEMENTS_RO (ospf
->oiflist
, node
, oi
))
419 if (oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
)
421 if (ifp
&& oi
->ifp
!= ifp
)
424 if (IPV4_ADDR_SAME (&address
, &oi
->address
->u
.prefix4
))
431 struct ospf_interface
*
432 ospf_if_lookup_by_prefix (struct ospf
*ospf
, struct prefix_ipv4
*p
)
434 struct listnode
*node
;
435 struct ospf_interface
*oi
;
437 /* Check each Interface. */
438 for (ALL_LIST_ELEMENTS_RO (ospf
->oiflist
, node
, oi
))
440 if (oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
)
444 prefix_copy (&ptmp
, CONNECTED_PREFIX(oi
->connected
));
446 if (prefix_same (&ptmp
, (struct prefix
*) p
))
453 /* determine receiving interface by ifp and source address */
454 struct ospf_interface
*
455 ospf_if_lookup_recv_if (struct ospf
*ospf
, struct in_addr src
,
456 struct interface
*ifp
)
458 struct route_node
*rn
;
459 struct prefix_ipv4 addr
;
460 struct ospf_interface
*oi
, *match
;
462 addr
.family
= AF_INET
;
464 addr
.prefixlen
= IPV4_MAX_BITLEN
;
468 for (rn
= route_top (IF_OIFS (ifp
)); rn
; rn
= route_next (rn
))
472 if (!oi
) /* oi can be NULL for PtP aliases */
475 if (oi
->type
== OSPF_IFTYPE_VIRTUALLINK
)
478 if (if_is_loopback (oi
->ifp
))
481 if (CHECK_FLAG(oi
->connected
->flags
, ZEBRA_IFA_UNNUMBERED
))
483 else if (prefix_match (CONNECTED_PREFIX(oi
->connected
),
484 (struct prefix
*) &addr
))
486 if ( (match
== NULL
) ||
487 (match
->address
->prefixlen
< oi
->address
->prefixlen
)
497 ospf_if_stream_set (struct ospf_interface
*oi
)
499 /* set output fifo queue. */
500 if (oi
->obuf
== NULL
)
501 oi
->obuf
= ospf_fifo_new ();
505 ospf_if_stream_unset (struct ospf_interface
*oi
)
507 struct ospf
*ospf
= oi
->ospf
;
511 ospf_fifo_free (oi
->obuf
);
516 listnode_delete (ospf
->oi_write_q
, oi
);
517 if (list_isempty(ospf
->oi_write_q
))
518 OSPF_TIMER_OFF (ospf
->t_write
);
525 static struct ospf_if_params
*
526 ospf_new_if_params (void)
528 struct ospf_if_params
*oip
;
530 oip
= XCALLOC (MTYPE_OSPF_IF_PARAMS
, sizeof (struct ospf_if_params
));
535 UNSET_IF_PARAM (oip
, output_cost_cmd
);
536 UNSET_IF_PARAM (oip
, transmit_delay
);
537 UNSET_IF_PARAM (oip
, retransmit_interval
);
538 UNSET_IF_PARAM (oip
, passive_interface
);
539 UNSET_IF_PARAM (oip
, v_hello
);
540 UNSET_IF_PARAM (oip
, fast_hello
);
541 UNSET_IF_PARAM (oip
, v_wait
);
542 UNSET_IF_PARAM (oip
, priority
);
543 UNSET_IF_PARAM (oip
, type
);
544 UNSET_IF_PARAM (oip
, auth_simple
);
545 UNSET_IF_PARAM (oip
, auth_crypt
);
546 UNSET_IF_PARAM (oip
, auth_type
);
548 oip
->auth_crypt
= list_new ();
550 oip
->network_lsa_seqnum
= htonl(OSPF_INITIAL_SEQUENCE_NUMBER
);
556 ospf_del_if_params (struct ospf_if_params
*oip
)
558 list_delete (oip
->auth_crypt
);
559 bfd_info_free(&(oip
->bfd_info
));
560 XFREE (MTYPE_OSPF_IF_PARAMS
, oip
);
564 ospf_free_if_params (struct interface
*ifp
, struct in_addr addr
)
566 struct ospf_if_params
*oip
;
567 struct prefix_ipv4 p
;
568 struct route_node
*rn
;
571 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
573 rn
= route_node_lookup (IF_OIFS_PARAMS (ifp
), (struct prefix
*)&p
);
574 if (!rn
|| !rn
->info
)
578 route_unlock_node (rn
);
580 if (!OSPF_IF_PARAM_CONFIGURED (oip
, output_cost_cmd
) &&
581 !OSPF_IF_PARAM_CONFIGURED (oip
, transmit_delay
) &&
582 !OSPF_IF_PARAM_CONFIGURED (oip
, retransmit_interval
) &&
583 !OSPF_IF_PARAM_CONFIGURED (oip
, passive_interface
) &&
584 !OSPF_IF_PARAM_CONFIGURED (oip
, v_hello
) &&
585 !OSPF_IF_PARAM_CONFIGURED (oip
, fast_hello
) &&
586 !OSPF_IF_PARAM_CONFIGURED (oip
, v_wait
) &&
587 !OSPF_IF_PARAM_CONFIGURED (oip
, priority
) &&
588 !OSPF_IF_PARAM_CONFIGURED (oip
, type
) &&
589 !OSPF_IF_PARAM_CONFIGURED (oip
, auth_simple
) &&
590 !OSPF_IF_PARAM_CONFIGURED (oip
, auth_type
) &&
591 listcount (oip
->auth_crypt
) == 0 &&
592 ntohl (oip
->network_lsa_seqnum
) != OSPF_INITIAL_SEQUENCE_NUMBER
)
594 ospf_del_if_params (oip
);
596 route_unlock_node (rn
);
600 struct ospf_if_params
*
601 ospf_lookup_if_params (struct interface
*ifp
, struct in_addr addr
)
603 struct prefix_ipv4 p
;
604 struct route_node
*rn
;
607 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
610 rn
= route_node_lookup (IF_OIFS_PARAMS (ifp
), (struct prefix
*)&p
);
614 route_unlock_node (rn
);
621 struct ospf_if_params
*
622 ospf_get_if_params (struct interface
*ifp
, struct in_addr addr
)
624 struct prefix_ipv4 p
;
625 struct route_node
*rn
;
628 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
631 rn
= route_node_get (IF_OIFS_PARAMS (ifp
), (struct prefix
*)&p
);
633 if (rn
->info
== NULL
)
634 rn
->info
= ospf_new_if_params ();
636 route_unlock_node (rn
);
642 ospf_if_update_params (struct interface
*ifp
, struct in_addr addr
)
644 struct route_node
*rn
;
645 struct ospf_interface
*oi
;
647 for (rn
= route_top (IF_OIFS (ifp
)); rn
; rn
= route_next (rn
))
649 if ((oi
= rn
->info
) == NULL
)
652 if (IPV4_ADDR_SAME (&oi
->address
->u
.prefix4
, &addr
))
653 oi
->params
= ospf_lookup_if_params (ifp
, oi
->address
->u
.prefix4
);
658 ospf_if_new_hook (struct interface
*ifp
)
662 ifp
->info
= XCALLOC (MTYPE_OSPF_IF_INFO
, sizeof (struct ospf_if_info
));
664 IF_OIFS (ifp
) = route_table_init ();
665 IF_OIFS_PARAMS (ifp
) = route_table_init ();
667 IF_DEF_PARAMS (ifp
) = ospf_new_if_params ();
669 SET_IF_PARAM (IF_DEF_PARAMS (ifp
), transmit_delay
);
670 IF_DEF_PARAMS (ifp
)->transmit_delay
= OSPF_TRANSMIT_DELAY_DEFAULT
;
672 SET_IF_PARAM (IF_DEF_PARAMS (ifp
), retransmit_interval
);
673 IF_DEF_PARAMS (ifp
)->retransmit_interval
= OSPF_RETRANSMIT_INTERVAL_DEFAULT
;
675 SET_IF_PARAM (IF_DEF_PARAMS (ifp
), priority
);
676 IF_DEF_PARAMS (ifp
)->priority
= OSPF_ROUTER_PRIORITY_DEFAULT
;
678 IF_DEF_PARAMS (ifp
)->mtu_ignore
= OSPF_MTU_IGNORE_DEFAULT
;
680 SET_IF_PARAM (IF_DEF_PARAMS (ifp
), v_hello
);
681 IF_DEF_PARAMS (ifp
)->v_hello
= OSPF_HELLO_INTERVAL_DEFAULT
;
683 SET_IF_PARAM (IF_DEF_PARAMS (ifp
), fast_hello
);
684 IF_DEF_PARAMS (ifp
)->fast_hello
= OSPF_FAST_HELLO_DEFAULT
;
686 SET_IF_PARAM (IF_DEF_PARAMS (ifp
), v_wait
);
687 IF_DEF_PARAMS (ifp
)->v_wait
= OSPF_ROUTER_DEAD_INTERVAL_DEFAULT
;
689 SET_IF_PARAM (IF_DEF_PARAMS (ifp
), auth_simple
);
690 memset (IF_DEF_PARAMS (ifp
)->auth_simple
, 0, OSPF_AUTH_SIMPLE_SIZE
);
692 SET_IF_PARAM (IF_DEF_PARAMS (ifp
), auth_type
);
693 IF_DEF_PARAMS (ifp
)->auth_type
= OSPF_AUTH_NOTSET
;
695 rc
= ospf_opaque_new_if (ifp
);
700 ospf_if_delete_hook (struct interface
*ifp
)
703 struct route_node
*rn
;
704 rc
= ospf_opaque_del_if (ifp
);
706 route_table_finish (IF_OIFS (ifp
));
708 for (rn
= route_top (IF_OIFS_PARAMS (ifp
)); rn
; rn
= route_next (rn
))
710 ospf_del_if_params (rn
->info
);
711 route_table_finish (IF_OIFS_PARAMS (ifp
));
713 ospf_del_if_params ((struct ospf_if_params
*) IF_DEF_PARAMS (ifp
));
714 XFREE (MTYPE_OSPF_IF_INFO
, ifp
->info
);
721 ospf_if_is_enable (struct ospf_interface
*oi
)
723 if (!if_is_loopback (oi
->ifp
))
724 if (if_is_up (oi
->ifp
))
731 ospf_if_set_multicast(struct ospf_interface
*oi
)
733 if ((oi
->state
> ISM_Loopback
) &&
734 (oi
->type
!= OSPF_IFTYPE_LOOPBACK
) &&
735 (oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
) &&
736 (OSPF_IF_PASSIVE_STATUS(oi
) == OSPF_IF_ACTIVE
))
738 /* The interface should belong to the OSPF-all-routers group. */
739 if (!OI_MEMBER_CHECK(oi
, MEMBER_ALLROUTERS
) &&
740 (ospf_if_add_allspfrouters(oi
->ospf
, oi
->address
,
741 oi
->ifp
->ifindex
) >= 0))
742 /* Set the flag only if the system call to join succeeded. */
743 OI_MEMBER_JOINED(oi
, MEMBER_ALLROUTERS
);
747 /* The interface should NOT belong to the OSPF-all-routers group. */
748 if (OI_MEMBER_CHECK(oi
, MEMBER_ALLROUTERS
))
750 /* Only actually drop if this is the last reference */
751 if (OI_MEMBER_COUNT(oi
, MEMBER_ALLROUTERS
) == 1)
752 ospf_if_drop_allspfrouters (oi
->ospf
, oi
->address
,
754 /* Unset the flag regardless of whether the system call to leave
755 the group succeeded, since it's much safer to assume that
756 we are not a member. */
757 OI_MEMBER_LEFT(oi
,MEMBER_ALLROUTERS
);
761 if (((oi
->type
== OSPF_IFTYPE_BROADCAST
) ||
762 (oi
->type
== OSPF_IFTYPE_POINTOPOINT
)) &&
763 ((oi
->state
== ISM_DR
) || (oi
->state
== ISM_Backup
)) &&
764 (OSPF_IF_PASSIVE_STATUS(oi
) == OSPF_IF_ACTIVE
))
766 /* The interface should belong to the OSPF-designated-routers group. */
767 if (!OI_MEMBER_CHECK(oi
, MEMBER_DROUTERS
) &&
768 (ospf_if_add_alldrouters(oi
->ospf
, oi
->address
,
769 oi
->ifp
->ifindex
) >= 0))
770 /* Set the flag only if the system call to join succeeded. */
771 OI_MEMBER_JOINED(oi
, MEMBER_DROUTERS
);
775 /* The interface should NOT belong to the OSPF-designated-routers group */
776 if (OI_MEMBER_CHECK(oi
, MEMBER_DROUTERS
))
778 /* drop only if last reference */
779 if (OI_MEMBER_COUNT(oi
, MEMBER_DROUTERS
) == 1)
780 ospf_if_drop_alldrouters(oi
->ospf
, oi
->address
, oi
->ifp
->ifindex
);
782 /* Unset the flag regardless of whether the system call to leave
783 the group succeeded, since it's much safer to assume that
784 we are not a member. */
785 OI_MEMBER_LEFT(oi
, MEMBER_DROUTERS
);
791 ospf_if_up (struct ospf_interface
*oi
)
796 if (oi
->type
== OSPF_IFTYPE_LOOPBACK
)
797 OSPF_ISM_EVENT_SCHEDULE (oi
, ISM_LoopInd
);
800 struct ospf
*ospf
= ospf_lookup ();
802 ospf_adjust_sndbuflen (ospf
, oi
->ifp
->mtu
);
804 zlog_warn ("%s: ospf_lookup() returned NULL", __func__
);
805 ospf_if_stream_set (oi
);
806 OSPF_ISM_EVENT_SCHEDULE (oi
, ISM_InterfaceUp
);
813 ospf_if_down (struct ospf_interface
*oi
)
818 OSPF_ISM_EVENT_EXECUTE (oi
, ISM_InterfaceDown
);
819 /* delete position in router LSA */
822 /* Shutdown packet reception and sending */
823 ospf_if_stream_unset (oi
);
829 /* Virtual Link related functions. */
831 struct ospf_vl_data
*
832 ospf_vl_data_new (struct ospf_area
*area
, struct in_addr vl_peer
)
834 struct ospf_vl_data
*vl_data
;
836 vl_data
= XCALLOC (MTYPE_OSPF_VL_DATA
, sizeof (struct ospf_vl_data
));
838 vl_data
->vl_peer
.s_addr
= vl_peer
.s_addr
;
839 vl_data
->vl_area_id
= area
->area_id
;
840 vl_data
->format
= area
->format
;
846 ospf_vl_data_free (struct ospf_vl_data
*vl_data
)
848 XFREE (MTYPE_OSPF_VL_DATA
, vl_data
);
851 u_int vlink_count
= 0;
853 struct ospf_interface
*
854 ospf_vl_new (struct ospf
*ospf
, struct ospf_vl_data
*vl_data
)
856 struct ospf_interface
* voi
;
857 struct interface
* vi
;
858 char ifname
[INTERFACE_NAMSIZ
+ 1];
859 struct ospf_area
*area
;
860 struct in_addr area_id
;
861 struct connected
*co
;
862 struct prefix_ipv4
*p
;
864 if (IS_DEBUG_OSPF_EVENT
)
865 zlog_debug ("ospf_vl_new(): Start");
866 if (vlink_count
== OSPF_VL_MAX_COUNT
)
868 if (IS_DEBUG_OSPF_EVENT
)
869 zlog_debug ("ospf_vl_new(): Alarm: "
870 "cannot create more than OSPF_MAX_VL_COUNT virtual links");
874 if (IS_DEBUG_OSPF_EVENT
)
875 zlog_debug ("ospf_vl_new(): creating pseudo zebra interface");
877 snprintf (ifname
, sizeof(ifname
), "VLINK%d", vlink_count
);
878 vi
= if_create (ifname
, strnlen(ifname
, sizeof(ifname
)));
880 * if_create sets ZEBRA_INTERFACE_LINKDETECTION
881 * virtual links don't need this.
883 UNSET_FLAG (vi
->status
, ZEBRA_INTERFACE_LINKDETECTION
);
884 co
= connected_new ();
886 listnode_add (vi
->connected
, co
);
888 p
= prefix_ipv4_new ();
890 p
->prefix
.s_addr
= 0;
893 co
->address
= (struct prefix
*)p
;
895 voi
= ospf_if_new (ospf
, vi
, co
->address
);
898 if (IS_DEBUG_OSPF_EVENT
)
899 zlog_debug ("ospf_vl_new(): Alarm: OSPF int structure is not created");
903 voi
->vl_data
= vl_data
;
904 voi
->ifp
->mtu
= OSPF_VL_MTU
;
905 voi
->type
= OSPF_IFTYPE_VIRTUALLINK
;
908 if (IS_DEBUG_OSPF_EVENT
)
909 zlog_debug ("ospf_vl_new(): Created name: %s", ifname
);
910 if (IS_DEBUG_OSPF_EVENT
)
911 zlog_debug ("ospf_vl_new(): set if->name to %s", vi
->name
);
914 area
= ospf_area_get (ospf
, area_id
, OSPF_AREA_ID_FORMAT_ADDRESS
);
917 if (IS_DEBUG_OSPF_EVENT
)
918 zlog_debug ("ospf_vl_new(): set associated area to the backbone");
920 /* Add pseudo neighbor. */
921 ospf_nbr_self_reset (voi
, voi
->ospf
->router_id
);
923 ospf_area_add_if (voi
->area
, voi
);
925 ospf_if_stream_set (voi
);
927 if (IS_DEBUG_OSPF_EVENT
)
928 zlog_debug ("ospf_vl_new(): Stop");
933 ospf_vl_if_delete (struct ospf_vl_data
*vl_data
)
935 struct interface
*ifp
= vl_data
->vl_oi
->ifp
;
936 vl_data
->vl_oi
->address
->u
.prefix4
.s_addr
= 0;
937 vl_data
->vl_oi
->address
->prefixlen
= 0;
938 ospf_if_free (vl_data
->vl_oi
);
943 /* Look up vl_data for given peer, optionally qualified to be in the
944 * specified area. NULL area returns first found..
946 struct ospf_vl_data
*
947 ospf_vl_lookup (struct ospf
*ospf
, struct ospf_area
*area
,
948 struct in_addr vl_peer
)
950 struct ospf_vl_data
*vl_data
;
951 struct listnode
*node
;
953 if (IS_DEBUG_OSPF_EVENT
)
955 zlog_debug ("%s: Looking for %s", __func__
, inet_ntoa (vl_peer
));
957 zlog_debug ("%s: in area %s", __func__
, inet_ntoa (area
->area_id
));
960 for (ALL_LIST_ELEMENTS_RO (ospf
->vlinks
, node
, vl_data
))
962 if (IS_DEBUG_OSPF_EVENT
)
963 zlog_debug ("%s: VL %s, peer %s", __func__
,
964 vl_data
->vl_oi
->ifp
->name
,
965 inet_ntoa (vl_data
->vl_peer
));
967 if (area
&& !IPV4_ADDR_SAME (&vl_data
->vl_area_id
, &area
->area_id
))
970 if (IPV4_ADDR_SAME (&vl_data
->vl_peer
, &vl_peer
))
978 ospf_vl_shutdown (struct ospf_vl_data
*vl_data
)
980 struct ospf_interface
*oi
;
982 if ((oi
= vl_data
->vl_oi
) == NULL
)
985 oi
->address
->u
.prefix4
.s_addr
= 0;
986 oi
->address
->prefixlen
= 0;
988 UNSET_FLAG (oi
->ifp
->flags
, IFF_UP
);
989 /* OSPF_ISM_EVENT_SCHEDULE (oi, ISM_InterfaceDown); */
990 OSPF_ISM_EVENT_EXECUTE (oi
, ISM_InterfaceDown
);
994 ospf_vl_add (struct ospf
*ospf
, struct ospf_vl_data
*vl_data
)
996 listnode_add (ospf
->vlinks
, vl_data
);
998 ospf_snmp_vl_add (vl_data
);
999 #endif /* HAVE_SNMP */
1003 ospf_vl_delete (struct ospf
*ospf
, struct ospf_vl_data
*vl_data
)
1005 ospf_vl_shutdown (vl_data
);
1006 ospf_vl_if_delete (vl_data
);
1009 ospf_snmp_vl_delete (vl_data
);
1010 #endif /* HAVE_SNMP */
1011 listnode_delete (ospf
->vlinks
, vl_data
);
1013 ospf_vl_data_free (vl_data
);
1017 ospf_vl_set_params (struct ospf_vl_data
*vl_data
, struct vertex
*v
)
1020 struct ospf_interface
*voi
;
1021 struct listnode
*node
;
1022 struct vertex_parent
*vp
= NULL
;
1024 struct router_lsa
*rl
;
1026 voi
= vl_data
->vl_oi
;
1028 if (voi
->output_cost
!= v
->distance
)
1031 voi
->output_cost
= v
->distance
;
1035 for (ALL_LIST_ELEMENTS_RO (v
->parents
, node
, vp
))
1037 vl_data
->nexthop
.oi
= vp
->nexthop
->oi
;
1038 vl_data
->nexthop
.router
= vp
->nexthop
->router
;
1040 if (!IPV4_ADDR_SAME(&voi
->address
->u
.prefix4
,
1041 &vl_data
->nexthop
.oi
->address
->u
.prefix4
))
1044 voi
->address
->u
.prefix4
= vl_data
->nexthop
.oi
->address
->u
.prefix4
;
1045 voi
->address
->prefixlen
= vl_data
->nexthop
.oi
->address
->prefixlen
;
1047 break; /* We take the first interface. */
1050 rl
= (struct router_lsa
*)v
->lsa
;
1052 /* use SPF determined backlink index in struct vertex
1053 * for virtual link destination address
1055 if (vp
&& vp
->backlink
>= 0)
1057 if (!IPV4_ADDR_SAME (&vl_data
->peer_addr
,
1058 &rl
->link
[vp
->backlink
].link_data
))
1060 vl_data
->peer_addr
= rl
->link
[vp
->backlink
].link_data
;
1064 /* This is highly odd, there is no backlink index
1065 * there should be due to the ospf_spf_has_link() check
1066 * in SPF. Lets warn and try pick a link anyway.
1068 zlog_warn ("ospf_vl_set_params: No backlink for %s!",
1069 vl_data
->vl_oi
->ifp
->name
);
1070 for (i
= 0; i
< ntohs (rl
->links
); i
++)
1072 switch (rl
->link
[i
].type
)
1074 case LSA_LINK_TYPE_VIRTUALLINK
:
1075 if (IS_DEBUG_OSPF_EVENT
)
1076 zlog_debug ("found back link through VL");
1077 case LSA_LINK_TYPE_TRANSIT
:
1078 case LSA_LINK_TYPE_POINTOPOINT
:
1079 if (!IPV4_ADDR_SAME (&vl_data
->peer_addr
,
1080 &rl
->link
[i
].link_data
))
1082 vl_data
->peer_addr
= rl
->link
[i
].link_data
;
1087 if (IS_DEBUG_OSPF_EVENT
)
1088 zlog_debug ("%s: %s peer address: %s, cost: %d,%schanged", __func__
,
1089 vl_data
->vl_oi
->ifp
->name
,
1090 inet_ntoa(vl_data
->peer_addr
),
1092 (changed
? " " : " un"));
1099 ospf_vl_up_check (struct ospf_area
*area
, struct in_addr rid
,
1102 struct ospf
*ospf
= area
->ospf
;
1103 struct listnode
*node
;
1104 struct ospf_vl_data
*vl_data
;
1105 struct ospf_interface
*oi
;
1107 if (IS_DEBUG_OSPF_EVENT
)
1109 zlog_debug ("ospf_vl_up_check(): Start");
1110 zlog_debug ("ospf_vl_up_check(): Router ID is %s", inet_ntoa (rid
));
1111 zlog_debug ("ospf_vl_up_check(): Area is %s", inet_ntoa (area
->area_id
));
1114 for (ALL_LIST_ELEMENTS_RO (ospf
->vlinks
, node
, vl_data
))
1116 if (IS_DEBUG_OSPF_EVENT
)
1118 zlog_debug ("%s: considering VL, %s in area %s", __func__
,
1119 vl_data
->vl_oi
->ifp
->name
,
1120 inet_ntoa (vl_data
->vl_area_id
));
1121 zlog_debug ("%s: peer ID: %s", __func__
,
1122 inet_ntoa (vl_data
->vl_peer
));
1125 if (IPV4_ADDR_SAME (&vl_data
->vl_peer
, &rid
) &&
1126 IPV4_ADDR_SAME (&vl_data
->vl_area_id
, &area
->area_id
))
1128 oi
= vl_data
->vl_oi
;
1129 SET_FLAG (vl_data
->flags
, OSPF_VL_FLAG_APPROVED
);
1131 if (IS_DEBUG_OSPF_EVENT
)
1132 zlog_debug ("ospf_vl_up_check(): this VL matched");
1134 if (oi
->state
== ISM_Down
)
1136 if (IS_DEBUG_OSPF_EVENT
)
1137 zlog_debug ("ospf_vl_up_check(): VL is down, waking it up");
1138 SET_FLAG (oi
->ifp
->flags
, IFF_UP
);
1139 OSPF_ISM_EVENT_EXECUTE(oi
,ISM_InterfaceUp
);
1142 if (ospf_vl_set_params (vl_data
, v
))
1144 if (IS_DEBUG_OSPF (ism
, ISM_EVENTS
))
1145 zlog_debug ("ospf_vl_up_check: VL cost change,"
1146 " scheduling router lsa refresh");
1148 ospf_router_lsa_update_area (ospf
->backbone
);
1149 else if (IS_DEBUG_OSPF (ism
, ISM_EVENTS
))
1150 zlog_debug ("ospf_vl_up_check: VL cost change, no backbone!");
1157 ospf_vl_unapprove (struct ospf
*ospf
)
1159 struct listnode
*node
;
1160 struct ospf_vl_data
*vl_data
;
1162 for (ALL_LIST_ELEMENTS_RO (ospf
->vlinks
, node
, vl_data
))
1163 UNSET_FLAG (vl_data
->flags
, OSPF_VL_FLAG_APPROVED
);
1167 ospf_vl_shut_unapproved (struct ospf
*ospf
)
1169 struct listnode
*node
, *nnode
;
1170 struct ospf_vl_data
*vl_data
;
1172 for (ALL_LIST_ELEMENTS (ospf
->vlinks
, node
, nnode
, vl_data
))
1173 if (!CHECK_FLAG (vl_data
->flags
, OSPF_VL_FLAG_APPROVED
))
1174 ospf_vl_shutdown (vl_data
);
1178 ospf_full_virtual_nbrs (struct ospf_area
*area
)
1180 if (IS_DEBUG_OSPF_EVENT
)
1182 zlog_debug ("counting fully adjacent virtual neighbors in area %s",
1183 inet_ntoa (area
->area_id
));
1184 zlog_debug ("there are %d of them", area
->full_vls
);
1187 return area
->full_vls
;
1191 ospf_vls_in_area (struct ospf_area
*area
)
1193 struct listnode
*node
;
1194 struct ospf_vl_data
*vl_data
;
1197 for (ALL_LIST_ELEMENTS_RO (area
->ospf
->vlinks
, node
, vl_data
))
1198 if (IPV4_ADDR_SAME (&vl_data
->vl_area_id
, &area
->area_id
))
1206 ospf_crypt_key_new ()
1208 return XCALLOC (MTYPE_OSPF_CRYPT_KEY
, sizeof (struct crypt_key
));
1212 ospf_crypt_key_add (struct list
*crypt
, struct crypt_key
*ck
)
1214 listnode_add (crypt
, ck
);
1218 ospf_crypt_key_lookup (struct list
*auth_crypt
, u_char key_id
)
1220 struct listnode
*node
;
1221 struct crypt_key
*ck
;
1223 for (ALL_LIST_ELEMENTS_RO (auth_crypt
, node
, ck
))
1224 if (ck
->key_id
== key_id
)
1231 ospf_crypt_key_delete (struct list
*auth_crypt
, u_char key_id
)
1233 struct listnode
*node
, *nnode
;
1234 struct crypt_key
*ck
;
1236 for (ALL_LIST_ELEMENTS (auth_crypt
, node
, nnode
, ck
))
1238 if (ck
->key_id
== key_id
)
1240 listnode_delete (auth_crypt
, ck
);
1241 XFREE (MTYPE_OSPF_CRYPT_KEY
, ck
);
1250 ospf_default_iftype(struct interface
*ifp
)
1252 if (if_is_pointopoint (ifp
))
1253 return OSPF_IFTYPE_POINTOPOINT
;
1254 else if (if_is_loopback (ifp
))
1255 return OSPF_IFTYPE_LOOPBACK
;
1257 return OSPF_IFTYPE_BROADCAST
;
1263 /* Initialize Zebra interface data structure. */
1264 om
->iflist
= vrf_iflist (VRF_DEFAULT
);
1265 if_add_hook (IF_NEW_HOOK
, ospf_if_new_hook
);
1266 if_add_hook (IF_DELETE_HOOK
, ospf_if_delete_hook
);