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