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 along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
36 #include "ospfd/ospfd.h"
37 #include "ospfd/ospf_spf.h"
38 #include "ospfd/ospf_interface.h"
39 #include "ospfd/ospf_ism.h"
40 #include "ospfd/ospf_asbr.h"
41 #include "ospfd/ospf_lsa.h"
42 #include "ospfd/ospf_lsdb.h"
43 #include "ospfd/ospf_neighbor.h"
44 #include "ospfd/ospf_nsm.h"
45 #include "ospfd/ospf_packet.h"
46 #include "ospfd/ospf_abr.h"
47 #include "ospfd/ospf_network.h"
48 #include "ospfd/ospf_dump.h"
50 DEFINE_QOBJ_TYPE(ospf_interface
)
51 DEFINE_HOOK(ospf_vl_add
, (struct ospf_vl_data
* vd
), (vd
))
52 DEFINE_HOOK(ospf_vl_delete
, (struct ospf_vl_data
* vd
), (vd
))
54 int ospf_if_get_output_cost(struct ospf_interface
*oi
)
56 /* If all else fails, use default OSPF cost */
60 /* ifp speed and bw can be 0 in some platforms, use ospf default bw
61 if bw is configured under interface it would be used.
63 if (!oi
->ifp
->bandwidth
&& oi
->ifp
->speed
)
66 bw
= oi
->ifp
->bandwidth
? oi
->ifp
->bandwidth
67 : OSPF_DEFAULT_BANDWIDTH
;
68 refbw
= oi
->ospf
->ref_bandwidth
;
70 /* A specifed ip ospf cost overrides a calculated one. */
71 if (OSPF_IF_PARAM_CONFIGURED(IF_DEF_PARAMS(oi
->ifp
), output_cost_cmd
)
72 || OSPF_IF_PARAM_CONFIGURED(oi
->params
, output_cost_cmd
))
73 cost
= OSPF_IF_PARAM(oi
, output_cost_cmd
);
74 /* See if a cost can be calculated from the zebra processes
75 interface bandwidth field. */
77 cost
= (u_int32_t
)((double)refbw
/ (double)bw
+ (double)0.5);
80 else if (cost
> 65535)
87 void ospf_if_recalculate_output_cost(struct interface
*ifp
)
90 struct route_node
*rn
;
92 for (rn
= route_top(IF_OIFS(ifp
)); rn
; rn
= route_next(rn
)) {
93 struct ospf_interface
*oi
;
95 if ((oi
= rn
->info
) == NULL
)
98 newcost
= ospf_if_get_output_cost(oi
);
100 /* Is actual output cost changed? */
101 if (oi
->output_cost
!= newcost
) {
102 oi
->output_cost
= newcost
;
103 ospf_router_lsa_update_area(oi
->area
);
108 /* Simulate down/up on the interface. This is needed, for example, when
110 void ospf_if_reset(struct interface
*ifp
)
112 struct route_node
*rn
;
114 for (rn
= route_top(IF_OIFS(ifp
)); rn
; rn
= route_next(rn
)) {
115 struct ospf_interface
*oi
;
117 if ((oi
= rn
->info
) == NULL
)
125 void 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
*ospf_if_table_lookup(struct interface
*ifp
,
149 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
))) {
160 rninfo
= (struct ospf_interface
*)rn
->info
;
161 route_unlock_node(rn
);
167 static void ospf_add_to_if(struct interface
*ifp
, struct ospf_interface
*oi
)
169 struct route_node
*rn
;
173 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
175 rn
= route_node_get(IF_OIFS(ifp
), &p
);
176 /* rn->info should either be NULL or equal to this oi
177 * as route_node_get may return an existing node
179 assert(!rn
->info
|| rn
->info
== oi
);
183 static void ospf_delete_from_if(struct interface
*ifp
,
184 struct ospf_interface
*oi
)
186 struct route_node
*rn
;
190 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
192 rn
= route_node_lookup(IF_OIFS(oi
->ifp
), &p
);
196 route_unlock_node(rn
);
197 route_unlock_node(rn
);
200 struct ospf_interface
*ospf_if_new(struct ospf
*ospf
, struct interface
*ifp
,
203 struct ospf_interface
*oi
;
205 if ((oi
= ospf_if_table_lookup(ifp
, p
)) == NULL
) {
206 oi
= XCALLOC(MTYPE_OSPF_IF
, sizeof(struct ospf_interface
));
207 memset(oi
, 0, sizeof(struct ospf_interface
));
211 /* Set zebra interface pointer. */
215 ospf_add_to_if(ifp
, oi
);
216 listnode_add(ospf
->oiflist
, oi
);
218 /* Initialize neighbor list. */
219 oi
->nbrs
= route_table_init();
221 /* Initialize static neighbor list. */
222 oi
->nbr_nbma
= list_new();
224 /* Initialize Link State Acknowledgment list. */
225 oi
->ls_ack
= list_new();
226 oi
->ls_ack_direct
.ls_ack
= list_new();
228 /* Set default values. */
229 ospf_if_reset_variables(oi
);
231 /* Set pseudo neighbor to Null */
234 oi
->ls_upd_queue
= route_table_init();
235 oi
->t_ls_upd_event
= NULL
;
236 oi
->t_ls_ack_direct
= NULL
;
238 oi
->crypt_seqnum
= time(NULL
);
240 ospf_opaque_type9_lsa_init(oi
);
243 QOBJ_REG(oi
, ospf_interface
);
248 /* Restore an interface to its pre UP state
249 Used from ism_interface_down only */
250 void ospf_if_cleanup(struct ospf_interface
*oi
)
252 struct route_node
*rn
;
253 struct listnode
*node
, *nnode
;
254 struct ospf_neighbor
*nbr
;
255 struct ospf_nbr_nbma
*nbr_nbma
;
256 struct ospf_lsa
*lsa
;
258 /* oi->nbrs and oi->nbr_nbma should be deleted on InterfaceDown event */
259 /* delete all static neighbors attached to this interface */
260 for (ALL_LIST_ELEMENTS(oi
->nbr_nbma
, node
, nnode
, nbr_nbma
)) {
261 OSPF_POLL_TIMER_OFF(nbr_nbma
->t_poll
);
264 nbr_nbma
->nbr
->nbr_nbma
= NULL
;
265 nbr_nbma
->nbr
= NULL
;
270 listnode_delete(oi
->nbr_nbma
, nbr_nbma
);
273 /* send Neighbor event KillNbr to all associated neighbors. */
274 for (rn
= route_top(oi
->nbrs
); rn
; rn
= route_next(rn
))
275 if ((nbr
= rn
->info
) != NULL
)
276 if (nbr
!= oi
->nbr_self
)
277 OSPF_NSM_EVENT_EXECUTE(nbr
, NSM_KillNbr
);
279 /* Cleanup Link State Acknowlegdment list. */
280 for (ALL_LIST_ELEMENTS(oi
->ls_ack
, node
, nnode
, lsa
))
281 ospf_lsa_unlock(&lsa
); /* oi->ls_ack */
282 list_delete_all_node(oi
->ls_ack
);
284 oi
->crypt_seqnum
= 0;
286 /* Empty link state update queue */
287 ospf_ls_upd_queue_empty(oi
);
289 /* Reset pseudo neighbor. */
290 ospf_nbr_self_reset(oi
, oi
->ospf
->router_id
);
293 void ospf_if_free(struct ospf_interface
*oi
)
297 assert(oi
->state
== ISM_Down
);
299 ospf_opaque_type9_lsa_term(oi
);
303 /* Free Pseudo Neighbour */
304 ospf_nbr_delete(oi
->nbr_self
);
306 route_table_finish(oi
->nbrs
);
307 route_table_finish(oi
->ls_upd_queue
);
309 /* Free any lists that should be freed */
310 list_free(oi
->nbr_nbma
);
312 list_free(oi
->ls_ack
);
313 list_free(oi
->ls_ack_direct
.ls_ack
);
315 ospf_delete_from_if(oi
->ifp
, oi
);
317 listnode_delete(oi
->ospf
->oiflist
, oi
);
318 listnode_delete(oi
->area
->oiflist
, oi
);
320 thread_cancel_event(master
, oi
);
322 memset(oi
, 0, sizeof(*oi
));
323 XFREE(MTYPE_OSPF_IF
, oi
);
326 int ospf_if_is_up(struct ospf_interface
*oi
)
328 return if_is_up(oi
->ifp
);
331 struct ospf_interface
*ospf_if_exists(struct ospf_interface
*oic
)
333 struct listnode
*node
;
335 struct ospf_interface
*oi
;
337 if ((ospf
= ospf_lookup()) == NULL
)
340 for (ALL_LIST_ELEMENTS_RO(ospf
->oiflist
, node
, oi
))
347 /* Lookup OSPF interface by router LSA posistion */
348 struct ospf_interface
*ospf_if_lookup_by_lsa_pos(struct ospf_area
*area
,
351 struct listnode
*node
;
352 struct ospf_interface
*oi
;
354 for (ALL_LIST_ELEMENTS_RO(area
->oiflist
, node
, oi
)) {
355 if (lsa_pos
>= oi
->lsa_pos_beg
&& lsa_pos
< oi
->lsa_pos_end
)
361 struct ospf_interface
*ospf_if_lookup_by_local_addr(struct ospf
*ospf
,
362 struct interface
*ifp
,
363 struct in_addr address
)
365 struct listnode
*node
;
366 struct ospf_interface
*oi
;
368 for (ALL_LIST_ELEMENTS_RO(ospf
->oiflist
, node
, oi
))
369 if (oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
) {
370 if (ifp
&& oi
->ifp
!= ifp
)
373 if (IPV4_ADDR_SAME(&address
, &oi
->address
->u
.prefix4
))
380 struct ospf_interface
*ospf_if_lookup_by_prefix(struct ospf
*ospf
,
381 struct prefix_ipv4
*p
)
383 struct listnode
*node
;
384 struct ospf_interface
*oi
;
386 /* Check each Interface. */
387 for (ALL_LIST_ELEMENTS_RO(ospf
->oiflist
, node
, oi
)) {
388 if (oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
) {
391 prefix_copy(&ptmp
, CONNECTED_PREFIX(oi
->connected
));
393 if (prefix_same(&ptmp
, (struct prefix
*)p
))
400 /* determine receiving interface by ifp and source address */
401 struct ospf_interface
*ospf_if_lookup_recv_if(struct ospf
*ospf
,
403 struct interface
*ifp
)
405 struct route_node
*rn
;
406 struct prefix_ipv4 addr
;
407 struct ospf_interface
*oi
, *match
;
409 addr
.family
= AF_INET
;
411 addr
.prefixlen
= IPV4_MAX_BITLEN
;
415 for (rn
= route_top(IF_OIFS(ifp
)); rn
; rn
= route_next(rn
)) {
418 if (!oi
) /* oi can be NULL for PtP aliases */
421 if (oi
->type
== OSPF_IFTYPE_VIRTUALLINK
)
424 if (if_is_loopback(oi
->ifp
))
427 if (CHECK_FLAG(oi
->connected
->flags
, ZEBRA_IFA_UNNUMBERED
))
429 else if (prefix_match(CONNECTED_PREFIX(oi
->connected
),
430 (struct prefix
*)&addr
)) {
431 if ((match
== NULL
) || (match
->address
->prefixlen
432 < oi
->address
->prefixlen
))
440 void ospf_if_stream_set(struct ospf_interface
*oi
)
442 /* set output fifo queue. */
443 if (oi
->obuf
== NULL
)
444 oi
->obuf
= ospf_fifo_new();
447 void ospf_if_stream_unset(struct ospf_interface
*oi
)
449 struct ospf
*ospf
= oi
->ospf
;
452 ospf_fifo_free(oi
->obuf
);
455 if (oi
->on_write_q
) {
456 listnode_delete(ospf
->oi_write_q
, oi
);
457 if (list_isempty(ospf
->oi_write_q
))
458 OSPF_TIMER_OFF(ospf
->t_write
);
465 static struct ospf_if_params
*ospf_new_if_params(void)
467 struct ospf_if_params
*oip
;
469 oip
= XCALLOC(MTYPE_OSPF_IF_PARAMS
, sizeof(struct ospf_if_params
));
474 UNSET_IF_PARAM(oip
, output_cost_cmd
);
475 UNSET_IF_PARAM(oip
, transmit_delay
);
476 UNSET_IF_PARAM(oip
, retransmit_interval
);
477 UNSET_IF_PARAM(oip
, passive_interface
);
478 UNSET_IF_PARAM(oip
, v_hello
);
479 UNSET_IF_PARAM(oip
, fast_hello
);
480 UNSET_IF_PARAM(oip
, v_wait
);
481 UNSET_IF_PARAM(oip
, priority
);
482 UNSET_IF_PARAM(oip
, type
);
483 UNSET_IF_PARAM(oip
, auth_simple
);
484 UNSET_IF_PARAM(oip
, auth_crypt
);
485 UNSET_IF_PARAM(oip
, auth_type
);
487 oip
->auth_crypt
= list_new();
489 oip
->network_lsa_seqnum
= htonl(OSPF_INITIAL_SEQUENCE_NUMBER
);
494 void ospf_del_if_params(struct ospf_if_params
*oip
)
496 list_delete(oip
->auth_crypt
);
497 bfd_info_free(&(oip
->bfd_info
));
498 XFREE(MTYPE_OSPF_IF_PARAMS
, oip
);
501 void ospf_free_if_params(struct interface
*ifp
, struct in_addr addr
)
503 struct ospf_if_params
*oip
;
504 struct prefix_ipv4 p
;
505 struct route_node
*rn
;
508 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
510 rn
= route_node_lookup(IF_OIFS_PARAMS(ifp
), (struct prefix
*)&p
);
511 if (!rn
|| !rn
->info
)
515 route_unlock_node(rn
);
517 if (!OSPF_IF_PARAM_CONFIGURED(oip
, output_cost_cmd
)
518 && !OSPF_IF_PARAM_CONFIGURED(oip
, transmit_delay
)
519 && !OSPF_IF_PARAM_CONFIGURED(oip
, retransmit_interval
)
520 && !OSPF_IF_PARAM_CONFIGURED(oip
, passive_interface
)
521 && !OSPF_IF_PARAM_CONFIGURED(oip
, v_hello
)
522 && !OSPF_IF_PARAM_CONFIGURED(oip
, fast_hello
)
523 && !OSPF_IF_PARAM_CONFIGURED(oip
, v_wait
)
524 && !OSPF_IF_PARAM_CONFIGURED(oip
, priority
)
525 && !OSPF_IF_PARAM_CONFIGURED(oip
, type
)
526 && !OSPF_IF_PARAM_CONFIGURED(oip
, auth_simple
)
527 && !OSPF_IF_PARAM_CONFIGURED(oip
, auth_type
)
528 && listcount(oip
->auth_crypt
) == 0
529 && ntohl(oip
->network_lsa_seqnum
) != OSPF_INITIAL_SEQUENCE_NUMBER
) {
530 ospf_del_if_params(oip
);
532 route_unlock_node(rn
);
536 struct ospf_if_params
*ospf_lookup_if_params(struct interface
*ifp
,
539 struct prefix_ipv4 p
;
540 struct route_node
*rn
;
543 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
546 rn
= route_node_lookup(IF_OIFS_PARAMS(ifp
), (struct prefix
*)&p
);
549 route_unlock_node(rn
);
556 struct ospf_if_params
*ospf_get_if_params(struct interface
*ifp
,
559 struct prefix_ipv4 p
;
560 struct route_node
*rn
;
563 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
566 rn
= route_node_get(IF_OIFS_PARAMS(ifp
), (struct prefix
*)&p
);
568 if (rn
->info
== NULL
)
569 rn
->info
= ospf_new_if_params();
571 route_unlock_node(rn
);
576 void ospf_if_update_params(struct interface
*ifp
, struct in_addr addr
)
578 struct route_node
*rn
;
579 struct ospf_interface
*oi
;
581 for (rn
= route_top(IF_OIFS(ifp
)); rn
; rn
= route_next(rn
)) {
582 if ((oi
= rn
->info
) == NULL
)
585 if (IPV4_ADDR_SAME(&oi
->address
->u
.prefix4
, &addr
))
586 oi
->params
= ospf_lookup_if_params(
587 ifp
, oi
->address
->u
.prefix4
);
591 int ospf_if_new_hook(struct interface
*ifp
)
595 ifp
->info
= XCALLOC(MTYPE_OSPF_IF_INFO
, sizeof(struct ospf_if_info
));
597 IF_OIFS(ifp
) = route_table_init();
598 IF_OIFS_PARAMS(ifp
) = route_table_init();
600 IF_DEF_PARAMS(ifp
) = ospf_new_if_params();
602 SET_IF_PARAM(IF_DEF_PARAMS(ifp
), transmit_delay
);
603 IF_DEF_PARAMS(ifp
)->transmit_delay
= OSPF_TRANSMIT_DELAY_DEFAULT
;
605 SET_IF_PARAM(IF_DEF_PARAMS(ifp
), retransmit_interval
);
606 IF_DEF_PARAMS(ifp
)->retransmit_interval
=
607 OSPF_RETRANSMIT_INTERVAL_DEFAULT
;
609 SET_IF_PARAM(IF_DEF_PARAMS(ifp
), priority
);
610 IF_DEF_PARAMS(ifp
)->priority
= OSPF_ROUTER_PRIORITY_DEFAULT
;
612 IF_DEF_PARAMS(ifp
)->mtu_ignore
= OSPF_MTU_IGNORE_DEFAULT
;
614 SET_IF_PARAM(IF_DEF_PARAMS(ifp
), v_hello
);
615 IF_DEF_PARAMS(ifp
)->v_hello
= OSPF_HELLO_INTERVAL_DEFAULT
;
617 SET_IF_PARAM(IF_DEF_PARAMS(ifp
), fast_hello
);
618 IF_DEF_PARAMS(ifp
)->fast_hello
= OSPF_FAST_HELLO_DEFAULT
;
620 SET_IF_PARAM(IF_DEF_PARAMS(ifp
), v_wait
);
621 IF_DEF_PARAMS(ifp
)->v_wait
= OSPF_ROUTER_DEAD_INTERVAL_DEFAULT
;
623 SET_IF_PARAM(IF_DEF_PARAMS(ifp
), auth_simple
);
624 memset(IF_DEF_PARAMS(ifp
)->auth_simple
, 0, OSPF_AUTH_SIMPLE_SIZE
);
626 SET_IF_PARAM(IF_DEF_PARAMS(ifp
), auth_type
);
627 IF_DEF_PARAMS(ifp
)->auth_type
= OSPF_AUTH_NOTSET
;
629 rc
= ospf_opaque_new_if(ifp
);
633 static int ospf_if_delete_hook(struct interface
*ifp
)
636 struct route_node
*rn
;
637 rc
= ospf_opaque_del_if(ifp
);
639 route_table_finish(IF_OIFS(ifp
));
641 for (rn
= route_top(IF_OIFS_PARAMS(ifp
)); rn
; rn
= route_next(rn
))
643 ospf_del_if_params(rn
->info
);
644 route_table_finish(IF_OIFS_PARAMS(ifp
));
646 ospf_del_if_params((struct ospf_if_params
*)IF_DEF_PARAMS(ifp
));
647 XFREE(MTYPE_OSPF_IF_INFO
, ifp
->info
);
653 int ospf_if_is_enable(struct ospf_interface
*oi
)
655 if (!if_is_loopback(oi
->ifp
))
656 if (if_is_up(oi
->ifp
))
662 void ospf_if_set_multicast(struct ospf_interface
*oi
)
664 if ((oi
->state
> ISM_Loopback
) && (oi
->type
!= OSPF_IFTYPE_LOOPBACK
)
665 && (oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
)
666 && (OSPF_IF_PASSIVE_STATUS(oi
) == OSPF_IF_ACTIVE
)) {
667 /* The interface should belong to the OSPF-all-routers group. */
668 if (!OI_MEMBER_CHECK(oi
, MEMBER_ALLROUTERS
)
669 && (ospf_if_add_allspfrouters(oi
->ospf
, oi
->address
,
672 /* Set the flag only if the system call to join
674 OI_MEMBER_JOINED(oi
, MEMBER_ALLROUTERS
);
676 /* The interface should NOT belong to the OSPF-all-routers
678 if (OI_MEMBER_CHECK(oi
, MEMBER_ALLROUTERS
)) {
679 /* Only actually drop if this is the last reference */
680 if (OI_MEMBER_COUNT(oi
, MEMBER_ALLROUTERS
) == 1)
681 ospf_if_drop_allspfrouters(oi
->ospf
,
684 /* Unset the flag regardless of whether the system call
686 the group succeeded, since it's much safer to assume
688 we are not a member. */
689 OI_MEMBER_LEFT(oi
, MEMBER_ALLROUTERS
);
693 if (((oi
->type
== OSPF_IFTYPE_BROADCAST
)
694 || (oi
->type
== OSPF_IFTYPE_POINTOPOINT
))
695 && ((oi
->state
== ISM_DR
) || (oi
->state
== ISM_Backup
))
696 && (OSPF_IF_PASSIVE_STATUS(oi
) == OSPF_IF_ACTIVE
)) {
697 /* The interface should belong to the OSPF-designated-routers
699 if (!OI_MEMBER_CHECK(oi
, MEMBER_DROUTERS
)
700 && (ospf_if_add_alldrouters(oi
->ospf
, oi
->address
,
703 /* Set the flag only if the system call to join
705 OI_MEMBER_JOINED(oi
, MEMBER_DROUTERS
);
707 /* The interface should NOT belong to the
708 * OSPF-designated-routers group */
709 if (OI_MEMBER_CHECK(oi
, MEMBER_DROUTERS
)) {
710 /* drop only if last reference */
711 if (OI_MEMBER_COUNT(oi
, MEMBER_DROUTERS
) == 1)
712 ospf_if_drop_alldrouters(oi
->ospf
, oi
->address
,
715 /* Unset the flag regardless of whether the system call
717 the group succeeded, since it's much safer to assume
719 we are not a member. */
720 OI_MEMBER_LEFT(oi
, MEMBER_DROUTERS
);
725 int ospf_if_up(struct ospf_interface
*oi
)
730 if (oi
->type
== OSPF_IFTYPE_LOOPBACK
)
731 OSPF_ISM_EVENT_SCHEDULE(oi
, ISM_LoopInd
);
733 ospf_if_stream_set(oi
);
734 OSPF_ISM_EVENT_SCHEDULE(oi
, ISM_InterfaceUp
);
740 int ospf_if_down(struct ospf_interface
*oi
)
745 OSPF_ISM_EVENT_EXECUTE(oi
, ISM_InterfaceDown
);
746 /* delete position in router LSA */
749 /* Shutdown packet reception and sending */
750 ospf_if_stream_unset(oi
);
756 /* Virtual Link related functions. */
758 struct ospf_vl_data
*ospf_vl_data_new(struct ospf_area
*area
,
759 struct in_addr vl_peer
)
761 struct ospf_vl_data
*vl_data
;
763 vl_data
= XCALLOC(MTYPE_OSPF_VL_DATA
, sizeof(struct ospf_vl_data
));
765 vl_data
->vl_peer
.s_addr
= vl_peer
.s_addr
;
766 vl_data
->vl_area_id
= area
->area_id
;
767 vl_data
->vl_area_id_fmt
= area
->area_id_fmt
;
772 void ospf_vl_data_free(struct ospf_vl_data
*vl_data
)
774 XFREE(MTYPE_OSPF_VL_DATA
, vl_data
);
777 u_int vlink_count
= 0;
779 struct ospf_interface
*ospf_vl_new(struct ospf
*ospf
,
780 struct ospf_vl_data
*vl_data
)
782 struct ospf_interface
*voi
;
783 struct interface
*vi
;
784 char ifname
[INTERFACE_NAMSIZ
+ 1];
785 struct ospf_area
*area
;
786 struct in_addr area_id
;
787 struct connected
*co
;
788 struct prefix_ipv4
*p
;
790 if (IS_DEBUG_OSPF_EVENT
)
791 zlog_debug("ospf_vl_new(): Start");
792 if (vlink_count
== OSPF_VL_MAX_COUNT
) {
793 if (IS_DEBUG_OSPF_EVENT
)
795 "ospf_vl_new(): Alarm: "
796 "cannot create more than OSPF_MAX_VL_COUNT virtual links");
800 if (IS_DEBUG_OSPF_EVENT
)
801 zlog_debug("ospf_vl_new(): creating pseudo zebra interface");
803 snprintf(ifname
, sizeof(ifname
), "VLINK%d", vlink_count
);
804 vi
= if_create(ifname
, strnlen(ifname
, sizeof(ifname
)), VRF_DEFAULT
);
806 * if_create sets ZEBRA_INTERFACE_LINKDETECTION
807 * virtual links don't need this.
809 UNSET_FLAG(vi
->status
, ZEBRA_INTERFACE_LINKDETECTION
);
810 co
= connected_new();
812 listnode_add(vi
->connected
, co
);
814 p
= prefix_ipv4_new();
816 p
->prefix
.s_addr
= 0;
819 co
->address
= (struct prefix
*)p
;
821 voi
= ospf_if_new(ospf
, vi
, co
->address
);
823 if (IS_DEBUG_OSPF_EVENT
)
825 "ospf_vl_new(): Alarm: OSPF int structure is not created");
829 voi
->vl_data
= vl_data
;
830 voi
->ifp
->mtu
= OSPF_VL_MTU
;
831 voi
->type
= OSPF_IFTYPE_VIRTUALLINK
;
834 if (IS_DEBUG_OSPF_EVENT
)
835 zlog_debug("ospf_vl_new(): Created name: %s", ifname
);
836 if (IS_DEBUG_OSPF_EVENT
)
837 zlog_debug("ospf_vl_new(): set if->name to %s", vi
->name
);
840 area
= ospf_area_get(ospf
, area_id
);
843 if (IS_DEBUG_OSPF_EVENT
)
845 "ospf_vl_new(): set associated area to the backbone");
847 /* Add pseudo neighbor. */
848 ospf_nbr_self_reset(voi
, voi
->ospf
->router_id
);
850 ospf_area_add_if(voi
->area
, voi
);
852 ospf_if_stream_set(voi
);
854 if (IS_DEBUG_OSPF_EVENT
)
855 zlog_debug("ospf_vl_new(): Stop");
859 static void ospf_vl_if_delete(struct ospf_vl_data
*vl_data
)
861 struct interface
*ifp
= vl_data
->vl_oi
->ifp
;
862 vl_data
->vl_oi
->address
->u
.prefix4
.s_addr
= 0;
863 vl_data
->vl_oi
->address
->prefixlen
= 0;
864 ospf_if_free(vl_data
->vl_oi
);
869 /* Look up vl_data for given peer, optionally qualified to be in the
870 * specified area. NULL area returns first found..
872 struct ospf_vl_data
*ospf_vl_lookup(struct ospf
*ospf
, struct ospf_area
*area
,
873 struct in_addr vl_peer
)
875 struct ospf_vl_data
*vl_data
;
876 struct listnode
*node
;
878 if (IS_DEBUG_OSPF_EVENT
) {
879 zlog_debug("%s: Looking for %s", __func__
, inet_ntoa(vl_peer
));
881 zlog_debug("%s: in area %s", __func__
,
882 inet_ntoa(area
->area_id
));
885 for (ALL_LIST_ELEMENTS_RO(ospf
->vlinks
, node
, vl_data
)) {
886 if (IS_DEBUG_OSPF_EVENT
)
887 zlog_debug("%s: VL %s, peer %s", __func__
,
888 vl_data
->vl_oi
->ifp
->name
,
889 inet_ntoa(vl_data
->vl_peer
));
892 && !IPV4_ADDR_SAME(&vl_data
->vl_area_id
, &area
->area_id
))
895 if (IPV4_ADDR_SAME(&vl_data
->vl_peer
, &vl_peer
))
902 static void ospf_vl_shutdown(struct ospf_vl_data
*vl_data
)
904 struct ospf_interface
*oi
;
906 if ((oi
= vl_data
->vl_oi
) == NULL
)
909 oi
->address
->u
.prefix4
.s_addr
= 0;
910 oi
->address
->prefixlen
= 0;
912 UNSET_FLAG(oi
->ifp
->flags
, IFF_UP
);
913 /* OSPF_ISM_EVENT_SCHEDULE (oi, ISM_InterfaceDown); */
914 OSPF_ISM_EVENT_EXECUTE(oi
, ISM_InterfaceDown
);
917 void ospf_vl_add(struct ospf
*ospf
, struct ospf_vl_data
*vl_data
)
919 listnode_add(ospf
->vlinks
, vl_data
);
920 hook_call(ospf_vl_add
, vl_data
);
923 void ospf_vl_delete(struct ospf
*ospf
, struct ospf_vl_data
*vl_data
)
925 ospf_vl_shutdown(vl_data
);
926 ospf_vl_if_delete(vl_data
);
928 hook_call(ospf_vl_delete
, vl_data
);
929 listnode_delete(ospf
->vlinks
, vl_data
);
931 ospf_vl_data_free(vl_data
);
934 static int ospf_vl_set_params(struct ospf_vl_data
*vl_data
, struct vertex
*v
)
937 struct ospf_interface
*voi
;
938 struct listnode
*node
;
939 struct vertex_parent
*vp
= NULL
;
941 struct router_lsa
*rl
;
943 voi
= vl_data
->vl_oi
;
945 if (voi
->output_cost
!= v
->distance
) {
947 voi
->output_cost
= v
->distance
;
951 for (ALL_LIST_ELEMENTS_RO(v
->parents
, node
, vp
)) {
952 vl_data
->nexthop
.oi
= vp
->nexthop
->oi
;
953 vl_data
->nexthop
.router
= vp
->nexthop
->router
;
955 if (!IPV4_ADDR_SAME(&voi
->address
->u
.prefix4
,
956 &vl_data
->nexthop
.oi
->address
->u
.prefix4
))
959 voi
->address
->u
.prefix4
=
960 vl_data
->nexthop
.oi
->address
->u
.prefix4
;
961 voi
->address
->prefixlen
=
962 vl_data
->nexthop
.oi
->address
->prefixlen
;
964 break; /* We take the first interface. */
967 rl
= (struct router_lsa
*)v
->lsa
;
969 /* use SPF determined backlink index in struct vertex
970 * for virtual link destination address
972 if (vp
&& vp
->backlink
>= 0) {
973 if (!IPV4_ADDR_SAME(&vl_data
->peer_addr
,
974 &rl
->link
[vp
->backlink
].link_data
))
976 vl_data
->peer_addr
= rl
->link
[vp
->backlink
].link_data
;
978 /* This is highly odd, there is no backlink index
979 * there should be due to the ospf_spf_has_link() check
980 * in SPF. Lets warn and try pick a link anyway.
982 zlog_warn("ospf_vl_set_params: No backlink for %s!",
983 vl_data
->vl_oi
->ifp
->name
);
984 for (i
= 0; i
< ntohs(rl
->links
); i
++) {
985 switch (rl
->link
[i
].type
) {
986 case LSA_LINK_TYPE_VIRTUALLINK
:
987 if (IS_DEBUG_OSPF_EVENT
)
989 "found back link through VL");
991 case LSA_LINK_TYPE_TRANSIT
:
992 case LSA_LINK_TYPE_POINTOPOINT
:
993 if (!IPV4_ADDR_SAME(&vl_data
->peer_addr
,
994 &rl
->link
[i
].link_data
))
996 vl_data
->peer_addr
= rl
->link
[i
].link_data
;
1001 if (IS_DEBUG_OSPF_EVENT
)
1002 zlog_debug("%s: %s peer address: %s, cost: %d,%schanged",
1003 __func__
, vl_data
->vl_oi
->ifp
->name
,
1004 inet_ntoa(vl_data
->peer_addr
), voi
->output_cost
,
1005 (changed
? " " : " un"));
1011 void ospf_vl_up_check(struct ospf_area
*area
, struct in_addr rid
,
1014 struct ospf
*ospf
= area
->ospf
;
1015 struct listnode
*node
;
1016 struct ospf_vl_data
*vl_data
;
1017 struct ospf_interface
*oi
;
1019 if (IS_DEBUG_OSPF_EVENT
) {
1020 zlog_debug("ospf_vl_up_check(): Start");
1021 zlog_debug("ospf_vl_up_check(): Router ID is %s",
1023 zlog_debug("ospf_vl_up_check(): Area is %s",
1024 inet_ntoa(area
->area_id
));
1027 for (ALL_LIST_ELEMENTS_RO(ospf
->vlinks
, node
, vl_data
)) {
1028 if (IS_DEBUG_OSPF_EVENT
) {
1029 zlog_debug("%s: considering VL, %s in area %s",
1030 __func__
, vl_data
->vl_oi
->ifp
->name
,
1031 inet_ntoa(vl_data
->vl_area_id
));
1032 zlog_debug("%s: peer ID: %s", __func__
,
1033 inet_ntoa(vl_data
->vl_peer
));
1036 if (IPV4_ADDR_SAME(&vl_data
->vl_peer
, &rid
)
1037 && IPV4_ADDR_SAME(&vl_data
->vl_area_id
, &area
->area_id
)) {
1038 oi
= vl_data
->vl_oi
;
1039 SET_FLAG(vl_data
->flags
, OSPF_VL_FLAG_APPROVED
);
1041 if (IS_DEBUG_OSPF_EVENT
)
1043 "ospf_vl_up_check(): this VL matched");
1045 if (oi
->state
== ISM_Down
) {
1046 if (IS_DEBUG_OSPF_EVENT
)
1048 "ospf_vl_up_check(): VL is down, waking it up");
1049 SET_FLAG(oi
->ifp
->flags
, IFF_UP
);
1050 OSPF_ISM_EVENT_EXECUTE(oi
, ISM_InterfaceUp
);
1053 if (ospf_vl_set_params(vl_data
, v
)) {
1054 if (IS_DEBUG_OSPF(ism
, ISM_EVENTS
))
1056 "ospf_vl_up_check: VL cost change,"
1057 " scheduling router lsa refresh");
1059 ospf_router_lsa_update_area(
1061 else if (IS_DEBUG_OSPF(ism
, ISM_EVENTS
))
1063 "ospf_vl_up_check: VL cost change, no backbone!");
1069 void ospf_vl_unapprove(struct ospf
*ospf
)
1071 struct listnode
*node
;
1072 struct ospf_vl_data
*vl_data
;
1074 for (ALL_LIST_ELEMENTS_RO(ospf
->vlinks
, node
, vl_data
))
1075 UNSET_FLAG(vl_data
->flags
, OSPF_VL_FLAG_APPROVED
);
1078 void ospf_vl_shut_unapproved(struct ospf
*ospf
)
1080 struct listnode
*node
, *nnode
;
1081 struct ospf_vl_data
*vl_data
;
1083 for (ALL_LIST_ELEMENTS(ospf
->vlinks
, node
, nnode
, vl_data
))
1084 if (!CHECK_FLAG(vl_data
->flags
, OSPF_VL_FLAG_APPROVED
))
1085 ospf_vl_shutdown(vl_data
);
1088 int ospf_full_virtual_nbrs(struct ospf_area
*area
)
1090 if (IS_DEBUG_OSPF_EVENT
) {
1092 "counting fully adjacent virtual neighbors in area %s",
1093 inet_ntoa(area
->area_id
));
1094 zlog_debug("there are %d of them", area
->full_vls
);
1097 return area
->full_vls
;
1100 int ospf_vls_in_area(struct ospf_area
*area
)
1102 struct listnode
*node
;
1103 struct ospf_vl_data
*vl_data
;
1106 for (ALL_LIST_ELEMENTS_RO(area
->ospf
->vlinks
, node
, vl_data
))
1107 if (IPV4_ADDR_SAME(&vl_data
->vl_area_id
, &area
->area_id
))
1114 struct crypt_key
*ospf_crypt_key_new()
1116 return XCALLOC(MTYPE_OSPF_CRYPT_KEY
, sizeof(struct crypt_key
));
1119 void ospf_crypt_key_add(struct list
*crypt
, struct crypt_key
*ck
)
1121 listnode_add(crypt
, ck
);
1124 struct crypt_key
*ospf_crypt_key_lookup(struct list
*auth_crypt
, u_char key_id
)
1126 struct listnode
*node
;
1127 struct crypt_key
*ck
;
1129 for (ALL_LIST_ELEMENTS_RO(auth_crypt
, node
, ck
))
1130 if (ck
->key_id
== key_id
)
1136 int ospf_crypt_key_delete(struct list
*auth_crypt
, u_char key_id
)
1138 struct listnode
*node
, *nnode
;
1139 struct crypt_key
*ck
;
1141 for (ALL_LIST_ELEMENTS(auth_crypt
, node
, nnode
, ck
)) {
1142 if (ck
->key_id
== key_id
) {
1143 listnode_delete(auth_crypt
, ck
);
1144 XFREE(MTYPE_OSPF_CRYPT_KEY
, ck
);
1152 u_char
ospf_default_iftype(struct interface
*ifp
)
1154 if (if_is_pointopoint(ifp
))
1155 return OSPF_IFTYPE_POINTOPOINT
;
1156 else if (if_is_loopback(ifp
))
1157 return OSPF_IFTYPE_LOOPBACK
;
1159 return OSPF_IFTYPE_BROADCAST
;
1164 /* Initialize Zebra interface data structure. */
1165 om
->iflist
= vrf_iflist(VRF_DEFAULT
);
1166 if_add_hook(IF_NEW_HOOK
, ospf_if_new_hook
);
1167 if_add_hook(IF_DELETE_HOOK
, ospf_if_delete_hook
);