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