+ fix the bug reported by Milan Kocian (IPv6 route handling was broken by the RIB...
[jleu-quagga.git] / ospfd / ospf_interface.c
blob636814297e148fce6f6e069eefbc97c0936a2492
1 /*
2 * OSPF Interface functions.
3 * Copyright (C) 1999, 2000 Toshiaki Takada
5 * This file is part of GNU Zebra.
6 *
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.
23 #include <zebra.h>
25 #include "thread.h"
26 #include "linklist.h"
27 #include "prefix.h"
28 #include "if.h"
29 #include "table.h"
30 #include "memory.h"
31 #include "command.h"
32 #include "stream.h"
33 #include "log.h"
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"
48 #ifdef HAVE_SNMP
49 #include "ospfd/ospf_snmp.h"
50 #endif /* HAVE_SNMP */
53 int
54 ospf_if_get_output_cost (struct ospf_interface *oi)
56 /* If all else fails, use default OSPF cost */
57 u_int32_t cost;
58 u_int32_t bw, refbw;
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. */
69 else
71 cost = (u_int32_t) ((double)refbw / (double)bw + (double)0.5);
72 if (cost < 1)
73 cost = 1;
74 else if (cost > 65535)
75 cost = 65535;
78 return cost;
81 void
82 ospf_if_recalculate_output_cost (struct interface *ifp)
84 u_int32_t newcost;
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)
92 continue;
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_timer_add (oi->area);
105 /* Simulate down/up on the interface. This is needed, for example, when
106 the MTU changes. */
107 void
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)
117 continue;
119 ospf_if_down(oi);
120 ospf_if_up(oi);
124 void
125 ospf_if_reset_variables (struct ospf_interface *oi)
127 /* Set default values. */
128 /* don't clear this flag. oi->flag = OSPF_IF_DISABLE; */
130 if (oi->vl_data)
131 oi->type = OSPF_IFTYPE_VIRTUALLINK;
132 else
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 */
144 oi->v_ls_ack = 1;
147 /* lookup oi for specified prefix/ifp */
148 static struct ospf_interface *
149 ospf_if_table_lookup (struct interface *ifp, struct prefix *prefix)
151 struct prefix p;
152 struct route_node *rn;
153 struct ospf_interface *rninfo = NULL;
155 p = *prefix;
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);
165 return rninfo;
168 static void
169 ospf_add_to_if (struct interface *ifp, struct ospf_interface *oi)
171 struct route_node *rn;
172 struct prefix p;
174 p = *oi->address;
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);
182 rn->info = oi;
185 static void
186 ospf_delete_from_if (struct interface *ifp, struct ospf_interface *oi)
188 struct route_node *rn;
189 struct prefix p;
191 p = *oi->address;
192 p.prefixlen = IPV4_MAX_PREFIXLEN;
194 rn = route_node_lookup (IF_OIFS (oi->ifp), &p);
195 assert (rn);
196 assert (rn->info);
197 rn->info = NULL;
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));
212 else
213 return oi;
215 /* Set zebra interface pointer. */
216 oi->ifp = ifp;
217 oi->address = p;
219 ospf_add_to_if (ifp, oi);
220 listnode_add (ospf->oiflist, oi);
222 /* Clear self-originated network-LSA. */
223 oi->network_lsa_self = NULL;
225 /* Initialize neighbor list. */
226 oi->nbrs = route_table_init ();
228 /* Initialize static neighbor list. */
229 oi->nbr_nbma = list_new ();
231 /* Initialize Link State Acknowledgment list. */
232 oi->ls_ack = list_new ();
233 oi->ls_ack_direct.ls_ack = list_new ();
235 /* Set default values. */
236 ospf_if_reset_variables (oi);
238 /* Add pseudo neighbor. */
239 oi->nbr_self = ospf_nbr_new (oi);
241 oi->ls_upd_queue = route_table_init ();
242 oi->t_ls_upd_event = NULL;
243 oi->t_ls_ack_direct = NULL;
245 oi->crypt_seqnum = time (NULL);
247 #ifdef HAVE_OPAQUE_LSA
248 ospf_opaque_type9_lsa_init (oi);
249 #endif /* HAVE_OPAQUE_LSA */
251 oi->ospf = ospf;
253 return oi;
256 /* Restore an interface to its pre UP state
257 Used from ism_interface_down only */
258 void
259 ospf_if_cleanup (struct ospf_interface *oi)
261 struct route_node *rn;
262 struct listnode *node, *nnode;
263 struct ospf_neighbor *nbr;
264 struct ospf_nbr_nbma *nbr_nbma;
265 struct ospf_lsa *lsa;
267 /* oi->nbrs and oi->nbr_nbma should be deleted on InterfaceDown event */
268 /* delete all static neighbors attached to this interface */
269 for (ALL_LIST_ELEMENTS (oi->nbr_nbma, node, nnode, nbr_nbma))
271 OSPF_POLL_TIMER_OFF (nbr_nbma->t_poll);
273 if (nbr_nbma->nbr)
275 nbr_nbma->nbr->nbr_nbma = NULL;
276 nbr_nbma->nbr = NULL;
279 nbr_nbma->oi = NULL;
281 listnode_delete (oi->nbr_nbma, nbr_nbma);
284 /* send Neighbor event KillNbr to all associated neighbors. */
285 for (rn = route_top (oi->nbrs); rn; rn = route_next (rn))
286 if ((nbr = rn->info) != NULL)
287 if (nbr != oi->nbr_self)
288 OSPF_NSM_EVENT_EXECUTE (nbr, NSM_KillNbr);
290 /* Cleanup Link State Acknowlegdment list. */
291 for (ALL_LIST_ELEMENTS (oi->ls_ack, node, nnode, lsa))
292 ospf_lsa_unlock (&lsa); /* oi->ls_ack */
293 list_delete_all_node (oi->ls_ack);
295 oi->crypt_seqnum = 0;
297 /* Empty link state update queue */
298 ospf_ls_upd_queue_empty (oi);
300 /* Reset pseudo neighbor. */
301 ospf_nbr_delete (oi->nbr_self);
302 oi->nbr_self = ospf_nbr_new (oi);
303 ospf_nbr_add_self (oi);
305 ospf_lsa_unlock (&oi->network_lsa_self);
306 oi->network_lsa_self = NULL;
307 OSPF_TIMER_OFF (oi->t_network_lsa_self);
310 void
311 ospf_if_free (struct ospf_interface *oi)
313 ospf_if_down (oi);
315 assert (oi->state == ISM_Down);
317 #ifdef HAVE_OPAQUE_LSA
318 ospf_opaque_type9_lsa_term (oi);
319 #endif /* HAVE_OPAQUE_LSA */
321 /* Free Pseudo Neighbour */
322 ospf_nbr_delete (oi->nbr_self);
324 route_table_finish (oi->nbrs);
325 route_table_finish (oi->ls_upd_queue);
327 /* Free any lists that should be freed */
328 list_free (oi->nbr_nbma);
330 list_free (oi->ls_ack);
331 list_free (oi->ls_ack_direct.ls_ack);
333 ospf_delete_from_if (oi->ifp, oi);
335 listnode_delete (oi->ospf->oiflist, oi);
336 listnode_delete (oi->area->oiflist, oi);
338 memset (oi, 0, sizeof (*oi));
339 XFREE (MTYPE_OSPF_IF, oi);
344 * check if interface with given address is configured and
345 * return it if yes. special treatment for PtP networks.
347 struct ospf_interface *
348 ospf_if_is_configured (struct ospf *ospf, struct in_addr *address)
350 struct listnode *node, *nnode;
351 struct ospf_interface *oi;
352 struct prefix_ipv4 addr;
354 addr.family = AF_INET;
355 addr.prefix = *address;
356 addr.prefixlen = IPV4_MAX_PREFIXLEN;
358 for (ALL_LIST_ELEMENTS (ospf->oiflist, node, nnode, oi))
359 if (oi->type != OSPF_IFTYPE_VIRTUALLINK)
361 if (oi->type == OSPF_IFTYPE_POINTOPOINT)
363 /* special leniency: match if addr is anywhere on peer subnet */
364 if (prefix_match(CONNECTED_PREFIX(oi->connected),
365 (struct prefix *)&addr))
366 return oi;
368 else
370 if (IPV4_ADDR_SAME (address, &oi->address->u.prefix4))
371 return oi;
374 return NULL;
378 ospf_if_is_up (struct ospf_interface *oi)
380 return if_is_up (oi->ifp);
383 struct ospf_interface *
384 ospf_if_exists (struct ospf_interface *oic)
386 struct listnode *node;
387 struct ospf *ospf;
388 struct ospf_interface *oi;
390 if ((ospf = ospf_lookup ()) == NULL)
391 return NULL;
393 for (ALL_LIST_ELEMENTS_RO (ospf->oiflist, node, oi))
394 if (oi == oic)
395 return oi;
397 return NULL;
400 struct ospf_interface *
401 ospf_if_lookup_by_local_addr (struct ospf *ospf,
402 struct interface *ifp, struct in_addr address)
404 struct listnode *node;
405 struct ospf_interface *oi;
407 for (ALL_LIST_ELEMENTS_RO (ospf->oiflist, node, oi))
408 if (oi->type != OSPF_IFTYPE_VIRTUALLINK)
410 if (ifp && oi->ifp != ifp)
411 continue;
413 if (IPV4_ADDR_SAME (&address, &oi->address->u.prefix4))
414 return oi;
417 return NULL;
420 struct ospf_interface *
421 ospf_if_lookup_by_prefix (struct ospf *ospf, struct prefix_ipv4 *p)
423 struct listnode *node;
424 struct ospf_interface *oi;
426 /* Check each Interface. */
427 for (ALL_LIST_ELEMENTS_RO (ospf->oiflist, node, oi))
429 if (oi->type != OSPF_IFTYPE_VIRTUALLINK)
431 struct prefix ptmp;
433 prefix_copy (&ptmp, CONNECTED_PREFIX(oi->connected));
434 apply_mask (&ptmp);
435 if (prefix_same (&ptmp, (struct prefix *) p))
436 return oi;
439 return NULL;
442 /* determine receiving interface by source of packet */
443 struct ospf_interface *
444 ospf_if_lookup_recv_if (struct ospf *ospf, struct in_addr src)
446 struct listnode *node;
447 struct prefix_ipv4 addr;
448 struct ospf_interface *oi, *match;
450 addr.family = AF_INET;
451 addr.prefix = src;
452 addr.prefixlen = IPV4_MAX_BITLEN;
454 match = NULL;
456 for (ALL_LIST_ELEMENTS_RO (ospf->oiflist, node, oi))
458 if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
459 continue;
461 if (if_is_loopback (oi->ifp))
462 continue;
464 if (prefix_match (CONNECTED_PREFIX(oi->connected),
465 (struct prefix *) &addr))
467 if ( (match == NULL) ||
468 (match->address->prefixlen < oi->address->prefixlen)
470 match = oi;
474 return match;
477 void
478 ospf_if_stream_set (struct ospf_interface *oi)
480 /* set output fifo queue. */
481 if (oi->obuf == NULL)
482 oi->obuf = ospf_fifo_new ();
485 void
486 ospf_if_stream_unset (struct ospf_interface *oi)
488 struct ospf *ospf = oi->ospf;
490 if (oi->obuf)
492 ospf_fifo_free (oi->obuf);
493 oi->obuf = NULL;
495 if (oi->on_write_q)
497 listnode_delete (ospf->oi_write_q, oi);
498 if (list_isempty(ospf->oi_write_q))
499 OSPF_TIMER_OFF (ospf->t_write);
500 oi->on_write_q = 0;
506 static struct ospf_if_params *
507 ospf_new_if_params (void)
509 struct ospf_if_params *oip;
511 oip = XMALLOC (MTYPE_OSPF_IF_PARAMS, sizeof (struct ospf_if_params));
513 if (!oip)
514 return NULL;
516 memset (oip, 0, sizeof (struct ospf_if_params));
518 UNSET_IF_PARAM (oip, output_cost_cmd);
519 UNSET_IF_PARAM (oip, transmit_delay);
520 UNSET_IF_PARAM (oip, retransmit_interval);
521 UNSET_IF_PARAM (oip, passive_interface);
522 UNSET_IF_PARAM (oip, v_hello);
523 UNSET_IF_PARAM (oip, fast_hello);
524 UNSET_IF_PARAM (oip, v_wait);
525 UNSET_IF_PARAM (oip, priority);
526 UNSET_IF_PARAM (oip, type);
527 UNSET_IF_PARAM (oip, auth_simple);
528 UNSET_IF_PARAM (oip, auth_crypt);
529 UNSET_IF_PARAM (oip, auth_type);
531 oip->auth_crypt = list_new ();
533 return oip;
536 void
537 ospf_del_if_params (struct ospf_if_params *oip)
539 list_delete (oip->auth_crypt);
540 XFREE (MTYPE_OSPF_IF_PARAMS, oip);
543 void
544 ospf_free_if_params (struct interface *ifp, struct in_addr addr)
546 struct ospf_if_params *oip;
547 struct prefix_ipv4 p;
548 struct route_node *rn;
550 p.family = AF_INET;
551 p.prefixlen = IPV4_MAX_PREFIXLEN;
552 p.prefix = addr;
553 rn = route_node_lookup (IF_OIFS_PARAMS (ifp), (struct prefix*)&p);
554 if (!rn || !rn->info)
555 return;
557 oip = rn->info;
558 route_unlock_node (rn);
560 if (!OSPF_IF_PARAM_CONFIGURED (oip, output_cost_cmd) &&
561 !OSPF_IF_PARAM_CONFIGURED (oip, transmit_delay) &&
562 !OSPF_IF_PARAM_CONFIGURED (oip, retransmit_interval) &&
563 !OSPF_IF_PARAM_CONFIGURED (oip, passive_interface) &&
564 !OSPF_IF_PARAM_CONFIGURED (oip, v_hello) &&
565 !OSPF_IF_PARAM_CONFIGURED (oip, fast_hello) &&
566 !OSPF_IF_PARAM_CONFIGURED (oip, v_wait) &&
567 !OSPF_IF_PARAM_CONFIGURED (oip, priority) &&
568 !OSPF_IF_PARAM_CONFIGURED (oip, type) &&
569 !OSPF_IF_PARAM_CONFIGURED (oip, auth_simple) &&
570 !OSPF_IF_PARAM_CONFIGURED (oip, auth_type) &&
571 listcount (oip->auth_crypt) == 0)
573 ospf_del_if_params (oip);
574 rn->info = NULL;
575 route_unlock_node (rn);
579 struct ospf_if_params *
580 ospf_lookup_if_params (struct interface *ifp, struct in_addr addr)
582 struct prefix_ipv4 p;
583 struct route_node *rn;
585 p.family = AF_INET;
586 p.prefixlen = IPV4_MAX_PREFIXLEN;
587 p.prefix = addr;
589 rn = route_node_lookup (IF_OIFS_PARAMS (ifp), (struct prefix*)&p);
591 if (rn)
593 route_unlock_node (rn);
594 return rn->info;
597 return NULL;
600 struct ospf_if_params *
601 ospf_get_if_params (struct interface *ifp, struct in_addr addr)
603 struct prefix_ipv4 p;
604 struct route_node *rn;
606 p.family = AF_INET;
607 p.prefixlen = IPV4_MAX_PREFIXLEN;
608 p.prefix = addr;
610 rn = route_node_get (IF_OIFS_PARAMS (ifp), (struct prefix*)&p);
612 if (rn->info == NULL)
613 rn->info = ospf_new_if_params ();
614 else
615 route_unlock_node (rn);
617 return rn->info;
620 void
621 ospf_if_update_params (struct interface *ifp, struct in_addr addr)
623 struct route_node *rn;
624 struct ospf_interface *oi;
626 for (rn = route_top (IF_OIFS (ifp)); rn; rn = route_next (rn))
628 if ((oi = rn->info) == NULL)
629 continue;
631 if (IPV4_ADDR_SAME (&oi->address->u.prefix4, &addr))
632 oi->params = ospf_lookup_if_params (ifp, oi->address->u.prefix4);
637 ospf_if_new_hook (struct interface *ifp)
639 int rc = 0;
641 ifp->info = XMALLOC (MTYPE_OSPF_IF_INFO, sizeof (struct ospf_if_info));
642 memset (ifp->info, 0, sizeof (struct ospf_if_info));
644 IF_OIFS (ifp) = route_table_init ();
645 IF_OIFS_PARAMS (ifp) = route_table_init ();
647 IF_DEF_PARAMS (ifp) = ospf_new_if_params ();
649 SET_IF_PARAM (IF_DEF_PARAMS (ifp), transmit_delay);
650 IF_DEF_PARAMS (ifp)->transmit_delay = OSPF_TRANSMIT_DELAY_DEFAULT;
652 SET_IF_PARAM (IF_DEF_PARAMS (ifp), retransmit_interval);
653 IF_DEF_PARAMS (ifp)->retransmit_interval = OSPF_RETRANSMIT_INTERVAL_DEFAULT;
655 SET_IF_PARAM (IF_DEF_PARAMS (ifp), priority);
656 IF_DEF_PARAMS (ifp)->priority = OSPF_ROUTER_PRIORITY_DEFAULT;
658 IF_DEF_PARAMS (ifp)->mtu_ignore = OSPF_MTU_IGNORE_DEFAULT;
660 SET_IF_PARAM (IF_DEF_PARAMS (ifp), v_hello);
661 IF_DEF_PARAMS (ifp)->v_hello = OSPF_HELLO_INTERVAL_DEFAULT;
663 SET_IF_PARAM (IF_DEF_PARAMS (ifp), fast_hello);
664 IF_DEF_PARAMS (ifp)->fast_hello = OSPF_FAST_HELLO_DEFAULT;
666 SET_IF_PARAM (IF_DEF_PARAMS (ifp), v_wait);
667 IF_DEF_PARAMS (ifp)->v_wait = OSPF_ROUTER_DEAD_INTERVAL_DEFAULT;
669 SET_IF_PARAM (IF_DEF_PARAMS (ifp), auth_simple);
670 memset (IF_DEF_PARAMS (ifp)->auth_simple, 0, OSPF_AUTH_SIMPLE_SIZE);
672 SET_IF_PARAM (IF_DEF_PARAMS (ifp), auth_type);
673 IF_DEF_PARAMS (ifp)->auth_type = OSPF_AUTH_NOTSET;
675 #ifdef HAVE_OPAQUE_LSA
676 rc = ospf_opaque_new_if (ifp);
677 #endif /* HAVE_OPAQUE_LSA */
678 return rc;
681 static int
682 ospf_if_delete_hook (struct interface *ifp)
684 int rc = 0;
685 struct route_node *rn;
686 #ifdef HAVE_OPAQUE_LSA
687 rc = ospf_opaque_del_if (ifp);
688 #endif /* HAVE_OPAQUE_LSA */
690 route_table_finish (IF_OIFS (ifp));
692 for (rn = route_top (IF_OIFS_PARAMS (ifp)); rn; rn = route_next (rn))
693 if (rn->info)
694 ospf_del_if_params (rn->info);
695 route_table_finish (IF_OIFS_PARAMS (ifp));
697 ospf_del_if_params ((struct ospf_if_params *) IF_DEF_PARAMS (ifp));
698 XFREE (MTYPE_OSPF_IF_INFO, ifp->info);
699 ifp->info = NULL;
701 return rc;
705 ospf_if_is_enable (struct ospf_interface *oi)
707 if (!if_is_loopback (oi->ifp))
708 if (if_is_up (oi->ifp))
709 return 1;
711 return 0;
714 void
715 ospf_if_set_multicast(struct ospf_interface *oi)
717 if ((oi->state > ISM_Loopback) &&
718 (oi->type != OSPF_IFTYPE_LOOPBACK) &&
719 (oi->type != OSPF_IFTYPE_VIRTUALLINK) &&
720 (OSPF_IF_PASSIVE_STATUS(oi) == OSPF_IF_ACTIVE))
722 /* The interface should belong to the OSPF-all-routers group. */
723 if (!OI_MEMBER_CHECK(oi, MEMBER_ALLROUTERS) &&
724 (ospf_if_add_allspfrouters(oi->ospf, oi->address,
725 oi->ifp->ifindex) >= 0))
726 /* Set the flag only if the system call to join succeeded. */
727 OI_MEMBER_JOINED(oi, MEMBER_ALLROUTERS);
729 else
731 /* The interface should NOT belong to the OSPF-all-routers group. */
732 if (OI_MEMBER_CHECK(oi, MEMBER_ALLROUTERS))
734 /* Only actually drop if this is the last reference */
735 if (OI_MEMBER_COUNT(oi, MEMBER_ALLROUTERS) == 1)
736 ospf_if_drop_allspfrouters (oi->ospf, oi->address,
737 oi->ifp->ifindex);
738 /* Unset the flag regardless of whether the system call to leave
739 the group succeeded, since it's much safer to assume that
740 we are not a member. */
741 OI_MEMBER_LEFT(oi,MEMBER_ALLROUTERS);
745 if (((oi->type == OSPF_IFTYPE_BROADCAST) ||
746 (oi->type == OSPF_IFTYPE_POINTOPOINT)) &&
747 ((oi->state == ISM_DR) || (oi->state == ISM_Backup)) &&
748 (OSPF_IF_PASSIVE_STATUS(oi) == OSPF_IF_ACTIVE))
750 /* The interface should belong to the OSPF-designated-routers group. */
751 if (!OI_MEMBER_CHECK(oi, MEMBER_DROUTERS) &&
752 (ospf_if_add_alldrouters(oi->ospf, oi->address,
753 oi->ifp->ifindex) >= 0))
754 /* Set the flag only if the system call to join succeeded. */
755 OI_MEMBER_JOINED(oi, MEMBER_DROUTERS);
757 else
759 /* The interface should NOT belong to the OSPF-designated-routers group */
760 if (OI_MEMBER_CHECK(oi, MEMBER_DROUTERS))
762 /* drop only if last reference */
763 if (OI_MEMBER_COUNT(oi, MEMBER_DROUTERS) == 1)
764 ospf_if_drop_alldrouters(oi->ospf, oi->address, oi->ifp->ifindex);
766 /* Unset the flag regardless of whether the system call to leave
767 the group succeeded, since it's much safer to assume that
768 we are not a member. */
769 OI_MEMBER_LEFT(oi, MEMBER_DROUTERS);
775 ospf_if_up (struct ospf_interface *oi)
777 if (oi == NULL)
778 return 0;
780 if (oi->type == OSPF_IFTYPE_LOOPBACK)
781 OSPF_ISM_EVENT_SCHEDULE (oi, ISM_LoopInd);
782 else
784 struct ospf *ospf = ospf_lookup ();
785 if (ospf != NULL)
786 ospf_adjust_sndbuflen (ospf, oi->ifp->mtu);
787 else
788 zlog_warn ("%s: ospf_lookup() returned NULL", __func__);
789 ospf_if_stream_set (oi);
790 OSPF_ISM_EVENT_SCHEDULE (oi, ISM_InterfaceUp);
793 return 1;
797 ospf_if_down (struct ospf_interface *oi)
799 if (oi == NULL)
800 return 0;
802 OSPF_ISM_EVENT_EXECUTE (oi, ISM_InterfaceDown);
803 /* Shutdown packet reception and sending */
804 ospf_if_stream_unset (oi);
806 return 1;
810 /* Virtual Link related functions. */
812 struct ospf_vl_data *
813 ospf_vl_data_new (struct ospf_area *area, struct in_addr vl_peer)
815 struct ospf_vl_data *vl_data;
817 vl_data = XMALLOC (MTYPE_OSPF_VL_DATA, sizeof (struct ospf_vl_data));
818 memset (vl_data, 0, 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;
824 return vl_data;
827 void
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");
853 return NULL;
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 ();
862 co->ifp = vi;
863 listnode_add (vi->connected, co);
865 p = prefix_ipv4_new ();
866 p->family = AF_INET;
867 p->prefix.s_addr = 0;
868 p->prefixlen = 0;
870 co->address = (struct prefix *)p;
872 voi = ospf_if_new (ospf, vi, co->address);
873 if (voi == NULL)
875 if (IS_DEBUG_OSPF_EVENT)
876 zlog_debug ("ospf_vl_new(): Alarm: OSPF int structure is not created");
877 return NULL;
879 voi->connected = co;
880 voi->vl_data = vl_data;
881 voi->ifp->mtu = OSPF_VL_MTU;
882 voi->type = OSPF_IFTYPE_VIRTUALLINK;
884 vlink_count++;
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);
890 area_id.s_addr = 0;
891 area = ospf_area_get (ospf, area_id, OSPF_AREA_ID_FORMAT_ADDRESS);
892 voi->area = area;
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");
904 return voi;
907 static void
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);
914 if_delete (ifp);
915 vlink_count--;
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));
931 if (area)
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))
943 continue;
945 if (IPV4_ADDR_SAME (&vl_data->vl_peer, &vl_peer))
946 return vl_data;
949 return NULL;
952 static void
953 ospf_vl_shutdown (struct ospf_vl_data *vl_data)
955 struct ospf_interface *oi;
957 if ((oi = vl_data->vl_oi) == NULL)
958 return;
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);
968 void
969 ospf_vl_add (struct ospf *ospf, struct ospf_vl_data *vl_data)
971 listnode_add (ospf->vlinks, vl_data);
972 #ifdef HAVE_SNMP
973 ospf_snmp_vl_add (vl_data);
974 #endif /* HAVE_SNMP */
977 void
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);
983 #ifdef HAVE_SNMP
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);
991 static int
992 ospf_vl_set_params (struct ospf_vl_data *vl_data, struct vertex *v)
994 int changed = 0;
995 struct ospf_interface *voi;
996 struct listnode *node;
997 struct vertex_parent *vp = NULL;
998 int i;
999 struct router_lsa *rl;
1001 voi = vl_data->vl_oi;
1003 if (voi->output_cost != v->distance)
1006 voi->output_cost = v->distance;
1007 changed = 1;
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))
1017 changed = 1;
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))
1034 changed = 1;
1035 vl_data->peer_addr = rl->link[vp->backlink].link_data;
1037 else
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))
1056 changed = 1;
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),
1066 voi->output_cost,
1067 (changed ? " " : " un"));
1069 return changed;
1073 void
1074 ospf_vl_up_check (struct ospf_area *area, struct in_addr rid,
1075 struct vertex *v)
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");
1122 if(ospf->backbone)
1123 ospf_router_lsa_timer_add (ospf->backbone);
1124 else if (IS_DEBUG_OSPF (ism, ISM_EVENTS))
1125 zlog_debug ("ospf_vl_up_check: VL cost change, no backbone!");
1131 void
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);
1141 void
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;
1170 int c = 0;
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))
1174 c++;
1176 return c;
1180 struct crypt_key *
1181 ospf_crypt_key_new ()
1183 struct crypt_key *ck;
1185 ck = XMALLOC (MTYPE_OSPF_CRYPT_KEY, sizeof (struct crypt_key));
1186 memset (ck, 0, sizeof (struct crypt_key));
1188 return ck;
1191 void
1192 ospf_crypt_key_add (struct list *crypt, struct crypt_key *ck)
1194 listnode_add (crypt, ck);
1197 struct crypt_key *
1198 ospf_crypt_key_lookup (struct list *auth_crypt, u_char key_id)
1200 struct listnode *node;
1201 struct crypt_key *ck;
1203 for (ALL_LIST_ELEMENTS_RO (auth_crypt, node, ck))
1204 if (ck->key_id == key_id)
1205 return ck;
1207 return NULL;
1211 ospf_crypt_key_delete (struct list *auth_crypt, u_char key_id)
1213 struct listnode *node, *nnode;
1214 struct crypt_key *ck;
1216 for (ALL_LIST_ELEMENTS (auth_crypt, node, nnode, ck))
1218 if (ck->key_id == key_id)
1220 listnode_delete (auth_crypt, ck);
1221 XFREE (MTYPE_OSPF_CRYPT_KEY, ck);
1222 return 1;
1226 return 0;
1229 u_char
1230 ospf_default_iftype(struct interface *ifp)
1232 if (if_is_pointopoint (ifp))
1233 return OSPF_IFTYPE_POINTOPOINT;
1234 else if (if_is_loopback (ifp))
1235 return OSPF_IFTYPE_LOOPBACK;
1236 else
1237 return OSPF_IFTYPE_BROADCAST;
1240 void
1241 ospf_if_init ()
1243 /* Initialize Zebra interface data structure. */
1244 if_init ();
1245 om->iflist = iflist;
1246 if_add_hook (IF_NEW_HOOK, ospf_if_new_hook);
1247 if_add_hook (IF_DELETE_HOOK, ospf_if_delete_hook);