]> git.proxmox.com Git - mirror_frr.git/blame_incremental - ospfd/ospf_interface.c
zebra: Export zclient_socket_un().
[mirror_frr.git] / ospfd / ospf_interface.c
... / ...
CommitLineData
1/*
2 * OSPF Interface functions.
3 * Copyright (C) 1999, 2000 Toshiaki Takada
4 *
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.
11 *
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.
16 *
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.
21 */
22
23#include <zebra.h>
24
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"
34#include "zclient.h"
35#include "bfd.h"
36
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"
50#ifdef HAVE_SNMP
51#include "ospfd/ospf_snmp.h"
52#endif /* HAVE_SNMP */
53
54
55int
56ospf_if_get_output_cost (struct ospf_interface *oi)
57{
58 /* If all else fails, use default OSPF cost */
59 u_int32_t cost;
60 u_int32_t bw, refbw;
61
62 bw = oi->ifp->bandwidth ? oi->ifp->bandwidth : OSPF_DEFAULT_BANDWIDTH;
63 refbw = oi->ospf->ref_bandwidth;
64
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. */
71 else
72 {
73 cost = (u_int32_t) ((double)refbw / (double)bw + (double)0.5);
74 if (cost < 1)
75 cost = 1;
76 else if (cost > 65535)
77 cost = 65535;
78 }
79
80 return cost;
81}
82
83void
84ospf_if_recalculate_output_cost (struct interface *ifp)
85{
86 u_int32_t newcost;
87 struct route_node *rn;
88
89 for (rn = route_top (IF_OIFS (ifp)); rn; rn = route_next (rn))
90 {
91 struct ospf_interface *oi;
92
93 if ( (oi = rn->info) == NULL)
94 continue;
95
96 newcost = ospf_if_get_output_cost (oi);
97
98 /* Is actual output cost changed? */
99 if (oi->output_cost != newcost)
100 {
101 oi->output_cost = newcost;
102 ospf_router_lsa_update_area (oi->area);
103 }
104 }
105}
106
107/* Simulate down/up on the interface. This is needed, for example, when
108 the MTU changes. */
109void
110ospf_if_reset(struct interface *ifp)
111{
112 struct route_node *rn;
113
114 for (rn = route_top (IF_OIFS (ifp)); rn; rn = route_next (rn))
115 {
116 struct ospf_interface *oi;
117
118 if ( (oi = rn->info) == NULL)
119 continue;
120
121 ospf_if_down(oi);
122 ospf_if_up(oi);
123 }
124}
125
126void
127ospf_if_reset_variables (struct ospf_interface *oi)
128{
129 /* Set default values. */
130 /* don't clear this flag. oi->flag = OSPF_IF_DISABLE; */
131
132 if (oi->vl_data)
133 oi->type = OSPF_IFTYPE_VIRTUALLINK;
134 else
135 /* preserve network-type */
136 if (oi->type != OSPF_IFTYPE_NBMA)
137 oi->type = OSPF_IFTYPE_BROADCAST;
138
139 oi->state = ISM_Down;
140
141 oi->crypt_seqnum = 0;
142
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 */
146 oi->v_ls_ack = 1;
147}
148
149/* lookup oi for specified prefix/ifp */
150struct ospf_interface *
151ospf_if_table_lookup (struct interface *ifp, struct prefix *prefix)
152{
153 struct prefix p;
154 struct route_node *rn;
155 struct ospf_interface *rninfo = NULL;
156
157 p = *prefix;
158 p.prefixlen = IPV4_MAX_PREFIXLEN;
159
160 /* route_node_get implicitely locks */
161 if ((rn = route_node_lookup (IF_OIFS (ifp), &p)))
162 {
163 rninfo = (struct ospf_interface *) rn->info;
164 route_unlock_node (rn);
165 }
166
167 return rninfo;
168}
169
170static void
171ospf_add_to_if (struct interface *ifp, struct ospf_interface *oi)
172{
173 struct route_node *rn;
174 struct prefix p;
175
176 p = *oi->address;
177 p.prefixlen = IPV4_MAX_PREFIXLEN;
178
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
182 */
183 assert (!rn->info || rn->info == oi);
184 rn->info = oi;
185}
186
187static void
188ospf_delete_from_if (struct interface *ifp, struct ospf_interface *oi)
189{
190 struct route_node *rn;
191 struct prefix p;
192
193 p = *oi->address;
194 p.prefixlen = IPV4_MAX_PREFIXLEN;
195
196 rn = route_node_lookup (IF_OIFS (oi->ifp), &p);
197 assert (rn);
198 assert (rn->info);
199 rn->info = NULL;
200 route_unlock_node (rn);
201 route_unlock_node (rn);
202}
203
204struct ospf_interface *
205ospf_if_new (struct ospf *ospf, struct interface *ifp, struct prefix *p)
206{
207 struct ospf_interface *oi;
208
209 if ((oi = ospf_if_table_lookup (ifp, p)) == NULL)
210 {
211 oi = XCALLOC (MTYPE_OSPF_IF, sizeof (struct ospf_interface));
212 memset (oi, 0, sizeof (struct ospf_interface));
213 }
214 else
215 return oi;
216
217 /* Set zebra interface pointer. */
218 oi->ifp = ifp;
219 oi->address = p;
220
221 ospf_add_to_if (ifp, oi);
222 listnode_add (ospf->oiflist, oi);
223
224 /* Initialize neighbor list. */
225 oi->nbrs = route_table_init ();
226
227 /* Initialize static neighbor list. */
228 oi->nbr_nbma = list_new ();
229
230 /* Initialize Link State Acknowledgment list. */
231 oi->ls_ack = list_new ();
232 oi->ls_ack_direct.ls_ack = list_new ();
233
234 /* Set default values. */
235 ospf_if_reset_variables (oi);
236
237 /* Add pseudo neighbor. */
238 oi->nbr_self = ospf_nbr_new (oi);
239
240 oi->ls_upd_queue = route_table_init ();
241 oi->t_ls_upd_event = NULL;
242 oi->t_ls_ack_direct = NULL;
243
244 oi->crypt_seqnum = time (NULL);
245
246#ifdef HAVE_OPAQUE_LSA
247 ospf_opaque_type9_lsa_init (oi);
248#endif /* HAVE_OPAQUE_LSA */
249
250 oi->ospf = ospf;
251
252 return oi;
253}
254
255/* Restore an interface to its pre UP state
256 Used from ism_interface_down only */
257void
258ospf_if_cleanup (struct ospf_interface *oi)
259{
260 struct route_node *rn;
261 struct listnode *node, *nnode;
262 struct ospf_neighbor *nbr;
263 struct ospf_nbr_nbma *nbr_nbma;
264 struct ospf_lsa *lsa;
265
266 /* oi->nbrs and oi->nbr_nbma should be deleted on InterfaceDown event */
267 /* delete all static neighbors attached to this interface */
268 for (ALL_LIST_ELEMENTS (oi->nbr_nbma, node, nnode, nbr_nbma))
269 {
270 OSPF_POLL_TIMER_OFF (nbr_nbma->t_poll);
271
272 if (nbr_nbma->nbr)
273 {
274 nbr_nbma->nbr->nbr_nbma = NULL;
275 nbr_nbma->nbr = NULL;
276 }
277
278 nbr_nbma->oi = NULL;
279
280 listnode_delete (oi->nbr_nbma, nbr_nbma);
281 }
282
283 /* send Neighbor event KillNbr to all associated neighbors. */
284 for (rn = route_top (oi->nbrs); rn; rn = route_next (rn))
285 if ((nbr = rn->info) != NULL)
286 if (nbr != oi->nbr_self)
287 OSPF_NSM_EVENT_EXECUTE (nbr, NSM_KillNbr);
288
289 /* Cleanup Link State Acknowlegdment list. */
290 for (ALL_LIST_ELEMENTS (oi->ls_ack, node, nnode, lsa))
291 ospf_lsa_unlock (&lsa); /* oi->ls_ack */
292 list_delete_all_node (oi->ls_ack);
293
294 oi->crypt_seqnum = 0;
295
296 /* Empty link state update queue */
297 ospf_ls_upd_queue_empty (oi);
298
299 /* Reset pseudo neighbor. */
300 ospf_nbr_self_reset (oi);
301}
302
303void
304ospf_if_free (struct ospf_interface *oi)
305{
306 ospf_if_down (oi);
307
308 assert (oi->state == ISM_Down);
309
310#ifdef HAVE_OPAQUE_LSA
311 ospf_opaque_type9_lsa_term (oi);
312#endif /* HAVE_OPAQUE_LSA */
313
314 /* Free Pseudo Neighbour */
315 ospf_nbr_delete (oi->nbr_self);
316
317 route_table_finish (oi->nbrs);
318 route_table_finish (oi->ls_upd_queue);
319
320 /* Free any lists that should be freed */
321 list_free (oi->nbr_nbma);
322
323 list_free (oi->ls_ack);
324 list_free (oi->ls_ack_direct.ls_ack);
325
326 ospf_delete_from_if (oi->ifp, oi);
327
328 listnode_delete (oi->ospf->oiflist, oi);
329 listnode_delete (oi->area->oiflist, oi);
330
331 thread_cancel_event (master, oi);
332
333 memset (oi, 0, sizeof (*oi));
334 XFREE (MTYPE_OSPF_IF, oi);
335}
336
337
338/*
339* check if interface with given address is configured and
340* return it if yes. special treatment for PtP networks.
341*/
342struct ospf_interface *
343ospf_if_is_configured (struct ospf *ospf, struct in_addr *address)
344{
345 struct listnode *node, *nnode;
346 struct ospf_interface *oi;
347 struct prefix_ipv4 addr;
348
349 addr.family = AF_INET;
350 addr.prefix = *address;
351 addr.prefixlen = IPV4_MAX_PREFIXLEN;
352
353 for (ALL_LIST_ELEMENTS (ospf->oiflist, node, nnode, oi))
354 if (oi->type != OSPF_IFTYPE_VIRTUALLINK)
355 {
356 if (CHECK_FLAG(oi->connected->flags, ZEBRA_IFA_UNNUMBERED))
357 {
358 if (htonl(oi->ifp->ifindex) == address->s_addr)
359 return oi;
360 }
361 else if (oi->type == OSPF_IFTYPE_POINTOPOINT)
362 {
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;
367 }
368 else
369 {
370 if (IPV4_ADDR_SAME (address, &oi->address->u.prefix4))
371 return oi;
372 }
373 }
374 return NULL;
375}
376
377int
378ospf_if_is_up (struct ospf_interface *oi)
379{
380 return if_is_up (oi->ifp);
381}
382
383struct ospf_interface *
384ospf_if_exists (struct ospf_interface *oic)
385{
386 struct listnode *node;
387 struct ospf *ospf;
388 struct ospf_interface *oi;
389
390 if ((ospf = ospf_lookup ()) == NULL)
391 return NULL;
392
393 for (ALL_LIST_ELEMENTS_RO (ospf->oiflist, node, oi))
394 if (oi == oic)
395 return oi;
396
397 return NULL;
398}
399
400/* Lookup OSPF interface by router LSA posistion */
401struct ospf_interface *
402ospf_if_lookup_by_lsa_pos (struct ospf_area *area, int lsa_pos)
403{
404 struct listnode *node;
405 struct ospf_interface *oi;
406
407 for (ALL_LIST_ELEMENTS_RO (area->oiflist, node, oi))
408 {
409 if (lsa_pos >= oi->lsa_pos_beg && lsa_pos < oi->lsa_pos_end)
410 return oi;
411 }
412 return NULL;
413}
414
415struct ospf_interface *
416ospf_if_lookup_by_local_addr (struct ospf *ospf,
417 struct interface *ifp, struct in_addr address)
418{
419 struct listnode *node;
420 struct ospf_interface *oi;
421
422 for (ALL_LIST_ELEMENTS_RO (ospf->oiflist, node, oi))
423 if (oi->type != OSPF_IFTYPE_VIRTUALLINK)
424 {
425 if (ifp && oi->ifp != ifp)
426 continue;
427
428 if (IPV4_ADDR_SAME (&address, &oi->address->u.prefix4))
429 return oi;
430 }
431
432 return NULL;
433}
434
435struct ospf_interface *
436ospf_if_lookup_by_prefix (struct ospf *ospf, struct prefix_ipv4 *p)
437{
438 struct listnode *node;
439 struct ospf_interface *oi;
440
441 /* Check each Interface. */
442 for (ALL_LIST_ELEMENTS_RO (ospf->oiflist, node, oi))
443 {
444 if (oi->type != OSPF_IFTYPE_VIRTUALLINK)
445 {
446 struct prefix ptmp;
447
448 prefix_copy (&ptmp, CONNECTED_PREFIX(oi->connected));
449 apply_mask (&ptmp);
450 if (prefix_same (&ptmp, (struct prefix *) p))
451 return oi;
452 }
453 }
454 return NULL;
455}
456
457/* determine receiving interface by ifp and source address */
458struct ospf_interface *
459ospf_if_lookup_recv_if (struct ospf *ospf, struct in_addr src,
460 struct interface *ifp)
461{
462 struct route_node *rn;
463 struct prefix_ipv4 addr;
464 struct ospf_interface *oi, *match;
465
466 addr.family = AF_INET;
467 addr.prefix = src;
468 addr.prefixlen = IPV4_MAX_BITLEN;
469
470 match = NULL;
471
472 for (rn = route_top (IF_OIFS (ifp)); rn; rn = route_next (rn))
473 {
474 oi = rn->info;
475
476 if (!oi) /* oi can be NULL for PtP aliases */
477 continue;
478
479 if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
480 continue;
481
482 if (if_is_loopback (oi->ifp))
483 continue;
484
485 if (CHECK_FLAG(oi->connected->flags, ZEBRA_IFA_UNNUMBERED))
486 match = oi;
487 else if (prefix_match (CONNECTED_PREFIX(oi->connected),
488 (struct prefix *) &addr))
489 {
490 if ( (match == NULL) ||
491 (match->address->prefixlen < oi->address->prefixlen)
492 )
493 match = oi;
494 }
495 }
496
497 return match;
498}
499
500void
501ospf_if_stream_set (struct ospf_interface *oi)
502{
503 /* set output fifo queue. */
504 if (oi->obuf == NULL)
505 oi->obuf = ospf_fifo_new ();
506}
507
508void
509ospf_if_stream_unset (struct ospf_interface *oi)
510{
511 struct ospf *ospf = oi->ospf;
512
513 if (oi->obuf)
514 {
515 ospf_fifo_free (oi->obuf);
516 oi->obuf = NULL;
517
518 if (oi->on_write_q)
519 {
520 listnode_delete (ospf->oi_write_q, oi);
521 if (list_isempty(ospf->oi_write_q))
522 OSPF_TIMER_OFF (ospf->t_write);
523 oi->on_write_q = 0;
524 }
525 }
526}
527
528
529static struct ospf_if_params *
530ospf_new_if_params (void)
531{
532 struct ospf_if_params *oip;
533
534 oip = XCALLOC (MTYPE_OSPF_IF_PARAMS, sizeof (struct ospf_if_params));
535
536 if (!oip)
537 return NULL;
538
539 UNSET_IF_PARAM (oip, output_cost_cmd);
540 UNSET_IF_PARAM (oip, transmit_delay);
541 UNSET_IF_PARAM (oip, retransmit_interval);
542 UNSET_IF_PARAM (oip, passive_interface);
543 UNSET_IF_PARAM (oip, v_hello);
544 UNSET_IF_PARAM (oip, fast_hello);
545 UNSET_IF_PARAM (oip, v_wait);
546 UNSET_IF_PARAM (oip, priority);
547 UNSET_IF_PARAM (oip, type);
548 UNSET_IF_PARAM (oip, auth_simple);
549 UNSET_IF_PARAM (oip, auth_crypt);
550 UNSET_IF_PARAM (oip, auth_type);
551
552 oip->auth_crypt = list_new ();
553
554 oip->network_lsa_seqnum = htonl(OSPF_INITIAL_SEQUENCE_NUMBER);
555
556 return oip;
557}
558
559void
560ospf_del_if_params (struct ospf_if_params *oip)
561{
562 list_delete (oip->auth_crypt);
563 bfd_info_free(&(oip->bfd_info));
564 XFREE (MTYPE_OSPF_IF_PARAMS, oip);
565}
566
567void
568ospf_free_if_params (struct interface *ifp, struct in_addr addr)
569{
570 struct ospf_if_params *oip;
571 struct prefix_ipv4 p;
572 struct route_node *rn;
573
574 p.family = AF_INET;
575 p.prefixlen = IPV4_MAX_PREFIXLEN;
576 p.prefix = addr;
577 rn = route_node_lookup (IF_OIFS_PARAMS (ifp), (struct prefix*)&p);
578 if (!rn || !rn->info)
579 return;
580
581 oip = rn->info;
582 route_unlock_node (rn);
583
584 if (!OSPF_IF_PARAM_CONFIGURED (oip, output_cost_cmd) &&
585 !OSPF_IF_PARAM_CONFIGURED (oip, transmit_delay) &&
586 !OSPF_IF_PARAM_CONFIGURED (oip, retransmit_interval) &&
587 !OSPF_IF_PARAM_CONFIGURED (oip, passive_interface) &&
588 !OSPF_IF_PARAM_CONFIGURED (oip, v_hello) &&
589 !OSPF_IF_PARAM_CONFIGURED (oip, fast_hello) &&
590 !OSPF_IF_PARAM_CONFIGURED (oip, v_wait) &&
591 !OSPF_IF_PARAM_CONFIGURED (oip, priority) &&
592 !OSPF_IF_PARAM_CONFIGURED (oip, type) &&
593 !OSPF_IF_PARAM_CONFIGURED (oip, auth_simple) &&
594 !OSPF_IF_PARAM_CONFIGURED (oip, auth_type) &&
595 listcount (oip->auth_crypt) == 0 &&
596 ntohl (oip->network_lsa_seqnum) != OSPF_INITIAL_SEQUENCE_NUMBER)
597 {
598 ospf_del_if_params (oip);
599 rn->info = NULL;
600 route_unlock_node (rn);
601 }
602}
603
604struct ospf_if_params *
605ospf_lookup_if_params (struct interface *ifp, struct in_addr addr)
606{
607 struct prefix_ipv4 p;
608 struct route_node *rn;
609
610 p.family = AF_INET;
611 p.prefixlen = IPV4_MAX_PREFIXLEN;
612 p.prefix = addr;
613
614 rn = route_node_lookup (IF_OIFS_PARAMS (ifp), (struct prefix*)&p);
615
616 if (rn)
617 {
618 route_unlock_node (rn);
619 return rn->info;
620 }
621
622 return NULL;
623}
624
625struct ospf_if_params *
626ospf_get_if_params (struct interface *ifp, struct in_addr addr)
627{
628 struct prefix_ipv4 p;
629 struct route_node *rn;
630
631 p.family = AF_INET;
632 p.prefixlen = IPV4_MAX_PREFIXLEN;
633 p.prefix = addr;
634
635 rn = route_node_get (IF_OIFS_PARAMS (ifp), (struct prefix*)&p);
636
637 if (rn->info == NULL)
638 rn->info = ospf_new_if_params ();
639 else
640 route_unlock_node (rn);
641
642 return rn->info;
643}
644
645void
646ospf_if_update_params (struct interface *ifp, struct in_addr addr)
647{
648 struct route_node *rn;
649 struct ospf_interface *oi;
650
651 for (rn = route_top (IF_OIFS (ifp)); rn; rn = route_next (rn))
652 {
653 if ((oi = rn->info) == NULL)
654 continue;
655
656 if (IPV4_ADDR_SAME (&oi->address->u.prefix4, &addr))
657 oi->params = ospf_lookup_if_params (ifp, oi->address->u.prefix4);
658 }
659}
660
661int
662ospf_if_new_hook (struct interface *ifp)
663{
664 int rc = 0;
665
666 ifp->info = XCALLOC (MTYPE_OSPF_IF_INFO, sizeof (struct ospf_if_info));
667
668 IF_OIFS (ifp) = route_table_init ();
669 IF_OIFS_PARAMS (ifp) = route_table_init ();
670
671 IF_DEF_PARAMS (ifp) = ospf_new_if_params ();
672
673 SET_IF_PARAM (IF_DEF_PARAMS (ifp), transmit_delay);
674 IF_DEF_PARAMS (ifp)->transmit_delay = OSPF_TRANSMIT_DELAY_DEFAULT;
675
676 SET_IF_PARAM (IF_DEF_PARAMS (ifp), retransmit_interval);
677 IF_DEF_PARAMS (ifp)->retransmit_interval = OSPF_RETRANSMIT_INTERVAL_DEFAULT;
678
679 SET_IF_PARAM (IF_DEF_PARAMS (ifp), priority);
680 IF_DEF_PARAMS (ifp)->priority = OSPF_ROUTER_PRIORITY_DEFAULT;
681
682 IF_DEF_PARAMS (ifp)->mtu_ignore = OSPF_MTU_IGNORE_DEFAULT;
683
684 SET_IF_PARAM (IF_DEF_PARAMS (ifp), v_hello);
685 IF_DEF_PARAMS (ifp)->v_hello = OSPF_HELLO_INTERVAL_DEFAULT;
686
687 SET_IF_PARAM (IF_DEF_PARAMS (ifp), fast_hello);
688 IF_DEF_PARAMS (ifp)->fast_hello = OSPF_FAST_HELLO_DEFAULT;
689
690 SET_IF_PARAM (IF_DEF_PARAMS (ifp), v_wait);
691 IF_DEF_PARAMS (ifp)->v_wait = OSPF_ROUTER_DEAD_INTERVAL_DEFAULT;
692
693 SET_IF_PARAM (IF_DEF_PARAMS (ifp), auth_simple);
694 memset (IF_DEF_PARAMS (ifp)->auth_simple, 0, OSPF_AUTH_SIMPLE_SIZE);
695
696 SET_IF_PARAM (IF_DEF_PARAMS (ifp), auth_type);
697 IF_DEF_PARAMS (ifp)->auth_type = OSPF_AUTH_NOTSET;
698
699#ifdef HAVE_OPAQUE_LSA
700 rc = ospf_opaque_new_if (ifp);
701#endif /* HAVE_OPAQUE_LSA */
702 return rc;
703}
704
705static int
706ospf_if_delete_hook (struct interface *ifp)
707{
708 int rc = 0;
709 struct route_node *rn;
710#ifdef HAVE_OPAQUE_LSA
711 rc = ospf_opaque_del_if (ifp);
712#endif /* HAVE_OPAQUE_LSA */
713
714 route_table_finish (IF_OIFS (ifp));
715
716 for (rn = route_top (IF_OIFS_PARAMS (ifp)); rn; rn = route_next (rn))
717 if (rn->info)
718 ospf_del_if_params (rn->info);
719 route_table_finish (IF_OIFS_PARAMS (ifp));
720
721 ospf_del_if_params ((struct ospf_if_params *) IF_DEF_PARAMS (ifp));
722 XFREE (MTYPE_OSPF_IF_INFO, ifp->info);
723 ifp->info = NULL;
724
725 return rc;
726}
727
728int
729ospf_if_is_enable (struct ospf_interface *oi)
730{
731 if (!if_is_loopback (oi->ifp))
732 if (if_is_up (oi->ifp))
733 return 1;
734
735 return 0;
736}
737
738void
739ospf_if_set_multicast(struct ospf_interface *oi)
740{
741 if ((oi->state > ISM_Loopback) &&
742 (oi->type != OSPF_IFTYPE_LOOPBACK) &&
743 (oi->type != OSPF_IFTYPE_VIRTUALLINK) &&
744 (OSPF_IF_PASSIVE_STATUS(oi) == OSPF_IF_ACTIVE))
745 {
746 /* The interface should belong to the OSPF-all-routers group. */
747 if (!OI_MEMBER_CHECK(oi, MEMBER_ALLROUTERS) &&
748 (ospf_if_add_allspfrouters(oi->ospf, oi->address,
749 oi->ifp->ifindex) >= 0))
750 /* Set the flag only if the system call to join succeeded. */
751 OI_MEMBER_JOINED(oi, MEMBER_ALLROUTERS);
752 }
753 else
754 {
755 /* The interface should NOT belong to the OSPF-all-routers group. */
756 if (OI_MEMBER_CHECK(oi, MEMBER_ALLROUTERS))
757 {
758 /* Only actually drop if this is the last reference */
759 if (OI_MEMBER_COUNT(oi, MEMBER_ALLROUTERS) == 1)
760 ospf_if_drop_allspfrouters (oi->ospf, oi->address,
761 oi->ifp->ifindex);
762 /* Unset the flag regardless of whether the system call to leave
763 the group succeeded, since it's much safer to assume that
764 we are not a member. */
765 OI_MEMBER_LEFT(oi,MEMBER_ALLROUTERS);
766 }
767 }
768
769 if (((oi->type == OSPF_IFTYPE_BROADCAST) ||
770 (oi->type == OSPF_IFTYPE_POINTOPOINT)) &&
771 ((oi->state == ISM_DR) || (oi->state == ISM_Backup)) &&
772 (OSPF_IF_PASSIVE_STATUS(oi) == OSPF_IF_ACTIVE))
773 {
774 /* The interface should belong to the OSPF-designated-routers group. */
775 if (!OI_MEMBER_CHECK(oi, MEMBER_DROUTERS) &&
776 (ospf_if_add_alldrouters(oi->ospf, oi->address,
777 oi->ifp->ifindex) >= 0))
778 /* Set the flag only if the system call to join succeeded. */
779 OI_MEMBER_JOINED(oi, MEMBER_DROUTERS);
780 }
781 else
782 {
783 /* The interface should NOT belong to the OSPF-designated-routers group */
784 if (OI_MEMBER_CHECK(oi, MEMBER_DROUTERS))
785 {
786 /* drop only if last reference */
787 if (OI_MEMBER_COUNT(oi, MEMBER_DROUTERS) == 1)
788 ospf_if_drop_alldrouters(oi->ospf, oi->address, oi->ifp->ifindex);
789
790 /* Unset the flag regardless of whether the system call to leave
791 the group succeeded, since it's much safer to assume that
792 we are not a member. */
793 OI_MEMBER_LEFT(oi, MEMBER_DROUTERS);
794 }
795 }
796}
797
798int
799ospf_if_up (struct ospf_interface *oi)
800{
801 if (oi == NULL)
802 return 0;
803
804 if (oi->type == OSPF_IFTYPE_LOOPBACK)
805 OSPF_ISM_EVENT_SCHEDULE (oi, ISM_LoopInd);
806 else
807 {
808 struct ospf *ospf = ospf_lookup ();
809 if (ospf != NULL)
810 ospf_adjust_sndbuflen (ospf, oi->ifp->mtu);
811 else
812 zlog_warn ("%s: ospf_lookup() returned NULL", __func__);
813 ospf_if_stream_set (oi);
814 OSPF_ISM_EVENT_SCHEDULE (oi, ISM_InterfaceUp);
815 }
816
817 return 1;
818}
819
820int
821ospf_if_down (struct ospf_interface *oi)
822{
823 if (oi == NULL)
824 return 0;
825
826 OSPF_ISM_EVENT_EXECUTE (oi, ISM_InterfaceDown);
827 /* delete position in router LSA */
828 oi->lsa_pos_beg = 0;
829 oi->lsa_pos_end = 0;
830 /* Shutdown packet reception and sending */
831 ospf_if_stream_unset (oi);
832
833 return 1;
834}
835
836
837/* Virtual Link related functions. */
838
839struct ospf_vl_data *
840ospf_vl_data_new (struct ospf_area *area, struct in_addr vl_peer)
841{
842 struct ospf_vl_data *vl_data;
843
844 vl_data = XCALLOC (MTYPE_OSPF_VL_DATA, sizeof (struct ospf_vl_data));
845
846 vl_data->vl_peer.s_addr = vl_peer.s_addr;
847 vl_data->vl_area_id = area->area_id;
848 vl_data->format = area->format;
849
850 return vl_data;
851}
852
853void
854ospf_vl_data_free (struct ospf_vl_data *vl_data)
855{
856 XFREE (MTYPE_OSPF_VL_DATA, vl_data);
857}
858
859u_int vlink_count = 0;
860
861struct ospf_interface *
862ospf_vl_new (struct ospf *ospf, struct ospf_vl_data *vl_data)
863{
864 struct ospf_interface * voi;
865 struct interface * vi;
866 char ifname[INTERFACE_NAMSIZ + 1];
867 struct ospf_area *area;
868 struct in_addr area_id;
869 struct connected *co;
870 struct prefix_ipv4 *p;
871
872 if (IS_DEBUG_OSPF_EVENT)
873 zlog_debug ("ospf_vl_new(): Start");
874 if (vlink_count == OSPF_VL_MAX_COUNT)
875 {
876 if (IS_DEBUG_OSPF_EVENT)
877 zlog_debug ("ospf_vl_new(): Alarm: "
878 "cannot create more than OSPF_MAX_VL_COUNT virtual links");
879 return NULL;
880 }
881
882 if (IS_DEBUG_OSPF_EVENT)
883 zlog_debug ("ospf_vl_new(): creating pseudo zebra interface");
884
885 snprintf (ifname, sizeof(ifname), "VLINK%d", vlink_count);
886 vi = if_create (ifname, strnlen(ifname, sizeof(ifname)));
887 /*
888 * if_create sets ZEBRA_INTERFACE_LINKDETECTION
889 * virtual links don't need this.
890 */
891 UNSET_FLAG (vi->status, ZEBRA_INTERFACE_LINKDETECTION);
892 co = connected_new ();
893 co->ifp = vi;
894 listnode_add (vi->connected, co);
895
896 p = prefix_ipv4_new ();
897 p->family = AF_INET;
898 p->prefix.s_addr = 0;
899 p->prefixlen = 0;
900
901 co->address = (struct prefix *)p;
902
903 voi = ospf_if_new (ospf, vi, co->address);
904 if (voi == NULL)
905 {
906 if (IS_DEBUG_OSPF_EVENT)
907 zlog_debug ("ospf_vl_new(): Alarm: OSPF int structure is not created");
908 return NULL;
909 }
910 voi->connected = co;
911 voi->vl_data = vl_data;
912 voi->ifp->mtu = OSPF_VL_MTU;
913 voi->type = OSPF_IFTYPE_VIRTUALLINK;
914
915 vlink_count++;
916 if (IS_DEBUG_OSPF_EVENT)
917 zlog_debug ("ospf_vl_new(): Created name: %s", ifname);
918 if (IS_DEBUG_OSPF_EVENT)
919 zlog_debug ("ospf_vl_new(): set if->name to %s", vi->name);
920
921 area_id.s_addr = 0;
922 area = ospf_area_get (ospf, area_id, OSPF_AREA_ID_FORMAT_ADDRESS);
923 voi->area = area;
924
925 if (IS_DEBUG_OSPF_EVENT)
926 zlog_debug ("ospf_vl_new(): set associated area to the backbone");
927
928 ospf_nbr_add_self (voi);
929 ospf_area_add_if (voi->area, voi);
930
931 ospf_if_stream_set (voi);
932
933 if (IS_DEBUG_OSPF_EVENT)
934 zlog_debug ("ospf_vl_new(): Stop");
935 return voi;
936}
937
938static void
939ospf_vl_if_delete (struct ospf_vl_data *vl_data)
940{
941 struct interface *ifp = vl_data->vl_oi->ifp;
942 vl_data->vl_oi->address->u.prefix4.s_addr = 0;
943 vl_data->vl_oi->address->prefixlen = 0;
944 ospf_if_free (vl_data->vl_oi);
945 if_delete (ifp);
946 vlink_count--;
947}
948
949/* Look up vl_data for given peer, optionally qualified to be in the
950 * specified area. NULL area returns first found..
951 */
952struct ospf_vl_data *
953ospf_vl_lookup (struct ospf *ospf, struct ospf_area *area,
954 struct in_addr vl_peer)
955{
956 struct ospf_vl_data *vl_data;
957 struct listnode *node;
958
959 if (IS_DEBUG_OSPF_EVENT)
960 {
961 zlog_debug ("%s: Looking for %s", __func__, inet_ntoa (vl_peer));
962 if (area)
963 zlog_debug ("%s: in area %s", __func__, inet_ntoa (area->area_id));
964 }
965
966 for (ALL_LIST_ELEMENTS_RO (ospf->vlinks, node, vl_data))
967 {
968 if (IS_DEBUG_OSPF_EVENT)
969 zlog_debug ("%s: VL %s, peer %s", __func__,
970 vl_data->vl_oi->ifp->name,
971 inet_ntoa (vl_data->vl_peer));
972
973 if (area && !IPV4_ADDR_SAME (&vl_data->vl_area_id, &area->area_id))
974 continue;
975
976 if (IPV4_ADDR_SAME (&vl_data->vl_peer, &vl_peer))
977 return vl_data;
978 }
979
980 return NULL;
981}
982
983static void
984ospf_vl_shutdown (struct ospf_vl_data *vl_data)
985{
986 struct ospf_interface *oi;
987
988 if ((oi = vl_data->vl_oi) == NULL)
989 return;
990
991 oi->address->u.prefix4.s_addr = 0;
992 oi->address->prefixlen = 0;
993
994 UNSET_FLAG (oi->ifp->flags, IFF_UP);
995 /* OSPF_ISM_EVENT_SCHEDULE (oi, ISM_InterfaceDown); */
996 OSPF_ISM_EVENT_EXECUTE (oi, ISM_InterfaceDown);
997}
998
999void
1000ospf_vl_add (struct ospf *ospf, struct ospf_vl_data *vl_data)
1001{
1002 listnode_add (ospf->vlinks, vl_data);
1003#ifdef HAVE_SNMP
1004 ospf_snmp_vl_add (vl_data);
1005#endif /* HAVE_SNMP */
1006}
1007
1008void
1009ospf_vl_delete (struct ospf *ospf, struct ospf_vl_data *vl_data)
1010{
1011 ospf_vl_shutdown (vl_data);
1012 ospf_vl_if_delete (vl_data);
1013
1014#ifdef HAVE_SNMP
1015 ospf_snmp_vl_delete (vl_data);
1016#endif /* HAVE_SNMP */
1017 listnode_delete (ospf->vlinks, vl_data);
1018
1019 ospf_vl_data_free (vl_data);
1020}
1021
1022static int
1023ospf_vl_set_params (struct ospf_vl_data *vl_data, struct vertex *v)
1024{
1025 int changed = 0;
1026 struct ospf_interface *voi;
1027 struct listnode *node;
1028 struct vertex_parent *vp = NULL;
1029 unsigned int i;
1030 struct router_lsa *rl;
1031
1032 voi = vl_data->vl_oi;
1033
1034 if (voi->output_cost != v->distance)
1035 {
1036
1037 voi->output_cost = v->distance;
1038 changed = 1;
1039 }
1040
1041 for (ALL_LIST_ELEMENTS_RO (v->parents, node, vp))
1042 {
1043 vl_data->nexthop.oi = vp->nexthop->oi;
1044 vl_data->nexthop.router = vp->nexthop->router;
1045
1046 if (!IPV4_ADDR_SAME(&voi->address->u.prefix4,
1047 &vl_data->nexthop.oi->address->u.prefix4))
1048 changed = 1;
1049
1050 voi->address->u.prefix4 = vl_data->nexthop.oi->address->u.prefix4;
1051 voi->address->prefixlen = vl_data->nexthop.oi->address->prefixlen;
1052
1053 break; /* We take the first interface. */
1054 }
1055
1056 rl = (struct router_lsa *)v->lsa;
1057
1058 /* use SPF determined backlink index in struct vertex
1059 * for virtual link destination address
1060 */
1061 if (vp && vp->backlink >= 0)
1062 {
1063 if (!IPV4_ADDR_SAME (&vl_data->peer_addr,
1064 &rl->link[vp->backlink].link_data))
1065 changed = 1;
1066 vl_data->peer_addr = rl->link[vp->backlink].link_data;
1067 }
1068 else
1069 {
1070 /* This is highly odd, there is no backlink index
1071 * there should be due to the ospf_spf_has_link() check
1072 * in SPF. Lets warn and try pick a link anyway.
1073 */
1074 zlog_warn ("ospf_vl_set_params: No backlink for %s!",
1075 vl_data->vl_oi->ifp->name);
1076 for (i = 0; i < ntohs (rl->links); i++)
1077 {
1078 switch (rl->link[i].type)
1079 {
1080 case LSA_LINK_TYPE_VIRTUALLINK:
1081 if (IS_DEBUG_OSPF_EVENT)
1082 zlog_debug ("found back link through VL");
1083 case LSA_LINK_TYPE_TRANSIT:
1084 case LSA_LINK_TYPE_POINTOPOINT:
1085 if (!IPV4_ADDR_SAME (&vl_data->peer_addr,
1086 &rl->link[i].link_data))
1087 changed = 1;
1088 vl_data->peer_addr = rl->link[i].link_data;
1089 }
1090 }
1091 }
1092
1093 if (IS_DEBUG_OSPF_EVENT)
1094 zlog_debug ("%s: %s peer address: %s, cost: %d,%schanged", __func__,
1095 vl_data->vl_oi->ifp->name,
1096 inet_ntoa(vl_data->peer_addr),
1097 voi->output_cost,
1098 (changed ? " " : " un"));
1099
1100 return changed;
1101}
1102
1103
1104void
1105ospf_vl_up_check (struct ospf_area *area, struct in_addr rid,
1106 struct vertex *v)
1107{
1108 struct ospf *ospf = area->ospf;
1109 struct listnode *node;
1110 struct ospf_vl_data *vl_data;
1111 struct ospf_interface *oi;
1112
1113 if (IS_DEBUG_OSPF_EVENT)
1114 {
1115 zlog_debug ("ospf_vl_up_check(): Start");
1116 zlog_debug ("ospf_vl_up_check(): Router ID is %s", inet_ntoa (rid));
1117 zlog_debug ("ospf_vl_up_check(): Area is %s", inet_ntoa (area->area_id));
1118 }
1119
1120 for (ALL_LIST_ELEMENTS_RO (ospf->vlinks, node, vl_data))
1121 {
1122 if (IS_DEBUG_OSPF_EVENT)
1123 {
1124 zlog_debug ("%s: considering VL, %s in area %s", __func__,
1125 vl_data->vl_oi->ifp->name,
1126 inet_ntoa (vl_data->vl_area_id));
1127 zlog_debug ("%s: peer ID: %s", __func__,
1128 inet_ntoa (vl_data->vl_peer));
1129 }
1130
1131 if (IPV4_ADDR_SAME (&vl_data->vl_peer, &rid) &&
1132 IPV4_ADDR_SAME (&vl_data->vl_area_id, &area->area_id))
1133 {
1134 oi = vl_data->vl_oi;
1135 SET_FLAG (vl_data->flags, OSPF_VL_FLAG_APPROVED);
1136
1137 if (IS_DEBUG_OSPF_EVENT)
1138 zlog_debug ("ospf_vl_up_check(): this VL matched");
1139
1140 if (oi->state == ISM_Down)
1141 {
1142 if (IS_DEBUG_OSPF_EVENT)
1143 zlog_debug ("ospf_vl_up_check(): VL is down, waking it up");
1144 SET_FLAG (oi->ifp->flags, IFF_UP);
1145 OSPF_ISM_EVENT_EXECUTE(oi,ISM_InterfaceUp);
1146 }
1147
1148 if (ospf_vl_set_params (vl_data, v))
1149 {
1150 if (IS_DEBUG_OSPF (ism, ISM_EVENTS))
1151 zlog_debug ("ospf_vl_up_check: VL cost change,"
1152 " scheduling router lsa refresh");
1153 if (ospf->backbone)
1154 ospf_router_lsa_update_area (ospf->backbone);
1155 else if (IS_DEBUG_OSPF (ism, ISM_EVENTS))
1156 zlog_debug ("ospf_vl_up_check: VL cost change, no backbone!");
1157 }
1158 }
1159 }
1160}
1161
1162void
1163ospf_vl_unapprove (struct ospf *ospf)
1164{
1165 struct listnode *node;
1166 struct ospf_vl_data *vl_data;
1167
1168 for (ALL_LIST_ELEMENTS_RO (ospf->vlinks, node, vl_data))
1169 UNSET_FLAG (vl_data->flags, OSPF_VL_FLAG_APPROVED);
1170}
1171
1172void
1173ospf_vl_shut_unapproved (struct ospf *ospf)
1174{
1175 struct listnode *node, *nnode;
1176 struct ospf_vl_data *vl_data;
1177
1178 for (ALL_LIST_ELEMENTS (ospf->vlinks, node, nnode, vl_data))
1179 if (!CHECK_FLAG (vl_data->flags, OSPF_VL_FLAG_APPROVED))
1180 ospf_vl_shutdown (vl_data);
1181}
1182
1183int
1184ospf_full_virtual_nbrs (struct ospf_area *area)
1185{
1186 if (IS_DEBUG_OSPF_EVENT)
1187 {
1188 zlog_debug ("counting fully adjacent virtual neighbors in area %s",
1189 inet_ntoa (area->area_id));
1190 zlog_debug ("there are %d of them", area->full_vls);
1191 }
1192
1193 return area->full_vls;
1194}
1195
1196int
1197ospf_vls_in_area (struct ospf_area *area)
1198{
1199 struct listnode *node;
1200 struct ospf_vl_data *vl_data;
1201 int c = 0;
1202
1203 for (ALL_LIST_ELEMENTS_RO (area->ospf->vlinks, node, vl_data))
1204 if (IPV4_ADDR_SAME (&vl_data->vl_area_id, &area->area_id))
1205 c++;
1206
1207 return c;
1208}
1209
1210
1211struct crypt_key *
1212ospf_crypt_key_new ()
1213{
1214 return XCALLOC (MTYPE_OSPF_CRYPT_KEY, sizeof (struct crypt_key));
1215}
1216
1217void
1218ospf_crypt_key_add (struct list *crypt, struct crypt_key *ck)
1219{
1220 listnode_add (crypt, ck);
1221}
1222
1223struct crypt_key *
1224ospf_crypt_key_lookup (struct list *auth_crypt, u_char key_id)
1225{
1226 struct listnode *node;
1227 struct crypt_key *ck;
1228
1229 for (ALL_LIST_ELEMENTS_RO (auth_crypt, node, ck))
1230 if (ck->key_id == key_id)
1231 return ck;
1232
1233 return NULL;
1234}
1235
1236int
1237ospf_crypt_key_delete (struct list *auth_crypt, u_char key_id)
1238{
1239 struct listnode *node, *nnode;
1240 struct crypt_key *ck;
1241
1242 for (ALL_LIST_ELEMENTS (auth_crypt, node, nnode, ck))
1243 {
1244 if (ck->key_id == key_id)
1245 {
1246 listnode_delete (auth_crypt, ck);
1247 XFREE (MTYPE_OSPF_CRYPT_KEY, ck);
1248 return 1;
1249 }
1250 }
1251
1252 return 0;
1253}
1254
1255u_char
1256ospf_default_iftype(struct interface *ifp)
1257{
1258 if (if_is_pointopoint (ifp))
1259 return OSPF_IFTYPE_POINTOPOINT;
1260 else if (if_is_loopback (ifp))
1261 return OSPF_IFTYPE_LOOPBACK;
1262 else
1263 return OSPF_IFTYPE_BROADCAST;
1264}
1265
1266void
1267ospf_if_init ()
1268{
1269 /* Initialize Zebra interface data structure. */
1270 om->iflist = vrf_iflist (VRF_DEFAULT);
1271 if_add_hook (IF_NEW_HOOK, ospf_if_new_hook);
1272 if_add_hook (IF_DELETE_HOOK, ospf_if_delete_hook);
1273}