blob: b48729142f31671cfe74ac876037073d09237650 [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/*
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
paul68980082003-03-25 05:07:42 +000035#include "ospfd/ospfd.h"
paul718e3742002-12-13 20:15:29 +000036#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"
paul718e3742002-12-13 20:15:29 +000046#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
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;
paul68980082003-03-25 05:07:42 +000061 refbw = oi->ospf->ref_bandwidth;
paul718e3742002-12-13 20:15:29 +000062
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
paul20916fb2003-10-15 21:14:20 +0000128/* lookup oi for specified prefix/ifp */
129struct ospf_interface *
130ospf_if_table_lookup (struct interface *ifp, struct prefix *prefix)
131{
132 struct prefix p;
paulaffe1d92003-10-15 21:40:57 +0000133 struct route_node *rn;
paulb5f2c122003-11-10 23:56:29 +0000134 struct ospf_interface *rninfo;
paul20916fb2003-10-15 21:14:20 +0000135
136 p = *prefix;
137
paulaffe1d92003-10-15 21:40:57 +0000138 rn = route_node_get (IF_OIFS (ifp), &p);
paul20916fb2003-10-15 21:14:20 +0000139 /* route_node_get implicitely locks */
paulb5f2c122003-11-10 23:56:29 +0000140 rninfo = (struct ospf_interface *) rn->info;
paulaffe1d92003-10-15 21:40:57 +0000141 route_unlock_node (rn);
paulb5f2c122003-11-10 23:56:29 +0000142 return rninfo;
paul20916fb2003-10-15 21:14:20 +0000143}
144
paul718e3742002-12-13 20:15:29 +0000145void
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);
paul8c80cb72003-02-18 23:25:44 +0000155 /* rn->info should either be NULL or equal to this oi
156 * as route_node_get may return an existing node
157 */
paul20916fb2003-10-15 21:14:20 +0000158 assert (!rn->info || rn->info == oi);
paul718e3742002-12-13 20:15:29 +0000159 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 *
paul68980082003-03-25 05:07:42 +0000180ospf_if_new (struct ospf *ospf, struct interface *ifp, struct prefix *p)
paul718e3742002-12-13 20:15:29 +0000181{
182 struct ospf_interface *oi;
183
paul20916fb2003-10-15 21:14:20 +0000184 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
paul718e3742002-12-13 20:15:29 +0000192 /* Set zebra interface pointer. */
193 oi->ifp = ifp;
194 oi->address = p;
195
196 ospf_add_to_if (ifp, oi);
paul68980082003-03-25 05:07:42 +0000197 listnode_add (ospf->oiflist, oi);
paul718e3742002-12-13 20:15:29 +0000198
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;
paul718e3742002-12-13 20:15:29 +0000218 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
paul68980082003-03-25 05:07:42 +0000225 oi->crypt_seqnum = time (NULL);
226
paul718e3742002-12-13 20:15:29 +0000227#ifdef HAVE_OPAQUE_LSA
228 ospf_opaque_type9_lsa_init (oi);
229#endif /* HAVE_OPAQUE_LSA */
230
paul68980082003-03-25 05:07:42 +0000231 oi->ospf = ospf;
paul718e3742002-12-13 20:15:29 +0000232
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;
242 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);
paulf2c80652002-12-13 21:44:27 +0000286
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#ifdef HAVE_NSSA
296 case OSPF_AREA_NSSA:
297 UNSET_FLAG (oi->nbr_self->options, OSPF_OPTION_E);
298 SET_FLAG (oi->nbr_self->options, OSPF_OPTION_NP);
299 break;
300#endif /* HAVE_NSSA */
301 }
paul718e3742002-12-13 20:15:29 +0000302
303 ospf_lsa_unlock (oi->network_lsa_self);
304 oi->network_lsa_self = NULL;
305 OSPF_TIMER_OFF (oi->t_network_lsa_self);
306}
307
308void
309ospf_if_free (struct ospf_interface *oi)
310{
311 ospf_if_down (oi);
312
313 assert (oi->state == ISM_Down);
314
315#ifdef HAVE_OPAQUE_LSA
316 ospf_opaque_type9_lsa_term (oi);
317#endif /* HAVE_OPAQUE_LSA */
318
319 /* Free Pseudo Neighbour */
320 ospf_nbr_delete (oi->nbr_self);
321
322 route_table_finish (oi->nbrs);
323 route_table_finish (oi->ls_upd_queue);
324
325 /* Free any lists that should be freed */
326 list_free (oi->nbr_nbma);
327
328 list_free (oi->ls_ack);
329 list_free (oi->ls_ack_direct.ls_ack);
330
331 ospf_delete_from_if (oi->ifp, oi);
332
paul68980082003-03-25 05:07:42 +0000333 listnode_delete (oi->ospf->oiflist, oi);
paul718e3742002-12-13 20:15:29 +0000334 listnode_delete (oi->area->oiflist, oi);
335
336 memset (oi, 0, sizeof (*oi));
337 XFREE (MTYPE_OSPF_IF, oi);
338}
339
340
341/*
342* check if interface with given address is configured and
343* return it if yes.
344*/
345struct ospf_interface *
paul68980082003-03-25 05:07:42 +0000346ospf_if_is_configured (struct ospf *ospf, struct in_addr *address)
paul718e3742002-12-13 20:15:29 +0000347{
348 listnode node;
349 struct ospf_interface *oi;
350 struct prefix *addr;
351
paul68980082003-03-25 05:07:42 +0000352 for (node = listhead (ospf->oiflist); node; nextnode (node))
paul718e3742002-12-13 20:15:29 +0000353 if ((oi = getdata (node)) != NULL && oi->type != OSPF_IFTYPE_VIRTUALLINK)
354 {
355 if (oi->type == OSPF_IFTYPE_POINTOPOINT)
356 addr = oi->connected->destination;
357 else
358 addr = oi->address;
359
360 if (IPV4_ADDR_SAME (address, &addr->u.prefix4))
361 return oi;
362 }
363
364 return NULL;
365}
366
367int
368ospf_if_is_up (struct ospf_interface *oi)
369{
370 return if_is_up (oi->ifp);
371}
372
373struct ospf_interface *
hasso2db3d052004-02-11 21:52:13 +0000374ospf_if_exists (struct ospf_interface *oic)
375{
376 listnode node;
377 struct ospf *ospf;
378 struct ospf_interface *oi;
379
380 ospf = ospf_lookup ();
381
382 for (node = listhead (ospf->oiflist); node; nextnode (node))
383 {
384 if (((oi = getdata (node)) != NULL) && (oi == oic))
385 return oi;
386 }
387 return NULL;
388}
389
390struct ospf_interface *
paul68980082003-03-25 05:07:42 +0000391ospf_if_lookup_by_local_addr (struct ospf *ospf,
392 struct interface *ifp, struct in_addr address)
paul718e3742002-12-13 20:15:29 +0000393{
394 listnode node;
395 struct ospf_interface *oi;
396
paul68980082003-03-25 05:07:42 +0000397 for (node = listhead (ospf->oiflist); node; nextnode (node))
paul718e3742002-12-13 20:15:29 +0000398 if ((oi = getdata (node)) != NULL && oi->type != OSPF_IFTYPE_VIRTUALLINK)
399 {
400 if (ifp && oi->ifp != ifp)
401 continue;
402
403 if (IPV4_ADDR_SAME (&address, &oi->address->u.prefix4))
404 return oi;
405 }
406
407 return NULL;
408}
409
410struct ospf_interface *
paul68980082003-03-25 05:07:42 +0000411ospf_if_lookup_by_prefix (struct ospf *ospf, struct prefix_ipv4 *p)
paul718e3742002-12-13 20:15:29 +0000412{
413 listnode node;
414 struct ospf_interface *oi;
415 struct prefix ptmp;
416
417 /* Check each Interface. */
paul68980082003-03-25 05:07:42 +0000418 for (node = listhead (ospf->oiflist); node; nextnode (node))
419 {
420 if ((oi = getdata (node)) != NULL && oi->type != OSPF_IFTYPE_VIRTUALLINK)
421 {
422 if (oi->type == OSPF_IFTYPE_POINTOPOINT)
423 {
424 prefix_copy (&ptmp, oi->connected->destination);
425 ptmp.prefixlen = IPV4_MAX_BITLEN;
426 }
427 else
428 prefix_copy (&ptmp, oi->address);
paul718e3742002-12-13 20:15:29 +0000429
paul68980082003-03-25 05:07:42 +0000430 apply_mask (&ptmp);
431 if (prefix_same (&ptmp, (struct prefix *) p))
432 return oi;
433 }
434 }
paul718e3742002-12-13 20:15:29 +0000435 return NULL;
436}
437
438/* determine receiving interface by source of packet */
439struct ospf_interface *
paul68980082003-03-25 05:07:42 +0000440ospf_if_lookup_recv_if (struct ospf *ospf, struct in_addr src)
paul718e3742002-12-13 20:15:29 +0000441{
442 listnode node;
443 struct prefix_ipv4 addr;
444 struct ospf_interface *oi, *match;
445
446 addr.family = AF_INET;
447 addr.prefix = src;
448 addr.prefixlen = IPV4_MAX_BITLEN;
449
450 match = NULL;
451
paul68980082003-03-25 05:07:42 +0000452 for (node = listhead (ospf->oiflist); node; nextnode (node))
paul718e3742002-12-13 20:15:29 +0000453 {
454 oi = getdata (node);
455
456 if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
457 continue;
458
459 if (oi->type == OSPF_IFTYPE_POINTOPOINT)
460 {
461 if (IPV4_ADDR_SAME (&oi->connected->destination->u.prefix4, &src))
462 return oi;
463 }
464 else
465 {
466 if (prefix_match (oi->address, (struct prefix *) &addr))
paul0a825c72003-05-24 13:48:16 +0000467 {
paulaf8d0332003-05-24 15:31:45 +0000468 if ( (match == NULL) ||
469 (match->address->prefixlen < oi->address->prefixlen)
470 )
paul0a825c72003-05-24 13:48:16 +0000471 match = oi;
472 }
paul718e3742002-12-13 20:15:29 +0000473 }
474 }
475
476 return match;
477}
478
479void
480ospf_if_stream_set (struct ospf_interface *oi)
481{
482 /* set output fifo queue. */
483 if (oi->obuf == NULL)
484 oi->obuf = ospf_fifo_new ();
485}
486
487void
488ospf_if_stream_unset (struct ospf_interface *oi)
489{
paul68980082003-03-25 05:07:42 +0000490 struct ospf *ospf = oi->ospf;
491
paul718e3742002-12-13 20:15:29 +0000492 if (oi->obuf)
493 {
494 ospf_fifo_free (oi->obuf);
495 oi->obuf = NULL;
496
497 if (oi->on_write_q)
498 {
paul68980082003-03-25 05:07:42 +0000499 listnode_delete (ospf->oi_write_q, oi);
500 if (list_isempty(ospf->oi_write_q))
501 OSPF_TIMER_OFF (ospf->t_write);
paul718e3742002-12-13 20:15:29 +0000502 oi->on_write_q = 0;
503 }
504 }
505}
paul68980082003-03-25 05:07:42 +0000506
paul718e3742002-12-13 20:15:29 +0000507
508struct ospf_if_params *
509ospf_new_if_params ()
510{
511 struct ospf_if_params *oip;
512
513 oip = XMALLOC (MTYPE_OSPF_IF_PARAMS, sizeof (struct ospf_if_params));
paul718e3742002-12-13 20:15:29 +0000514
515 if (!oip)
516 return NULL;
517
paulf6457892003-04-17 16:11:30 +0000518 memset (oip, 0, sizeof (struct ospf_if_params));
519
paul718e3742002-12-13 20:15:29 +0000520 UNSET_IF_PARAM (oip, output_cost_cmd);
521 UNSET_IF_PARAM (oip, transmit_delay);
522 UNSET_IF_PARAM (oip, retransmit_interval);
523 UNSET_IF_PARAM (oip, passive_interface);
524 UNSET_IF_PARAM (oip, v_hello);
525 UNSET_IF_PARAM (oip, v_wait);
526 UNSET_IF_PARAM (oip, priority);
527 UNSET_IF_PARAM (oip, type);
528 UNSET_IF_PARAM (oip, auth_simple);
529 UNSET_IF_PARAM (oip, auth_crypt);
530 UNSET_IF_PARAM (oip, auth_type);
paulec1ca632003-06-04 02:23:15 +0000531
532 oip->auth_crypt = list_new ();
paul718e3742002-12-13 20:15:29 +0000533
paul718e3742002-12-13 20:15:29 +0000534 return oip;
535}
536
537void
538ospf_del_if_params (struct ospf_if_params *oip)
539{
540 list_delete (oip->auth_crypt);
541 XFREE (MTYPE_OSPF_IF_PARAMS, oip);
542}
543
544void
545ospf_free_if_params (struct interface *ifp, struct in_addr addr)
546{
547 struct ospf_if_params *oip;
548 struct prefix_ipv4 p;
549 struct route_node *rn;
550 p.prefixlen = IPV4_MAX_PREFIXLEN;
551 p.prefix = addr;
552 rn = route_node_lookup (IF_OIFS_PARAMS (ifp), (struct prefix*)&p);
553 if (!rn || !rn->info)
554 return;
555
556 oip = rn->info;
557 route_unlock_node (rn);
558
559 if (!OSPF_IF_PARAM_CONFIGURED (oip, output_cost_cmd) &&
560 !OSPF_IF_PARAM_CONFIGURED (oip, transmit_delay) &&
561 !OSPF_IF_PARAM_CONFIGURED (oip, retransmit_interval) &&
562 !OSPF_IF_PARAM_CONFIGURED (oip, passive_interface) &&
563 !OSPF_IF_PARAM_CONFIGURED (oip, v_hello) &&
564 !OSPF_IF_PARAM_CONFIGURED (oip, v_wait) &&
565 !OSPF_IF_PARAM_CONFIGURED (oip, priority) &&
566 !OSPF_IF_PARAM_CONFIGURED (oip, type) &&
567 !OSPF_IF_PARAM_CONFIGURED (oip, auth_simple) &&
568 !OSPF_IF_PARAM_CONFIGURED (oip, auth_type) &&
569 listcount (oip->auth_crypt) == 0)
570 {
571 ospf_del_if_params (oip);
572 rn->info = NULL;
573 route_unlock_node (rn);
574 }
575}
576
577struct ospf_if_params *
578ospf_lookup_if_params (struct interface *ifp, struct in_addr addr)
579{
580 struct prefix_ipv4 p;
581 struct route_node *rn;
582
583 p.prefixlen = IPV4_MAX_PREFIXLEN;
584 p.prefix = addr;
585
586 rn = route_node_lookup (IF_OIFS_PARAMS (ifp), (struct prefix*)&p);
587
588 if (rn)
589 {
590 route_unlock_node (rn);
591 return rn->info;
592 }
593
594 return NULL;
595}
596
597struct ospf_if_params *
598ospf_get_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_get (IF_OIFS_PARAMS (ifp), (struct prefix*)&p);
608
609 if (rn->info == NULL)
610 rn->info = ospf_new_if_params ();
611 else
612 route_unlock_node (rn);
613
614 return rn->info;
615}
616
617void
618ospf_if_update_params (struct interface *ifp, struct in_addr addr)
619{
620 struct route_node *rn;
621 struct ospf_interface *oi;
622
623 for (rn = route_top (IF_OIFS (ifp)); rn; rn = route_next (rn))
624 {
625 if ((oi = rn->info) == NULL)
626 continue;
627
628 if (IPV4_ADDR_SAME (&oi->address->u.prefix4, &addr))
629 oi->params = ospf_lookup_if_params (ifp, oi->address->u.prefix4);
630 }
631}
632
633int
634ospf_if_new_hook (struct interface *ifp)
635{
636 int rc = 0;
637
638 ifp->info = XMALLOC (MTYPE_OSPF_IF_INFO, sizeof (struct ospf_if_info));
639 memset (ifp->info, 0, sizeof (struct ospf_if_info));
640
641 IF_OIFS (ifp) = route_table_init ();
642 IF_OIFS_PARAMS (ifp) = route_table_init ();
643
644 IF_DEF_PARAMS (ifp) = ospf_new_if_params ();
645
646 SET_IF_PARAM (IF_DEF_PARAMS (ifp), transmit_delay);
647 IF_DEF_PARAMS (ifp)->transmit_delay = OSPF_TRANSMIT_DELAY_DEFAULT;
648
649 SET_IF_PARAM (IF_DEF_PARAMS (ifp), retransmit_interval);
650 IF_DEF_PARAMS (ifp)->retransmit_interval = OSPF_RETRANSMIT_INTERVAL_DEFAULT;
651
652 SET_IF_PARAM (IF_DEF_PARAMS (ifp), priority);
653 IF_DEF_PARAMS (ifp)->priority = OSPF_ROUTER_PRIORITY_DEFAULT;
654
655 SET_IF_PARAM (IF_DEF_PARAMS (ifp), passive_interface);
656 IF_DEF_PARAMS (ifp)->passive_interface = OSPF_IF_ACTIVE;
657
658 SET_IF_PARAM (IF_DEF_PARAMS (ifp), v_hello);
659 IF_DEF_PARAMS (ifp)->v_hello = OSPF_HELLO_INTERVAL_DEFAULT;
660
661 SET_IF_PARAM (IF_DEF_PARAMS (ifp), v_wait);
662 IF_DEF_PARAMS (ifp)->v_wait = OSPF_ROUTER_DEAD_INTERVAL_DEFAULT;
663
664 SET_IF_PARAM (IF_DEF_PARAMS (ifp), auth_simple);
665 memset (IF_DEF_PARAMS (ifp)->auth_simple, 0, OSPF_AUTH_SIMPLE_SIZE);
666
paul718e3742002-12-13 20:15:29 +0000667 SET_IF_PARAM (IF_DEF_PARAMS (ifp), auth_type);
668 IF_DEF_PARAMS (ifp)->auth_type = OSPF_AUTH_NOTSET;
669
670#ifdef HAVE_OPAQUE_LSA
671 rc = ospf_opaque_new_if (ifp);
672#endif /* HAVE_OPAQUE_LSA */
673 return rc;
674}
675
676int
677ospf_if_delete_hook (struct interface *ifp)
678{
679 int rc = 0;
paul940b01a2004-02-17 20:07:30 +0000680 struct route_node *rn;
paul718e3742002-12-13 20:15:29 +0000681#ifdef HAVE_OPAQUE_LSA
682 rc = ospf_opaque_del_if (ifp);
683#endif /* HAVE_OPAQUE_LSA */
paul940b01a2004-02-17 20:07:30 +0000684
paul718e3742002-12-13 20:15:29 +0000685 route_table_finish (IF_OIFS (ifp));
paul940b01a2004-02-17 20:07:30 +0000686
687 for (rn = route_top (IF_OIFS_PARAMS (ifp)); rn; rn = route_next (rn))
688 if (rn->info)
689 ospf_del_if_params (rn->info);
paul718e3742002-12-13 20:15:29 +0000690 route_table_finish (IF_OIFS_PARAMS (ifp));
paul940b01a2004-02-17 20:07:30 +0000691
paulcfc959b2003-06-04 02:28:45 +0000692 ospf_del_if_params ((struct ospf_if_params *) IF_DEF_PARAMS (ifp));
paul718e3742002-12-13 20:15:29 +0000693 XFREE (MTYPE_OSPF_IF_INFO, ifp->info);
694 ifp->info = NULL;
695
696 return rc;
697}
698
699int
700ospf_if_is_enable (struct ospf_interface *oi)
701{
702 if (!if_is_loopback (oi->ifp))
703 if (if_is_up (oi->ifp))
704 return 1;
705
706 return 0;
707}
708
709int
710ospf_if_up (struct ospf_interface *oi)
711{
712 if (oi == NULL)
713 return 0;
714
715 if (oi->type == OSPF_IFTYPE_LOOPBACK)
716 OSPF_ISM_EVENT_SCHEDULE (oi, ISM_LoopInd);
717 else
718 {
719 if (oi->type != OSPF_IFTYPE_VIRTUALLINK)
paul68980082003-03-25 05:07:42 +0000720 ospf_if_add_allspfrouters (oi->ospf, oi->address, oi->ifp->ifindex);
paul718e3742002-12-13 20:15:29 +0000721 ospf_if_stream_set (oi);
722 OSPF_ISM_EVENT_SCHEDULE (oi, ISM_InterfaceUp);
723 }
724
725 return 1;
726}
727
728int
729ospf_if_down (struct ospf_interface *oi)
730{
731 if (oi == NULL)
732 return 0;
733
734 OSPF_ISM_EVENT_EXECUTE (oi, ISM_InterfaceDown);
735 /* Shutdown packet reception and sending */
736 ospf_if_stream_unset (oi);
737 if (oi->type != OSPF_IFTYPE_VIRTUALLINK)
paul68980082003-03-25 05:07:42 +0000738 ospf_if_drop_allspfrouters (oi->ospf, oi->address, oi->ifp->ifindex);
paul718e3742002-12-13 20:15:29 +0000739
740
741 return 1;
742}
743
744
745/* Virtual Link related functions. */
746
747struct ospf_vl_data *
748ospf_vl_data_new (struct ospf_area *area, struct in_addr vl_peer)
749{
750 struct ospf_vl_data *vl_data;
751
752 vl_data = XMALLOC (MTYPE_OSPF_VL_DATA, sizeof (struct ospf_vl_data));
753 memset (vl_data, 0, sizeof (struct ospf_vl_data));
754
755 vl_data->vl_peer.s_addr = vl_peer.s_addr;
756 vl_data->vl_area_id = area->area_id;
757 vl_data->format = area->format;
758
759 return vl_data;
760}
761
762void
763ospf_vl_data_free (struct ospf_vl_data *vl_data)
764{
765 XFREE (MTYPE_OSPF_VL_DATA, vl_data);
766}
767
768u_int vlink_count = 0;
769
770struct ospf_interface *
paul68980082003-03-25 05:07:42 +0000771ospf_vl_new (struct ospf *ospf, struct ospf_vl_data *vl_data)
paul718e3742002-12-13 20:15:29 +0000772{
773 struct ospf_interface * voi;
774 struct interface * vi;
775 char ifname[INTERFACE_NAMSIZ + 1];
776 struct ospf_area *area;
777 struct in_addr area_id;
778 struct connected *co;
779 struct prefix_ipv4 *p;
780
781 if (IS_DEBUG_OSPF_EVENT)
782 zlog_info ("ospf_vl_new(): Start");
783 if (vlink_count == OSPF_VL_MAX_COUNT)
784 {
785 if (IS_DEBUG_OSPF_EVENT)
786 zlog_info ("ospf_vl_new(): Alarm: "
787 "cannot create more than OSPF_MAX_VL_COUNT virtual links");
788 return NULL;
789 }
790
791 if (IS_DEBUG_OSPF_EVENT)
792 zlog_info ("ospf_vl_new(): creating pseudo zebra interface");
793
paul106d2fd2003-08-01 00:24:13 +0000794 snprintf (ifname, INTERFACE_NAMSIZ + 1, "VLINK%d", vlink_count);
795 vi = if_create (ifname, INTERFACE_NAMSIZ);
paul718e3742002-12-13 20:15:29 +0000796 co = connected_new ();
797 co->ifp = vi;
798 listnode_add (vi->connected, co);
799
800 p = prefix_ipv4_new ();
801 p->family = AF_INET;
802 p->prefix.s_addr = 0;
803 p->prefixlen = 0;
804
805 co->address = (struct prefix *)p;
806
paul68980082003-03-25 05:07:42 +0000807 voi = ospf_if_new (ospf, vi, co->address);
paul718e3742002-12-13 20:15:29 +0000808 if (voi == NULL)
809 {
810 if (IS_DEBUG_OSPF_EVENT)
811 zlog_info ("ospf_vl_new(): Alarm: OSPF int structure is not created");
812 return NULL;
813 }
814 voi->connected = co;
815 voi->vl_data = vl_data;
816 voi->ifp->mtu = OSPF_VL_MTU;
817 voi->type = OSPF_IFTYPE_VIRTUALLINK;
818
paul106d2fd2003-08-01 00:24:13 +0000819 vlink_count++;
paul718e3742002-12-13 20:15:29 +0000820 if (IS_DEBUG_OSPF_EVENT)
821 zlog_info ("ospf_vl_new(): Created name: %s", ifname);
paul718e3742002-12-13 20:15:29 +0000822 if (IS_DEBUG_OSPF_EVENT)
823 zlog_info ("ospf_vl_new(): set if->name to %s", vi->name);
824
825 area_id.s_addr = 0;
paul68980082003-03-25 05:07:42 +0000826 area = ospf_area_get (ospf, area_id, OSPF_AREA_ID_FORMAT_ADDRESS);
paul718e3742002-12-13 20:15:29 +0000827 voi->area = area;
828
829 if (IS_DEBUG_OSPF_EVENT)
830 zlog_info ("ospf_vl_new(): set associated area to the backbone");
831
832 ospf_area_add_if (voi->area, voi);
833
834 ospf_if_stream_set (voi);
835
836 if (IS_DEBUG_OSPF_EVENT)
837 zlog_info ("ospf_vl_new(): Stop");
838 return voi;
839}
840
841void
842ospf_vl_if_delete (struct ospf_vl_data *vl_data)
843{
844 struct interface *ifp = vl_data->vl_oi->ifp;
845 vl_data->vl_oi->address->u.prefix4.s_addr = 0;
846 vl_data->vl_oi->address->prefixlen = 0;
847 ospf_if_free (vl_data->vl_oi);
848 if_delete (ifp);
849 vlink_count--;
850}
851
852struct ospf_vl_data *
853ospf_vl_lookup (struct ospf_area *area, struct in_addr vl_peer)
854{
855 struct ospf_vl_data *vl_data;
856 listnode node;
857
paul68980082003-03-25 05:07:42 +0000858 for (node = listhead (area->ospf->vlinks); node; nextnode (node))
paul718e3742002-12-13 20:15:29 +0000859 if ((vl_data = getdata (node)) != NULL)
860 if (vl_data->vl_peer.s_addr == vl_peer.s_addr &&
861 IPV4_ADDR_SAME (&vl_data->vl_area_id, &area->area_id))
862 return vl_data;
863
864 return NULL;
865}
866
867void
868ospf_vl_shutdown (struct ospf_vl_data *vl_data)
869{
870 struct ospf_interface *oi;
871
872 if ((oi = vl_data->vl_oi) == NULL)
873 return;
874
875 oi->address->u.prefix4.s_addr = 0;
876 oi->address->prefixlen = 0;
877
878 UNSET_FLAG (oi->ifp->flags, IFF_UP);
879 /* OSPF_ISM_EVENT_SCHEDULE (oi, ISM_InterfaceDown); */
880 OSPF_ISM_EVENT_EXECUTE (oi, ISM_InterfaceDown);
881}
882
883void
paul68980082003-03-25 05:07:42 +0000884ospf_vl_add (struct ospf *ospf, struct ospf_vl_data *vl_data)
paul718e3742002-12-13 20:15:29 +0000885{
paul68980082003-03-25 05:07:42 +0000886 listnode_add (ospf->vlinks, vl_data);
paul718e3742002-12-13 20:15:29 +0000887#ifdef HAVE_SNMP
888 ospf_snmp_vl_add (vl_data);
889#endif /* HAVE_SNMP */
890}
891
892void
paul68980082003-03-25 05:07:42 +0000893ospf_vl_delete (struct ospf *ospf, struct ospf_vl_data *vl_data)
paul718e3742002-12-13 20:15:29 +0000894{
895 ospf_vl_shutdown (vl_data);
896 ospf_vl_if_delete (vl_data);
897
898#ifdef HAVE_SNMP
899 ospf_snmp_vl_delete (vl_data);
900#endif /* HAVE_SNMP */
paul68980082003-03-25 05:07:42 +0000901 listnode_delete (ospf->vlinks, vl_data);
paul718e3742002-12-13 20:15:29 +0000902
903 ospf_vl_data_free (vl_data);
904}
905
paul2e6b0bb2003-06-22 08:17:12 +0000906int
paul718e3742002-12-13 20:15:29 +0000907ospf_vl_set_params (struct ospf_vl_data *vl_data, struct vertex *v)
908{
909 int changed = 0;
910 struct ospf_interface *voi;
911 listnode node;
912 struct vertex_nexthop *nh;
913 int i;
914 struct router_lsa *rl;
915
916 voi = vl_data->vl_oi;
917
918 if (voi->output_cost != v->distance)
919 {
920 voi->output_cost = v->distance;
921 changed = 1;
922 }
923
924 for (node = listhead (v->nexthop); node; nextnode (node))
925 if ((nh = getdata (node)) != NULL)
926 {
927 vl_data->out_oi = (struct ospf_interface *) nh->oi;
928
929 voi->address->u.prefix4 = vl_data->out_oi->address->u.prefix4;
930 voi->address->prefixlen = vl_data->out_oi->address->prefixlen;
931
932 break; /* We take the first interface. */
933 }
934
935 rl = (struct router_lsa *)v->lsa;
pauld355bfa2004-04-08 07:43:45 +0000936
937 /* use SPF determined backlink index in struct vertex
938 * for virtual link destination address
939 */
940 if (v->backlink >= 0)
paul718e3742002-12-13 20:15:29 +0000941 {
pauld355bfa2004-04-08 07:43:45 +0000942 if (!IPV4_ADDR_SAME (&vl_data->peer_addr,
943 &rl->link[v->backlink].link_data))
944 changed = 1;
945 vl_data->peer_addr = rl->link[v->backlink].link_data;
946 }
947 else
948 {
949 /* This is highly odd, there is no backlink index
950 * there should be due to the ospf_spf_has_link() check
951 * in SPF. Lets warn and try pick a link anyway.
952 */
953 zlog_warn ("ospf_vl_set_params: No backlink for %s!",
954 vl_data->vl_oi->ifp->name);
955 for (i = 0; i < ntohs (rl->links); i++)
paul2e6b0bb2003-06-22 08:17:12 +0000956 {
pauld355bfa2004-04-08 07:43:45 +0000957 switch (rl->link[i].type)
958 {
959 case LSA_LINK_TYPE_VIRTUALLINK:
960 if (IS_DEBUG_OSPF_EVENT)
961 zlog_info ("found back link through VL");
962 case LSA_LINK_TYPE_TRANSIT:
963 case LSA_LINK_TYPE_POINTOPOINT:
964 vl_data->peer_addr = rl->link[i].link_data;
965 if (IS_DEBUG_OSPF_EVENT)
966 zlog_info ("%s peer address is %s\n",
967 vl_data->vl_oi->ifp->name,
968 inet_ntoa(vl_data->peer_addr));
969 return changed;
970 }
paul2e6b0bb2003-06-22 08:17:12 +0000971 }
paul718e3742002-12-13 20:15:29 +0000972 }
pauld355bfa2004-04-08 07:43:45 +0000973
974 if (IS_DEBUG_OSPF_EVENT)
975 zlog_info ("ospf_vl_set_params: %s peer address is %s\n",
976 vl_data->vl_oi->ifp->name,
977 inet_ntoa(vl_data->peer_addr));
978
paul2e6b0bb2003-06-22 08:17:12 +0000979 return changed;
paul718e3742002-12-13 20:15:29 +0000980}
981
982
983void
paul68980082003-03-25 05:07:42 +0000984ospf_vl_up_check (struct ospf_area *area, struct in_addr rid,
paul718e3742002-12-13 20:15:29 +0000985 struct vertex *v)
986{
paul68980082003-03-25 05:07:42 +0000987 struct ospf *ospf = area->ospf;
paul718e3742002-12-13 20:15:29 +0000988 listnode node;
989 struct ospf_vl_data *vl_data;
990 struct ospf_interface *oi;
991
992 if (IS_DEBUG_OSPF_EVENT)
993 {
994 zlog_info ("ospf_vl_up_check(): Start");
995 zlog_info ("ospf_vl_up_check(): Router ID is %s", inet_ntoa (rid));
996 zlog_info ("ospf_vl_up_check(): Area is %s", inet_ntoa (area->area_id));
997 }
998
paul68980082003-03-25 05:07:42 +0000999 for (node = listhead (ospf->vlinks); node; nextnode (node))
paul718e3742002-12-13 20:15:29 +00001000 {
1001 if ((vl_data = getdata (node)) == NULL)
1002 continue;
1003
1004 if (IS_DEBUG_OSPF_EVENT)
1005 {
1006 zlog_info ("ospf_vl_up_check(): considering VL, name: %s",
1007 vl_data->vl_oi->ifp->name);
1008 zlog_info ("ospf_vl_up_check(): VL area: %s, peer ID: %s",
1009 inet_ntoa (vl_data->vl_area_id),
1010 inet_ntoa (vl_data->vl_peer));
1011 }
1012
1013 if (IPV4_ADDR_SAME (&vl_data->vl_peer, &rid) &&
1014 IPV4_ADDR_SAME (&vl_data->vl_area_id, &area->area_id))
1015 {
1016 oi = vl_data->vl_oi;
1017 SET_FLAG (vl_data->flags, OSPF_VL_FLAG_APPROVED);
1018
1019 if (IS_DEBUG_OSPF_EVENT)
1020 zlog_info ("ospf_vl_up_check(): this VL matched");
1021
1022 if (oi->state == ISM_Down)
1023 {
1024 if (IS_DEBUG_OSPF_EVENT)
1025 zlog_info ("ospf_vl_up_check(): VL is down, waking it up");
1026 SET_FLAG (oi->ifp->flags, IFF_UP);
paul2e6b0bb2003-06-22 08:17:12 +00001027 OSPF_ISM_EVENT_EXECUTE(oi,ISM_InterfaceUp);
paul718e3742002-12-13 20:15:29 +00001028 }
1029
paul2e6b0bb2003-06-22 08:17:12 +00001030 if (ospf_vl_set_params (vl_data, v))
1031 {
1032 if (IS_DEBUG_OSPF (ism, ISM_EVENTS))
1033 zlog_info ("ospf_vl_up_check: VL cost change, scheduling router lsa refresh");
1034 if(ospf->backbone)
1035 ospf_router_lsa_timer_add(ospf->backbone);
1036 else if (IS_DEBUG_OSPF (ism, ISM_EVENTS))
1037 zlog_info ("ospf_vl_up_check: VL cost change, no backbone!");
1038 }
paul718e3742002-12-13 20:15:29 +00001039 }
1040 }
1041}
1042
1043void
paul68980082003-03-25 05:07:42 +00001044ospf_vl_unapprove (struct ospf *ospf)
paul718e3742002-12-13 20:15:29 +00001045{
1046 listnode node;
1047 struct ospf_vl_data *vl_data;
1048
paul68980082003-03-25 05:07:42 +00001049 for (node = listhead (ospf->vlinks); node; nextnode (node))
paul718e3742002-12-13 20:15:29 +00001050 if ((vl_data = getdata (node)) != NULL)
1051 UNSET_FLAG (vl_data->flags, OSPF_VL_FLAG_APPROVED);
1052}
1053
1054void
paul68980082003-03-25 05:07:42 +00001055ospf_vl_shut_unapproved (struct ospf *ospf)
paul718e3742002-12-13 20:15:29 +00001056{
1057 listnode node;
1058 struct ospf_vl_data *vl_data;
1059
paul68980082003-03-25 05:07:42 +00001060 for (node = listhead (ospf->vlinks); node; nextnode (node))
paul718e3742002-12-13 20:15:29 +00001061 if ((vl_data = getdata (node)) != NULL)
1062 if (!CHECK_FLAG (vl_data->flags, OSPF_VL_FLAG_APPROVED))
1063 ospf_vl_shutdown (vl_data);
1064}
1065
1066int
1067ospf_full_virtual_nbrs (struct ospf_area *area)
1068{
1069 if (IS_DEBUG_OSPF_EVENT)
1070 {
1071 zlog_info ("counting fully adjacent virtual neighbors in area %s",
1072 inet_ntoa (area->area_id));
1073 zlog_info ("there are %d of them", area->full_vls);
1074 }
1075
1076 return area->full_vls;
1077}
1078
1079int
1080ospf_vls_in_area (struct ospf_area *area)
1081{
1082 listnode node;
1083 struct ospf_vl_data *vl_data;
1084 int c = 0;
1085
paul68980082003-03-25 05:07:42 +00001086 for (node = listhead (area->ospf->vlinks); node; nextnode (node))
paul718e3742002-12-13 20:15:29 +00001087 if ((vl_data = getdata (node)) != NULL)
1088 if (IPV4_ADDR_SAME (&vl_data->vl_area_id, &area->area_id))
1089 c++;
1090
1091 return c;
1092}
1093
1094
1095struct crypt_key *
1096ospf_crypt_key_new ()
1097{
1098 struct crypt_key *ck;
1099
1100 ck = XMALLOC (MTYPE_OSPF_CRYPT_KEY, sizeof (struct crypt_key));
1101 memset (ck, 0, sizeof (struct crypt_key));
1102
1103 return ck;
1104}
1105
1106void
1107ospf_crypt_key_add (list crypt, struct crypt_key *ck)
1108{
1109 listnode_add (crypt, ck);
1110}
1111
1112struct crypt_key *
1113ospf_crypt_key_lookup (list auth_crypt, u_char key_id)
1114{
1115 listnode node;
1116 struct crypt_key *ck;
1117
1118 for (node = listhead (auth_crypt); node; nextnode (node))
1119 {
1120 ck = getdata (node);
1121 if (ck->key_id == key_id)
1122 return ck;
1123 }
1124
1125 return NULL;
1126}
1127
1128int
1129ospf_crypt_key_delete (list auth_crypt, u_char key_id)
1130{
1131 listnode node;
1132 struct crypt_key *ck;
1133
1134 for (node = listhead (auth_crypt); node; nextnode (node))
1135 {
1136 ck = getdata (node);
1137 if (ck->key_id == key_id)
1138 {
1139 listnode_delete (auth_crypt, ck);
1140 return 1;
1141 }
1142 }
1143
1144 return 0;
1145}
1146
1147void
1148ospf_if_init ()
1149{
1150 /* Initialize Zebra interface data structure. */
1151 if_init ();
paul020709f2003-04-04 02:44:16 +00001152 om->iflist = iflist;
paul718e3742002-12-13 20:15:29 +00001153 if_add_hook (IF_NEW_HOOK, ospf_if_new_hook);
1154 if_add_hook (IF_DELETE_HOOK, ospf_if_delete_hook);
1155}