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