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