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