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