]> git.proxmox.com Git - mirror_frr.git/blob - ospfd/ospf_interface.c
2004-12-08 Andrew J. Schorr <ajschorr@alumni.princeton.edu>
[mirror_frr.git] / ospfd / ospf_interface.c
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
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 */
51
52 \f
53 int
54 ospf_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;
61 refbw = oi->ospf->ref_bandwidth;
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
81 void
82 ospf_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
105 void
106 ospf_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
128 /* lookup oi for specified prefix/ifp */
129 struct ospf_interface *
130 ospf_if_table_lookup (struct interface *ifp, struct prefix *prefix)
131 {
132 struct prefix p;
133 struct route_node *rn;
134 struct ospf_interface *rninfo;
135
136 p = *prefix;
137
138 rn = route_node_get (IF_OIFS (ifp), &p);
139 /* route_node_get implicitely locks */
140 rninfo = (struct ospf_interface *) rn->info;
141 route_unlock_node (rn);
142 return rninfo;
143 }
144
145 void
146 ospf_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);
155 /* rn->info should either be NULL or equal to this oi
156 * as route_node_get may return an existing node
157 */
158 assert (!rn->info || rn->info == oi);
159 rn->info = oi;
160 }
161
162 void
163 ospf_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
179 struct ospf_interface *
180 ospf_if_new (struct ospf *ospf, struct interface *ifp, struct prefix *p)
181 {
182 struct ospf_interface *oi;
183
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
192 /* Set zebra interface pointer. */
193 oi->ifp = ifp;
194 oi->address = p;
195
196 ospf_add_to_if (ifp, oi);
197 listnode_add (ospf->oiflist, oi);
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;
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
225 oi->crypt_seqnum = time (NULL);
226
227 #ifdef HAVE_OPAQUE_LSA
228 ospf_opaque_type9_lsa_init (oi);
229 #endif /* HAVE_OPAQUE_LSA */
230
231 oi->ospf = ospf;
232
233 return oi;
234 }
235
236 /* Restore an interface to its pre UP state
237 Used from ism_interface_down only */
238 void
239 ospf_if_cleanup (struct ospf_interface *oi)
240 {
241 struct route_node *rn;
242 struct listnode *node;
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);
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;
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;
299 }
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
306 void
307 ospf_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
331 listnode_delete (oi->ospf->oiflist, oi);
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
341 * return it if yes. special treatment for PtP networks.
342 */
343 struct ospf_interface *
344 ospf_if_is_configured (struct ospf *ospf, struct in_addr *address)
345 {
346 struct listnode *node;
347 struct ospf_interface *oi;
348 struct prefix_ipv4 addr;
349
350 addr.family = AF_INET;
351 addr.prefix = *address;
352 addr.prefixlen = IPV4_MAX_PREFIXLEN;
353
354 for (node = listhead (ospf->oiflist); node; nextnode (node))
355 if ((oi = getdata (node)) != NULL && oi->type != OSPF_IFTYPE_VIRTUALLINK)
356 {
357 if (oi->type == OSPF_IFTYPE_POINTOPOINT)
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 }
374 else
375 {
376 if (IPV4_ADDR_SAME (address, &oi->address->u.prefix4))
377 return oi;
378 }
379 }
380 return NULL;
381 }
382
383 int
384 ospf_if_is_up (struct ospf_interface *oi)
385 {
386 return if_is_up (oi->ifp);
387 }
388
389 struct ospf_interface *
390 ospf_if_exists (struct ospf_interface *oic)
391 {
392 struct listnode *node;
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
406 struct ospf_interface *
407 ospf_if_lookup_by_local_addr (struct ospf *ospf,
408 struct interface *ifp, struct in_addr address)
409 {
410 struct listnode *node;
411 struct ospf_interface *oi;
412
413 for (node = listhead (ospf->oiflist); node; nextnode (node))
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
426 struct ospf_interface *
427 ospf_if_lookup_by_prefix (struct ospf *ospf, struct prefix_ipv4 *p)
428 {
429 struct listnode *node;
430 struct ospf_interface *oi;
431 struct prefix ptmp;
432
433 /* Check each Interface. */
434 for (node = listhead (ospf->oiflist); node; nextnode (node))
435 {
436 if ((oi = getdata (node)) != NULL && oi->type != OSPF_IFTYPE_VIRTUALLINK)
437 {
438 if ((oi->type == OSPF_IFTYPE_POINTOPOINT) &&
439 CONNECTED_DEST_HOST(oi->connected))
440 {
441 prefix_copy (&ptmp, oi->connected->destination);
442 ptmp.prefixlen = IPV4_MAX_BITLEN;
443 }
444 else
445 prefix_copy (&ptmp, oi->address);
446
447 apply_mask (&ptmp);
448 if (prefix_same (&ptmp, (struct prefix *) p))
449 return oi;
450 }
451 }
452 return NULL;
453 }
454
455 /* determine receiving interface by source of packet */
456 struct ospf_interface *
457 ospf_if_lookup_recv_if (struct ospf *ospf, struct in_addr src)
458 {
459 struct listnode *node;
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
469 for (node = listhead (ospf->oiflist); node; nextnode (node))
470 {
471 oi = getdata (node);
472
473 if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
474 continue;
475
476 if ((oi->type == OSPF_IFTYPE_POINTOPOINT) &&
477 CONNECTED_DEST_HOST(oi->connected))
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))
485 {
486 if ( (match == NULL) ||
487 (match->address->prefixlen < oi->address->prefixlen)
488 )
489 match = oi;
490 }
491 }
492 }
493
494 return match;
495 }
496 \f
497 void
498 ospf_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
505 void
506 ospf_if_stream_unset (struct ospf_interface *oi)
507 {
508 struct ospf *ospf = oi->ospf;
509
510 if (oi->obuf)
511 {
512 ospf_fifo_free (oi->obuf);
513 oi->obuf = NULL;
514
515 if (oi->on_write_q)
516 {
517 listnode_delete (ospf->oi_write_q, oi);
518 if (list_isempty(ospf->oi_write_q))
519 OSPF_TIMER_OFF (ospf->t_write);
520 oi->on_write_q = 0;
521 }
522 }
523 }
524
525 \f
526 struct ospf_if_params *
527 ospf_new_if_params ()
528 {
529 struct ospf_if_params *oip;
530
531 oip = XMALLOC (MTYPE_OSPF_IF_PARAMS, sizeof (struct ospf_if_params));
532
533 if (!oip)
534 return NULL;
535
536 memset (oip, 0, sizeof (struct ospf_if_params));
537
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);
549
550 oip->auth_crypt = list_new ();
551
552 return oip;
553 }
554
555 void
556 ospf_del_if_params (struct ospf_if_params *oip)
557 {
558 list_delete (oip->auth_crypt);
559 XFREE (MTYPE_OSPF_IF_PARAMS, oip);
560 }
561
562 void
563 ospf_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;
568
569 p.family = AF_INET;
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
597 struct ospf_if_params *
598 ospf_lookup_if_params (struct interface *ifp, struct in_addr addr)
599 {
600 struct prefix_ipv4 p;
601 struct route_node *rn;
602
603 p.family = AF_INET;
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
618 struct ospf_if_params *
619 ospf_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
638 void
639 ospf_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
654 int
655 ospf_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
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
697 int
698 ospf_if_delete_hook (struct interface *ifp)
699 {
700 int rc = 0;
701 struct route_node *rn;
702 #ifdef HAVE_OPAQUE_LSA
703 rc = ospf_opaque_del_if (ifp);
704 #endif /* HAVE_OPAQUE_LSA */
705
706 route_table_finish (IF_OIFS (ifp));
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);
711 route_table_finish (IF_OIFS_PARAMS (ifp));
712
713 ospf_del_if_params ((struct ospf_if_params *) IF_DEF_PARAMS (ifp));
714 XFREE (MTYPE_OSPF_IF_INFO, ifp->info);
715 ifp->info = NULL;
716
717 return rc;
718 }
719
720 int
721 ospf_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
730 int
731 ospf_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)
741 ospf_if_add_allspfrouters (oi->ospf, oi->address, oi->ifp->ifindex);
742 ospf_if_stream_set (oi);
743 OSPF_ISM_EVENT_SCHEDULE (oi, ISM_InterfaceUp);
744 }
745
746 return 1;
747 }
748
749 int
750 ospf_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)
759 ospf_if_drop_allspfrouters (oi->ospf, oi->address, oi->ifp->ifindex);
760
761
762 return 1;
763 }
764
765 \f
766 /* Virtual Link related functions. */
767
768 struct ospf_vl_data *
769 ospf_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
783 void
784 ospf_vl_data_free (struct ospf_vl_data *vl_data)
785 {
786 XFREE (MTYPE_OSPF_VL_DATA, vl_data);
787 }
788
789 u_int vlink_count = 0;
790
791 struct ospf_interface *
792 ospf_vl_new (struct ospf *ospf, struct ospf_vl_data *vl_data)
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)
803 zlog_debug ("ospf_vl_new(): Start");
804 if (vlink_count == OSPF_VL_MAX_COUNT)
805 {
806 if (IS_DEBUG_OSPF_EVENT)
807 zlog_debug ("ospf_vl_new(): Alarm: "
808 "cannot create more than OSPF_MAX_VL_COUNT virtual links");
809 return NULL;
810 }
811
812 if (IS_DEBUG_OSPF_EVENT)
813 zlog_debug ("ospf_vl_new(): creating pseudo zebra interface");
814
815 snprintf (ifname, INTERFACE_NAMSIZ + 1, "VLINK%d", vlink_count);
816 vi = if_create (ifname, INTERFACE_NAMSIZ);
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
828 voi = ospf_if_new (ospf, vi, co->address);
829 if (voi == NULL)
830 {
831 if (IS_DEBUG_OSPF_EVENT)
832 zlog_debug ("ospf_vl_new(): Alarm: OSPF int structure is not created");
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
840 vlink_count++;
841 if (IS_DEBUG_OSPF_EVENT)
842 zlog_debug ("ospf_vl_new(): Created name: %s", ifname);
843 if (IS_DEBUG_OSPF_EVENT)
844 zlog_debug ("ospf_vl_new(): set if->name to %s", vi->name);
845
846 area_id.s_addr = 0;
847 area = ospf_area_get (ospf, area_id, OSPF_AREA_ID_FORMAT_ADDRESS);
848 voi->area = area;
849
850 if (IS_DEBUG_OSPF_EVENT)
851 zlog_debug ("ospf_vl_new(): set associated area to the backbone");
852
853 ospf_area_add_if (voi->area, voi);
854
855 ospf_if_stream_set (voi);
856
857 if (IS_DEBUG_OSPF_EVENT)
858 zlog_debug ("ospf_vl_new(): Stop");
859 return voi;
860 }
861
862 void
863 ospf_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
873 struct ospf_vl_data *
874 ospf_vl_lookup (struct ospf_area *area, struct in_addr vl_peer)
875 {
876 struct ospf_vl_data *vl_data;
877 struct listnode *node;
878
879 for (node = listhead (area->ospf->vlinks); node; nextnode (node))
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
888 void
889 ospf_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
904 void
905 ospf_vl_add (struct ospf *ospf, struct ospf_vl_data *vl_data)
906 {
907 listnode_add (ospf->vlinks, vl_data);
908 #ifdef HAVE_SNMP
909 ospf_snmp_vl_add (vl_data);
910 #endif /* HAVE_SNMP */
911 }
912
913 void
914 ospf_vl_delete (struct ospf *ospf, struct ospf_vl_data *vl_data)
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 */
922 listnode_delete (ospf->vlinks, vl_data);
923
924 ospf_vl_data_free (vl_data);
925 }
926
927 int
928 ospf_vl_set_params (struct ospf_vl_data *vl_data, struct vertex *v)
929 {
930 int changed = 0;
931 struct ospf_interface *voi;
932 struct listnode *node;
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 {
941
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 {
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. */
959 }
960
961 rl = (struct router_lsa *)v->lsa;
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
974 {
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++)
982 {
983 switch (rl->link[i].type)
984 {
985 case LSA_LINK_TYPE_VIRTUALLINK:
986 if (IS_DEBUG_OSPF_EVENT)
987 zlog_debug ("found back link through VL");
988 case LSA_LINK_TYPE_TRANSIT:
989 case LSA_LINK_TYPE_POINTOPOINT:
990 if (!IPV4_ADDR_SAME (&vl_data->peer_addr,
991 &rl->link[i].link_data))
992 changed = 1;
993 vl_data->peer_addr = rl->link[i].link_data;
994 if (IS_DEBUG_OSPF_EVENT)
995 zlog_debug ("ospf_vl_set_params: %s peer address is %s\n",
996 vl_data->vl_oi->ifp->name,
997 inet_ntoa(vl_data->peer_addr));
998 return changed;
999 }
1000 }
1001 }
1002
1003 if (IS_DEBUG_OSPF_EVENT)
1004 zlog_debug ("ospf_vl_set_params: %s peer address is %s\n",
1005 vl_data->vl_oi->ifp->name,
1006 inet_ntoa(vl_data->peer_addr));
1007
1008 return changed;
1009 }
1010
1011
1012 void
1013 ospf_vl_up_check (struct ospf_area *area, struct in_addr rid,
1014 struct vertex *v)
1015 {
1016 struct ospf *ospf = area->ospf;
1017 struct listnode *node;
1018 struct ospf_vl_data *vl_data;
1019 struct ospf_interface *oi;
1020
1021 if (IS_DEBUG_OSPF_EVENT)
1022 {
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));
1026 }
1027
1028 for (node = listhead (ospf->vlinks); node; nextnode (node))
1029 {
1030 if ((vl_data = getdata (node)) == NULL)
1031 continue;
1032
1033 if (IS_DEBUG_OSPF_EVENT)
1034 {
1035 zlog_debug ("ospf_vl_up_check(): considering VL, name: %s",
1036 vl_data->vl_oi->ifp->name);
1037 zlog_debug ("ospf_vl_up_check(): VL area: %s, peer ID: %s",
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)
1049 zlog_debug ("ospf_vl_up_check(): this VL matched");
1050
1051 if (oi->state == ISM_Down)
1052 {
1053 if (IS_DEBUG_OSPF_EVENT)
1054 zlog_debug ("ospf_vl_up_check(): VL is down, waking it up");
1055 SET_FLAG (oi->ifp->flags, IFF_UP);
1056 OSPF_ISM_EVENT_EXECUTE(oi,ISM_InterfaceUp);
1057 }
1058
1059 if (ospf_vl_set_params (vl_data, v))
1060 {
1061 if (IS_DEBUG_OSPF (ism, ISM_EVENTS))
1062 zlog_debug ("ospf_vl_up_check: VL cost change,"
1063 " scheduling router lsa refresh");
1064 if(ospf->backbone)
1065 ospf_router_lsa_timer_add (ospf->backbone);
1066 else if (IS_DEBUG_OSPF (ism, ISM_EVENTS))
1067 zlog_debug ("ospf_vl_up_check: VL cost change, no backbone!");
1068 }
1069 }
1070 }
1071 }
1072
1073 void
1074 ospf_vl_unapprove (struct ospf *ospf)
1075 {
1076 struct listnode *node;
1077 struct ospf_vl_data *vl_data;
1078
1079 for (node = listhead (ospf->vlinks); node; nextnode (node))
1080 if ((vl_data = getdata (node)) != NULL)
1081 UNSET_FLAG (vl_data->flags, OSPF_VL_FLAG_APPROVED);
1082 }
1083
1084 void
1085 ospf_vl_shut_unapproved (struct ospf *ospf)
1086 {
1087 struct listnode *node;
1088 struct ospf_vl_data *vl_data;
1089
1090 for (node = listhead (ospf->vlinks); node; nextnode (node))
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
1096 int
1097 ospf_full_virtual_nbrs (struct ospf_area *area)
1098 {
1099 if (IS_DEBUG_OSPF_EVENT)
1100 {
1101 zlog_debug ("counting fully adjacent virtual neighbors in area %s",
1102 inet_ntoa (area->area_id));
1103 zlog_debug ("there are %d of them", area->full_vls);
1104 }
1105
1106 return area->full_vls;
1107 }
1108
1109 int
1110 ospf_vls_in_area (struct ospf_area *area)
1111 {
1112 struct listnode *node;
1113 struct ospf_vl_data *vl_data;
1114 int c = 0;
1115
1116 for (node = listhead (area->ospf->vlinks); node; nextnode (node))
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
1125 struct crypt_key *
1126 ospf_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
1136 void
1137 ospf_crypt_key_add (struct list *crypt, struct crypt_key *ck)
1138 {
1139 listnode_add (crypt, ck);
1140 }
1141
1142 struct crypt_key *
1143 ospf_crypt_key_lookup (struct list *auth_crypt, u_char key_id)
1144 {
1145 struct listnode *node;
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
1158 int
1159 ospf_crypt_key_delete (struct list *auth_crypt, u_char key_id)
1160 {
1161 struct listnode *node;
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
1177 void
1178 ospf_if_init ()
1179 {
1180 /* Initialize Zebra interface data structure. */
1181 if_init ();
1182 om->iflist = iflist;
1183 if_add_hook (IF_NEW_HOOK, ospf_if_new_hook);
1184 if_add_hook (IF_DELETE_HOOK, ospf_if_delete_hook);
1185 }