]> git.proxmox.com Git - mirror_frr.git/blob - ospfd/ospf_interface.c
ospfd: Allow unnumbered and numbered addresses to co-exist better
[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 #include "ldp_sync.h"
36
37 #include "ospfd/ospfd.h"
38 #include "ospfd/ospf_bfd.h"
39 #include "ospfd/ospf_spf.h"
40 #include "ospfd/ospf_interface.h"
41 #include "ospfd/ospf_ism.h"
42 #include "ospfd/ospf_asbr.h"
43 #include "ospfd/ospf_lsa.h"
44 #include "ospfd/ospf_lsdb.h"
45 #include "ospfd/ospf_neighbor.h"
46 #include "ospfd/ospf_nsm.h"
47 #include "ospfd/ospf_packet.h"
48 #include "ospfd/ospf_abr.h"
49 #include "ospfd/ospf_network.h"
50 #include "ospfd/ospf_dump.h"
51 #include "ospfd/ospf_ldp_sync.h"
52 #include "ospfd/ospf_route.h"
53 #include "ospfd/ospf_te.h"
54
55 DEFINE_QOBJ_TYPE(ospf_interface);
56 DEFINE_HOOK(ospf_vl_add, (struct ospf_vl_data * vd), (vd));
57 DEFINE_HOOK(ospf_vl_delete, (struct ospf_vl_data * vd), (vd));
58 DEFINE_HOOK(ospf_if_update, (struct interface * ifp), (ifp));
59 DEFINE_HOOK(ospf_if_delete, (struct interface * ifp), (ifp));
60
61 int ospf_interface_neighbor_count(struct ospf_interface *oi)
62 {
63 int count = 0;
64 struct route_node *rn;
65 struct ospf_neighbor *nbr = NULL;
66
67 for (rn = route_top(oi->nbrs); rn; rn = route_next(rn)) {
68 nbr = rn->info;
69 if (!nbr)
70 continue;
71
72 /* Do not show myself. */
73 if (nbr == oi->nbr_self)
74 continue;
75 /* Down state is not shown. */
76 if (nbr->state == NSM_Down)
77 continue;
78 count++;
79 }
80
81 return count;
82 }
83
84 int ospf_if_get_output_cost(struct ospf_interface *oi)
85 {
86 /* If all else fails, use default OSPF cost */
87 uint32_t cost;
88 uint32_t bw, refbw;
89
90 /* if LDP-IGP Sync is running on interface set cost so interface
91 * is used only as last resort
92 */
93 if (ldp_sync_if_is_enabled(IF_DEF_PARAMS(oi->ifp)->ldp_sync_info))
94 return (LDP_OSPF_LSINFINITY);
95
96 /* ifp speed and bw can be 0 in some platforms, use ospf default bw
97 if bw is configured under interface it would be used.
98 */
99 if (!oi->ifp->bandwidth && oi->ifp->speed)
100 bw = oi->ifp->speed;
101 else
102 bw = oi->ifp->bandwidth ? oi->ifp->bandwidth
103 : OSPF_DEFAULT_BANDWIDTH;
104 refbw = oi->ospf->ref_bandwidth;
105
106 /* A specified ip ospf cost overrides a calculated one. */
107 if (OSPF_IF_PARAM_CONFIGURED(IF_DEF_PARAMS(oi->ifp), output_cost_cmd)
108 || OSPF_IF_PARAM_CONFIGURED(oi->params, output_cost_cmd))
109 cost = OSPF_IF_PARAM(oi, output_cost_cmd);
110 /* See if a cost can be calculated from the zebra processes
111 interface bandwidth field. */
112 else {
113 cost = (uint32_t)((double)refbw / (double)bw + (double)0.5);
114 if (cost < 1)
115 cost = 1;
116 else if (cost > 65535)
117 cost = 65535;
118 }
119
120 return cost;
121 }
122
123 void ospf_if_recalculate_output_cost(struct interface *ifp)
124 {
125 uint32_t newcost;
126 struct route_node *rn;
127
128 for (rn = route_top(IF_OIFS(ifp)); rn; rn = route_next(rn)) {
129 struct ospf_interface *oi;
130
131 if ((oi = rn->info) == NULL)
132 continue;
133
134 newcost = ospf_if_get_output_cost(oi);
135
136 /* Is actual output cost changed? */
137 if (oi->output_cost != newcost) {
138 oi->output_cost = newcost;
139 ospf_router_lsa_update_area(oi->area);
140 }
141 }
142 }
143
144 /* Simulate down/up on the interface. This is needed, for example, when
145 the MTU changes. */
146 void ospf_if_reset(struct interface *ifp)
147 {
148 struct route_node *rn;
149
150 for (rn = route_top(IF_OIFS(ifp)); rn; rn = route_next(rn)) {
151 struct ospf_interface *oi;
152
153 if ((oi = rn->info) == NULL)
154 continue;
155
156 ospf_if_down(oi);
157 ospf_if_up(oi);
158 }
159 }
160
161 void ospf_if_reset_variables(struct ospf_interface *oi)
162 {
163 /* Set default values. */
164 /* don't clear this flag. oi->flag = OSPF_IF_DISABLE; */
165
166 if (oi->vl_data)
167 oi->type = OSPF_IFTYPE_VIRTUALLINK;
168 else
169 /* preserve network-type */
170 if (oi->type != OSPF_IFTYPE_NBMA)
171 oi->type = OSPF_IFTYPE_BROADCAST;
172
173 oi->state = ISM_Down;
174
175 oi->crypt_seqnum = 0;
176
177 /* This must be short, (less than RxmtInterval)
178 - RFC 2328 Section 13.5 para 3. Set to 1 second to avoid Acks being
179 held back for too long - MAG */
180 oi->v_ls_ack = 1;
181 }
182
183 /* lookup oi for specified prefix/ifp */
184 struct ospf_interface *ospf_if_table_lookup(struct interface *ifp,
185 struct prefix *prefix)
186 {
187 struct prefix p;
188 struct route_node *rn;
189 struct ospf_interface *rninfo = NULL;
190
191 p = *prefix;
192 p.prefixlen = IPV4_MAX_BITLEN;
193
194 /* route_node_get implicitely locks */
195 if ((rn = route_node_lookup(IF_OIFS(ifp), &p))) {
196 rninfo = (struct ospf_interface *)rn->info;
197 route_unlock_node(rn);
198 }
199
200 return rninfo;
201 }
202
203 static void ospf_add_to_if(struct interface *ifp, struct ospf_interface *oi)
204 {
205 struct route_node *rn;
206 struct prefix p;
207
208 p = *oi->address;
209 p.prefixlen = IPV4_MAX_BITLEN;
210 apply_mask(&p);
211
212 rn = route_node_get(IF_OIFS(ifp), &p);
213 /* rn->info should either be NULL or equal to this oi
214 * as route_node_get may return an existing node
215 */
216 assert(!rn->info || rn->info == oi);
217 rn->info = oi;
218 }
219
220 static void ospf_delete_from_if(struct interface *ifp,
221 struct ospf_interface *oi)
222 {
223 struct route_node *rn;
224 struct prefix p;
225
226 p = *oi->address;
227 p.prefixlen = IPV4_MAX_BITLEN;
228
229 rn = route_node_lookup(IF_OIFS(oi->ifp), &p);
230 assert(rn);
231 assert(rn->info);
232 rn->info = NULL;
233 route_unlock_node(rn);
234 route_unlock_node(rn);
235 }
236
237 struct ospf_interface *ospf_if_new(struct ospf *ospf, struct interface *ifp,
238 struct prefix *p)
239 {
240 struct ospf_interface *oi;
241
242 oi = ospf_if_table_lookup(ifp, p);
243 if (oi)
244 return oi;
245
246 oi = XCALLOC(MTYPE_OSPF_IF, sizeof(struct ospf_interface));
247
248 oi->obuf = ospf_fifo_new();
249
250 /* Set zebra interface pointer. */
251 oi->ifp = ifp;
252 oi->address = p;
253
254 ospf_add_to_if(ifp, oi);
255 listnode_add(ospf->oiflist, oi);
256
257 /* Initialize neighbor list. */
258 oi->nbrs = route_table_init();
259
260 /* Initialize static neighbor list. */
261 oi->nbr_nbma = list_new();
262
263 /* Initialize Link State Acknowledgment list. */
264 oi->ls_ack = list_new();
265 oi->ls_ack_direct.ls_ack = list_new();
266
267 /* Set default values. */
268 ospf_if_reset_variables(oi);
269
270 /* Set pseudo neighbor to Null */
271 oi->nbr_self = NULL;
272
273 oi->ls_upd_queue = route_table_init();
274 oi->t_ls_upd_event = NULL;
275 oi->t_ls_ack_direct = NULL;
276
277 oi->crypt_seqnum = time(NULL);
278
279 ospf_opaque_type9_lsa_init(oi);
280
281 oi->ospf = ospf;
282
283 QOBJ_REG(oi, ospf_interface);
284
285 if (IS_DEBUG_OSPF_EVENT)
286 zlog_debug("%s: ospf interface %s vrf %s id %u created",
287 __func__, ifp->name, ospf_get_name(ospf),
288 ospf->vrf_id);
289
290 return oi;
291 }
292
293 /* Restore an interface to its pre UP state
294 Used from ism_interface_down only */
295 void ospf_if_cleanup(struct ospf_interface *oi)
296 {
297 struct route_node *rn;
298 struct listnode *node, *nnode;
299 struct ospf_neighbor *nbr;
300 struct ospf_nbr_nbma *nbr_nbma;
301 struct ospf_lsa *lsa;
302
303 /* oi->nbrs and oi->nbr_nbma should be deleted on InterfaceDown event */
304 /* delete all static neighbors attached to this interface */
305 for (ALL_LIST_ELEMENTS(oi->nbr_nbma, node, nnode, nbr_nbma)) {
306 THREAD_OFF(nbr_nbma->t_poll);
307
308 if (nbr_nbma->nbr) {
309 nbr_nbma->nbr->nbr_nbma = NULL;
310 nbr_nbma->nbr = NULL;
311 }
312
313 nbr_nbma->oi = NULL;
314
315 listnode_delete(oi->nbr_nbma, nbr_nbma);
316 }
317
318 /* send Neighbor event KillNbr to all associated neighbors. */
319 for (rn = route_top(oi->nbrs); rn; rn = route_next(rn)) {
320 if ((nbr = rn->info) != NULL)
321 if (nbr != oi->nbr_self)
322 OSPF_NSM_EVENT_EXECUTE(nbr, NSM_KillNbr);
323 }
324
325 /* Cleanup Link State Acknowlegdment list. */
326 for (ALL_LIST_ELEMENTS(oi->ls_ack, node, nnode, lsa))
327 ospf_lsa_unlock(&lsa); /* oi->ls_ack */
328 list_delete_all_node(oi->ls_ack);
329
330 oi->crypt_seqnum = 0;
331
332 /* Empty link state update queue */
333 ospf_ls_upd_queue_empty(oi);
334
335 /* Reset pseudo neighbor. */
336 ospf_nbr_self_reset(oi, oi->ospf->router_id);
337 }
338
339 void ospf_if_free(struct ospf_interface *oi)
340 {
341 ospf_if_down(oi);
342
343 ospf_fifo_free(oi->obuf);
344
345 assert(oi->state == ISM_Down);
346
347 ospf_opaque_type9_lsa_term(oi);
348
349 QOBJ_UNREG(oi);
350
351 /* Free Pseudo Neighbour */
352 ospf_nbr_delete(oi->nbr_self);
353
354 route_table_finish(oi->nbrs);
355 route_table_finish(oi->ls_upd_queue);
356
357 /* Free any lists that should be freed */
358 list_delete(&oi->nbr_nbma);
359
360 list_delete(&oi->ls_ack);
361 list_delete(&oi->ls_ack_direct.ls_ack);
362
363 if (IS_DEBUG_OSPF_EVENT)
364 zlog_debug("%s: ospf interface %s vrf %s id %u deleted",
365 __func__, oi->ifp->name, oi->ifp->vrf->name,
366 oi->ifp->vrf->vrf_id);
367
368 ospf_delete_from_if(oi->ifp, oi);
369
370 listnode_delete(oi->ospf->oiflist, oi);
371 listnode_delete(oi->area->oiflist, oi);
372
373 thread_cancel_event(master, oi);
374
375 memset(oi, 0, sizeof(*oi));
376 XFREE(MTYPE_OSPF_IF, oi);
377 }
378
379 int ospf_if_is_up(struct ospf_interface *oi)
380 {
381 return if_is_up(oi->ifp);
382 }
383
384 struct ospf_interface *ospf_if_exists(struct ospf_interface *oic)
385 {
386 struct listnode *node;
387 struct ospf *ospf;
388 struct ospf_interface *oi;
389
390 if (!oic)
391 return NULL;
392
393 ospf = oic->ospf;
394 if (ospf == NULL)
395 return NULL;
396
397 for (ALL_LIST_ELEMENTS_RO(ospf->oiflist, node, oi))
398 if (oi == oic)
399 return oi;
400
401 return NULL;
402 }
403
404 /* Lookup OSPF interface by router LSA posistion */
405 struct ospf_interface *ospf_if_lookup_by_lsa_pos(struct ospf_area *area,
406 int lsa_pos)
407 {
408 struct listnode *node;
409 struct ospf_interface *oi;
410
411 for (ALL_LIST_ELEMENTS_RO(area->oiflist, node, oi)) {
412 if (lsa_pos >= oi->lsa_pos_beg && lsa_pos < oi->lsa_pos_end)
413 return oi;
414 }
415 return NULL;
416 }
417
418 struct ospf_interface *ospf_if_lookup_by_local_addr(struct ospf *ospf,
419 struct interface *ifp,
420 struct in_addr address)
421 {
422 struct listnode *node;
423 struct ospf_interface *oi;
424
425 for (ALL_LIST_ELEMENTS_RO(ospf->oiflist, node, oi))
426 if (oi->type != OSPF_IFTYPE_VIRTUALLINK) {
427 if (ifp && oi->ifp != ifp)
428 continue;
429
430 if (IPV4_ADDR_SAME(&address, &oi->address->u.prefix4))
431 return oi;
432 }
433
434 return NULL;
435 }
436
437 struct ospf_interface *ospf_if_lookup_by_prefix(struct ospf *ospf,
438 struct prefix_ipv4 *p)
439 {
440 struct listnode *node;
441 struct ospf_interface *oi;
442
443 /* Check each Interface. */
444 for (ALL_LIST_ELEMENTS_RO(ospf->oiflist, node, oi)) {
445 if (oi->type != OSPF_IFTYPE_VIRTUALLINK) {
446 struct prefix ptmp;
447
448 prefix_copy(&ptmp, CONNECTED_PREFIX(oi->connected));
449 apply_mask(&ptmp);
450 if (prefix_same(&ptmp, (struct prefix *)p))
451 return oi;
452 }
453 }
454 return NULL;
455 }
456
457 /* determine receiving interface by ifp and source address */
458 struct ospf_interface *ospf_if_lookup_recv_if(struct ospf *ospf,
459 struct in_addr src,
460 struct interface *ifp)
461 {
462 struct route_node *rn;
463 struct prefix_ipv4 addr;
464 struct ospf_interface *oi, *match, *unnumbered_match;
465
466 addr.family = AF_INET;
467 addr.prefix = src;
468 addr.prefixlen = IPV4_MAX_BITLEN;
469
470 match = unnumbered_match = NULL;
471
472 for (rn = route_top(IF_OIFS(ifp)); rn; rn = route_next(rn)) {
473 oi = rn->info;
474
475 if (!oi) /* oi can be NULL for PtP aliases */
476 continue;
477
478 if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
479 continue;
480
481 if (if_is_loopback(oi->ifp))
482 continue;
483
484 if (CHECK_FLAG(oi->connected->flags, ZEBRA_IFA_UNNUMBERED))
485 unnumbered_match = oi;
486 else if (prefix_match(CONNECTED_PREFIX(oi->connected),
487 (struct prefix *)&addr)) {
488 if ((match == NULL) || (match->address->prefixlen
489 < oi->address->prefixlen))
490 match = oi;
491 }
492 }
493
494 if (match)
495 return match;
496
497 return unnumbered_match;
498 }
499
500 void ospf_interface_fifo_flush(struct ospf_interface *oi)
501 {
502 struct ospf *ospf = oi->ospf;
503
504 ospf_fifo_flush(oi->obuf);
505
506 if (oi->on_write_q) {
507 listnode_delete(ospf->oi_write_q, oi);
508 if (list_isempty(ospf->oi_write_q))
509 THREAD_OFF(ospf->t_write);
510 oi->on_write_q = 0;
511 }
512 }
513
514 static void ospf_if_reset_stats(struct ospf_interface *oi)
515 {
516 oi->hello_in = oi->hello_out = 0;
517 oi->db_desc_in = oi->db_desc_out = 0;
518 oi->ls_req_in = oi->ls_req_out = 0;
519 oi->ls_upd_in = oi->ls_upd_out = 0;
520 oi->ls_ack_in = oi->ls_ack_out = 0;
521 }
522
523 void ospf_if_stream_unset(struct ospf_interface *oi)
524 {
525 /* flush the interface packet queue */
526 ospf_interface_fifo_flush(oi);
527 /*reset protocol stats */
528 ospf_if_reset_stats(oi);
529 }
530
531
532 static struct ospf_if_params *ospf_new_if_params(void)
533 {
534 struct ospf_if_params *oip;
535
536 oip = XCALLOC(MTYPE_OSPF_IF_PARAMS, sizeof(struct ospf_if_params));
537
538 UNSET_IF_PARAM(oip, output_cost_cmd);
539 UNSET_IF_PARAM(oip, transmit_delay);
540 UNSET_IF_PARAM(oip, retransmit_interval);
541 UNSET_IF_PARAM(oip, passive_interface);
542 UNSET_IF_PARAM(oip, v_hello);
543 UNSET_IF_PARAM(oip, fast_hello);
544 UNSET_IF_PARAM(oip, v_wait);
545 UNSET_IF_PARAM(oip, priority);
546 UNSET_IF_PARAM(oip, type);
547 UNSET_IF_PARAM(oip, auth_simple);
548 UNSET_IF_PARAM(oip, auth_crypt);
549 UNSET_IF_PARAM(oip, auth_type);
550 UNSET_IF_PARAM(oip, if_area);
551
552 oip->auth_crypt = list_new();
553
554 oip->network_lsa_seqnum = htonl(OSPF_INITIAL_SEQUENCE_NUMBER);
555 oip->is_v_wait_set = false;
556
557 oip->ptp_dmvpn = 0;
558
559 return oip;
560 }
561
562 static void ospf_del_if_params(struct interface *ifp,
563 struct ospf_if_params *oip)
564 {
565 list_delete(&oip->auth_crypt);
566 ospf_interface_disable_bfd(ifp, oip);
567 ldp_sync_info_free(&(oip->ldp_sync_info));
568 XFREE(MTYPE_OSPF_IF_PARAMS, oip);
569 }
570
571 void ospf_free_if_params(struct interface *ifp, struct in_addr addr)
572 {
573 struct ospf_if_params *oip;
574 struct prefix_ipv4 p;
575 struct route_node *rn;
576
577 p.family = AF_INET;
578 p.prefixlen = IPV4_MAX_BITLEN;
579 p.prefix = addr;
580 rn = route_node_lookup(IF_OIFS_PARAMS(ifp), (struct prefix *)&p);
581 if (!rn || !rn->info)
582 return;
583
584 oip = rn->info;
585 route_unlock_node(rn);
586
587 if (!OSPF_IF_PARAM_CONFIGURED(oip, output_cost_cmd)
588 && !OSPF_IF_PARAM_CONFIGURED(oip, transmit_delay)
589 && !OSPF_IF_PARAM_CONFIGURED(oip, retransmit_interval)
590 && !OSPF_IF_PARAM_CONFIGURED(oip, passive_interface)
591 && !OSPF_IF_PARAM_CONFIGURED(oip, v_hello)
592 && !OSPF_IF_PARAM_CONFIGURED(oip, fast_hello)
593 && !OSPF_IF_PARAM_CONFIGURED(oip, v_wait)
594 && !OSPF_IF_PARAM_CONFIGURED(oip, priority)
595 && !OSPF_IF_PARAM_CONFIGURED(oip, type)
596 && !OSPF_IF_PARAM_CONFIGURED(oip, auth_simple)
597 && !OSPF_IF_PARAM_CONFIGURED(oip, auth_type)
598 && !OSPF_IF_PARAM_CONFIGURED(oip, if_area)
599 && listcount(oip->auth_crypt) == 0) {
600 ospf_del_if_params(ifp, oip);
601 rn->info = NULL;
602 route_unlock_node(rn);
603 }
604 }
605
606 struct ospf_if_params *ospf_lookup_if_params(struct interface *ifp,
607 struct in_addr addr)
608 {
609 struct prefix_ipv4 p;
610 struct route_node *rn;
611
612 p.family = AF_INET;
613 p.prefixlen = IPV4_MAX_BITLEN;
614 p.prefix = addr;
615
616 rn = route_node_lookup(IF_OIFS_PARAMS(ifp), (struct prefix *)&p);
617
618 if (rn) {
619 route_unlock_node(rn);
620 return rn->info;
621 }
622
623 return NULL;
624 }
625
626 struct ospf_if_params *ospf_get_if_params(struct interface *ifp,
627 struct in_addr addr)
628 {
629 struct prefix_ipv4 p;
630 struct route_node *rn;
631
632 p.family = AF_INET;
633 p.prefixlen = IPV4_MAX_BITLEN;
634 p.prefix = addr;
635 apply_mask_ipv4(&p);
636
637 rn = route_node_get(IF_OIFS_PARAMS(ifp), (struct prefix *)&p);
638
639 if (rn->info == NULL)
640 rn->info = ospf_new_if_params();
641 else
642 route_unlock_node(rn);
643
644 return rn->info;
645 }
646
647 void ospf_if_update_params(struct interface *ifp, struct in_addr addr)
648 {
649 struct route_node *rn;
650 struct ospf_interface *oi;
651
652 for (rn = route_top(IF_OIFS(ifp)); rn; rn = route_next(rn)) {
653 if ((oi = rn->info) == NULL)
654 continue;
655
656 if (IPV4_ADDR_SAME(&oi->address->u.prefix4, &addr))
657 oi->params = ospf_lookup_if_params(
658 ifp, oi->address->u.prefix4);
659 }
660 }
661
662 int ospf_if_new_hook(struct interface *ifp)
663 {
664 int rc = 0;
665
666 ifp->info = XCALLOC(MTYPE_OSPF_IF_INFO, sizeof(struct ospf_if_info));
667
668 IF_OIFS(ifp) = route_table_init();
669 IF_OIFS_PARAMS(ifp) = route_table_init();
670
671 IF_DEF_PARAMS(ifp) = ospf_new_if_params();
672
673 SET_IF_PARAM(IF_DEF_PARAMS(ifp), transmit_delay);
674 IF_DEF_PARAMS(ifp)->transmit_delay = OSPF_TRANSMIT_DELAY_DEFAULT;
675
676 SET_IF_PARAM(IF_DEF_PARAMS(ifp), retransmit_interval);
677 IF_DEF_PARAMS(ifp)->retransmit_interval =
678 OSPF_RETRANSMIT_INTERVAL_DEFAULT;
679
680 SET_IF_PARAM(IF_DEF_PARAMS(ifp), priority);
681 IF_DEF_PARAMS(ifp)->priority = OSPF_ROUTER_PRIORITY_DEFAULT;
682
683 IF_DEF_PARAMS(ifp)->mtu_ignore = OSPF_MTU_IGNORE_DEFAULT;
684
685 SET_IF_PARAM(IF_DEF_PARAMS(ifp), v_hello);
686 IF_DEF_PARAMS(ifp)->v_hello = OSPF_HELLO_INTERVAL_DEFAULT;
687
688 SET_IF_PARAM(IF_DEF_PARAMS(ifp), fast_hello);
689 IF_DEF_PARAMS(ifp)->fast_hello = OSPF_FAST_HELLO_DEFAULT;
690
691 SET_IF_PARAM(IF_DEF_PARAMS(ifp), v_wait);
692 IF_DEF_PARAMS(ifp)->v_wait = OSPF_ROUTER_DEAD_INTERVAL_DEFAULT;
693
694 SET_IF_PARAM(IF_DEF_PARAMS(ifp), auth_simple);
695 memset(IF_DEF_PARAMS(ifp)->auth_simple, 0, OSPF_AUTH_SIMPLE_SIZE);
696
697 SET_IF_PARAM(IF_DEF_PARAMS(ifp), auth_type);
698 IF_DEF_PARAMS(ifp)->auth_type = OSPF_AUTH_NOTSET;
699
700 rc = ospf_opaque_new_if(ifp);
701 return rc;
702 }
703
704 static int ospf_if_delete_hook(struct interface *ifp)
705 {
706 int rc = 0;
707 struct route_node *rn;
708 rc = ospf_opaque_del_if(ifp);
709
710 /*
711 * This function must be called before `route_table_finish` due to
712 * BFD integration need to iterate over the interface neighbors to
713 * remove all registrations.
714 */
715 ospf_del_if_params(ifp, IF_DEF_PARAMS(ifp));
716
717 for (rn = route_top(IF_OIFS_PARAMS(ifp)); rn; rn = route_next(rn))
718 if (rn->info)
719 ospf_del_if_params(ifp, rn->info);
720
721 route_table_finish(IF_OIFS(ifp));
722 route_table_finish(IF_OIFS_PARAMS(ifp));
723
724 XFREE(MTYPE_OSPF_IF_INFO, ifp->info);
725
726 return rc;
727 }
728
729 int ospf_if_is_enable(struct ospf_interface *oi)
730 {
731 if (!(if_is_loopback(oi->ifp)))
732 if (if_is_up(oi->ifp))
733 return 1;
734
735 return 0;
736 }
737
738 void ospf_if_set_multicast(struct ospf_interface *oi)
739 {
740 if ((oi->state > ISM_Loopback) && (oi->type != OSPF_IFTYPE_LOOPBACK)
741 && (oi->type != OSPF_IFTYPE_VIRTUALLINK)
742 && (OSPF_IF_PASSIVE_STATUS(oi) == OSPF_IF_ACTIVE)) {
743 /* The interface should belong to the OSPF-all-routers group. */
744 if (!OI_MEMBER_CHECK(oi, MEMBER_ALLROUTERS)
745 && (ospf_if_add_allspfrouters(oi->ospf, oi->address,
746 oi->ifp->ifindex)
747 >= 0))
748 /* Set the flag only if the system call to join
749 * succeeded. */
750 OI_MEMBER_JOINED(oi, MEMBER_ALLROUTERS);
751 } else {
752 /* The interface should NOT belong to the OSPF-all-routers
753 * group. */
754 if (OI_MEMBER_CHECK(oi, MEMBER_ALLROUTERS)) {
755 /* Only actually drop if this is the last reference */
756 if (OI_MEMBER_COUNT(oi, MEMBER_ALLROUTERS) == 1)
757 ospf_if_drop_allspfrouters(oi->ospf,
758 oi->address,
759 oi->ifp->ifindex);
760 /* Unset the flag regardless of whether the system call
761 to leave
762 the group succeeded, since it's much safer to assume
763 that
764 we are not a member. */
765 OI_MEMBER_LEFT(oi, MEMBER_ALLROUTERS);
766 }
767 }
768
769 if (((oi->type == OSPF_IFTYPE_BROADCAST)
770 || (oi->type == OSPF_IFTYPE_POINTOPOINT))
771 && ((oi->state == ISM_DR) || (oi->state == ISM_Backup))
772 && (OSPF_IF_PASSIVE_STATUS(oi) == OSPF_IF_ACTIVE)) {
773 /* The interface should belong to the OSPF-designated-routers
774 * group. */
775 if (!OI_MEMBER_CHECK(oi, MEMBER_DROUTERS)
776 && (ospf_if_add_alldrouters(oi->ospf, oi->address,
777 oi->ifp->ifindex)
778 >= 0))
779 /* Set the flag only if the system call to join
780 * succeeded. */
781 OI_MEMBER_JOINED(oi, MEMBER_DROUTERS);
782 } else {
783 /* The interface should NOT belong to the
784 * OSPF-designated-routers group */
785 if (OI_MEMBER_CHECK(oi, MEMBER_DROUTERS)) {
786 /* drop only if last reference */
787 if (OI_MEMBER_COUNT(oi, MEMBER_DROUTERS) == 1)
788 ospf_if_drop_alldrouters(oi->ospf, oi->address,
789 oi->ifp->ifindex);
790
791 /* Unset the flag regardless of whether the system call
792 to leave
793 the group succeeded, since it's much safer to assume
794 that
795 we are not a member. */
796 OI_MEMBER_LEFT(oi, MEMBER_DROUTERS);
797 }
798 }
799 }
800
801 int ospf_if_up(struct ospf_interface *oi)
802 {
803 if (oi == NULL)
804 return 0;
805
806 if (oi->type == OSPF_IFTYPE_LOOPBACK)
807 OSPF_ISM_EVENT_SCHEDULE(oi, ISM_LoopInd);
808 else {
809 OSPF_ISM_EVENT_SCHEDULE(oi, ISM_InterfaceUp);
810 }
811
812 return 1;
813 }
814
815 int ospf_if_down(struct ospf_interface *oi)
816 {
817 struct ospf *ospf;
818 struct route_node *rn;
819 struct ospf_route *or;
820 struct listnode *nh;
821 struct ospf_path *op;
822
823 if (oi == NULL)
824 return 0;
825
826 ospf = oi->ospf;
827
828 /* Cease the HELPER role for all the neighbours
829 * of this interface.
830 */
831 if (ospf->is_helper_supported) {
832 struct route_node *rn = NULL;
833
834 if (ospf_interface_neighbor_count(oi)) {
835 for (rn = route_top(oi->nbrs); rn;
836 rn = route_next(rn)) {
837 struct ospf_neighbor *nbr = NULL;
838
839 if (!rn->info)
840 continue;
841
842 nbr = rn->info;
843
844 if (OSPF_GR_IS_ACTIVE_HELPER(nbr))
845 ospf_gr_helper_exit(
846 nbr, OSPF_GR_HELPER_TOPO_CHG);
847 }
848 }
849 }
850
851 OSPF_ISM_EVENT_EXECUTE(oi, ISM_InterfaceDown);
852 /* delete position in router LSA */
853 oi->lsa_pos_beg = 0;
854 oi->lsa_pos_end = 0;
855 /* Shutdown packet reception and sending */
856 ospf_if_stream_unset(oi);
857
858 if (!ospf->new_table)
859 return 1;
860 for (rn = route_top(ospf->new_table); rn; rn = route_next(rn)) {
861 or = rn->info;
862
863 if (!or)
864 continue;
865
866 for (nh = listhead(or->paths); nh;
867 nh = listnextnode_unchecked(nh)) {
868 op = listgetdata(nh);
869 if (op->ifindex == oi->ifp->ifindex) {
870 or->changed = true;
871 break;
872 }
873 }
874 }
875
876 return 1;
877 }
878
879
880 /* Virtual Link related functions. */
881
882 struct ospf_vl_data *ospf_vl_data_new(struct ospf_area *area,
883 struct in_addr vl_peer)
884 {
885 struct ospf_vl_data *vl_data;
886
887 vl_data = XCALLOC(MTYPE_OSPF_VL_DATA, sizeof(struct ospf_vl_data));
888
889 vl_data->vl_peer.s_addr = vl_peer.s_addr;
890 vl_data->vl_area_id = area->area_id;
891 vl_data->vl_area_id_fmt = area->area_id_fmt;
892
893 return vl_data;
894 }
895
896 void ospf_vl_data_free(struct ospf_vl_data *vl_data)
897 {
898 XFREE(MTYPE_OSPF_VL_DATA, vl_data);
899 }
900
901 unsigned int vlink_count = 0;
902
903 struct ospf_interface *ospf_vl_new(struct ospf *ospf,
904 struct ospf_vl_data *vl_data)
905 {
906 struct ospf_interface *voi;
907 struct interface *vi;
908 char ifname[INTERFACE_NAMSIZ];
909 struct ospf_area *area;
910 struct in_addr area_id;
911 struct connected *co;
912 struct prefix_ipv4 *p;
913
914 if (IS_DEBUG_OSPF_EVENT)
915 zlog_debug("%s: (%s): Start", __func__, ospf_get_name(ospf));
916 if (vlink_count == OSPF_VL_MAX_COUNT) {
917 if (IS_DEBUG_OSPF_EVENT)
918 zlog_debug(
919 "%s: Alarm: cannot create more than OSPF_MAX_VL_COUNT virtual links",
920 __func__);
921
922 return NULL;
923 }
924
925 if (IS_DEBUG_OSPF_EVENT)
926 zlog_debug("%s: creating pseudo zebra interface vrf id %u",
927 __func__, ospf->vrf_id);
928
929 snprintf(ifname, sizeof(ifname), "VLINK%u", vlink_count);
930 vi = if_get_by_name(ifname, ospf->vrf_id, ospf->name);
931 /*
932 * if_get_by_name sets ZEBRA_INTERFACE_LINKDETECTION
933 * virtual links don't need this.
934 */
935 UNSET_FLAG(vi->status, ZEBRA_INTERFACE_LINKDETECTION);
936 co = connected_new();
937 co->ifp = vi;
938 listnode_add(vi->connected, co);
939
940 p = prefix_ipv4_new();
941 p->family = AF_INET;
942 p->prefix.s_addr = INADDR_ANY;
943 p->prefixlen = 0;
944
945 co->address = (struct prefix *)p;
946
947 voi = ospf_if_new(ospf, vi, co->address);
948 if (voi == NULL) {
949 if (IS_DEBUG_OSPF_EVENT)
950 zlog_debug(
951 "%s: Alarm: OSPF int structure is not created",
952 __func__);
953
954 return NULL;
955 }
956 voi->connected = co;
957 voi->vl_data = vl_data;
958 voi->ifp->mtu = OSPF_VL_MTU;
959 voi->type = OSPF_IFTYPE_VIRTUALLINK;
960
961 vlink_count++;
962 if (IS_DEBUG_OSPF_EVENT)
963 zlog_debug("%s: Created name: %s set if->name to %s", __func__,
964 ifname, vi->name);
965
966 area_id.s_addr = INADDR_ANY;
967 area = ospf_area_get(ospf, area_id);
968 voi->area = area;
969
970 if (IS_DEBUG_OSPF_EVENT)
971 zlog_debug("%s: set associated area to the backbone", __func__);
972
973 /* Add pseudo neighbor. */
974 ospf_nbr_self_reset(voi, voi->ospf->router_id);
975
976 ospf_area_add_if(voi->area, voi);
977
978 if (IS_DEBUG_OSPF_EVENT)
979 zlog_debug("%s: Stop", __func__);
980 return voi;
981 }
982
983 static void ospf_vl_if_delete(struct ospf_vl_data *vl_data)
984 {
985 struct interface *ifp = vl_data->vl_oi->ifp;
986 struct vrf *vrf = ifp->vrf;
987
988 vl_data->vl_oi->address->u.prefix4.s_addr = INADDR_ANY;
989 vl_data->vl_oi->address->prefixlen = 0;
990 ospf_if_free(vl_data->vl_oi);
991 if_delete(&ifp);
992 if (!vrf_is_enabled(vrf))
993 vrf_delete(vrf);
994 vlink_count--;
995 }
996
997 /* for a defined area, count the number of configured vl
998 */
999 int ospf_vl_count(struct ospf *ospf, struct ospf_area *area)
1000 {
1001 int count = 0;
1002 struct ospf_vl_data *vl_data;
1003 struct listnode *node;
1004
1005 for (ALL_LIST_ELEMENTS_RO(ospf->vlinks, node, vl_data)) {
1006 if (area
1007 && !IPV4_ADDR_SAME(&vl_data->vl_area_id, &area->area_id))
1008 continue;
1009 count++;
1010 }
1011 return count;
1012 }
1013
1014 /* Look up vl_data for given peer, optionally qualified to be in the
1015 * specified area. NULL area returns first found..
1016 */
1017 struct ospf_vl_data *ospf_vl_lookup(struct ospf *ospf, struct ospf_area *area,
1018 struct in_addr vl_peer)
1019 {
1020 struct ospf_vl_data *vl_data;
1021 struct listnode *node;
1022
1023 if (IS_DEBUG_OSPF_EVENT) {
1024 zlog_debug("%s: Looking for %pI4", __func__, &vl_peer);
1025 if (area)
1026 zlog_debug("%s: in area %pI4", __func__,
1027 &area->area_id);
1028 }
1029
1030 for (ALL_LIST_ELEMENTS_RO(ospf->vlinks, node, vl_data)) {
1031 if (IS_DEBUG_OSPF_EVENT)
1032 zlog_debug("%s: VL %s, peer %pI4", __func__,
1033 vl_data->vl_oi->ifp->name,
1034 &vl_data->vl_peer);
1035
1036 if (area
1037 && !IPV4_ADDR_SAME(&vl_data->vl_area_id, &area->area_id))
1038 continue;
1039
1040 if (IPV4_ADDR_SAME(&vl_data->vl_peer, &vl_peer))
1041 return vl_data;
1042 }
1043
1044 return NULL;
1045 }
1046
1047 static void ospf_vl_shutdown(struct ospf_vl_data *vl_data)
1048 {
1049 struct ospf_interface *oi;
1050
1051 if ((oi = vl_data->vl_oi) == NULL)
1052 return;
1053
1054 oi->address->u.prefix4.s_addr = INADDR_ANY;
1055 oi->address->prefixlen = 0;
1056
1057 UNSET_FLAG(oi->ifp->flags, IFF_UP);
1058 /* OSPF_ISM_EVENT_SCHEDULE (oi, ISM_InterfaceDown); */
1059 OSPF_ISM_EVENT_EXECUTE(oi, ISM_InterfaceDown);
1060 }
1061
1062 void ospf_vl_add(struct ospf *ospf, struct ospf_vl_data *vl_data)
1063 {
1064 listnode_add(ospf->vlinks, vl_data);
1065 hook_call(ospf_vl_add, vl_data);
1066 }
1067
1068 void ospf_vl_delete(struct ospf *ospf, struct ospf_vl_data *vl_data)
1069 {
1070 ospf_vl_shutdown(vl_data);
1071 ospf_vl_if_delete(vl_data);
1072
1073 hook_call(ospf_vl_delete, vl_data);
1074 listnode_delete(ospf->vlinks, vl_data);
1075
1076 ospf_vl_data_free(vl_data);
1077 }
1078
1079 static int ospf_vl_set_params(struct ospf_area *area,
1080 struct ospf_vl_data *vl_data, struct vertex *v)
1081 {
1082 int changed = 0;
1083 struct ospf_interface *voi;
1084 struct listnode *node;
1085 struct vertex_parent *vp = NULL;
1086 unsigned int i;
1087 struct router_lsa *rl;
1088 struct ospf_interface *oi;
1089
1090 voi = vl_data->vl_oi;
1091
1092 if (voi->output_cost != v->distance) {
1093
1094 voi->output_cost = v->distance;
1095 changed = 1;
1096 }
1097
1098 for (ALL_LIST_ELEMENTS_RO(v->parents, node, vp)) {
1099 vl_data->nexthop.lsa_pos = vp->nexthop->lsa_pos;
1100 vl_data->nexthop.router = vp->nexthop->router;
1101
1102 /*
1103 * Only deal with interface data when the local
1104 * (calculating) node is the SPF root node
1105 */
1106 if (!area->spf_dry_run) {
1107 oi = ospf_if_lookup_by_lsa_pos(
1108 area, vl_data->nexthop.lsa_pos);
1109
1110 if (!IPV4_ADDR_SAME(&voi->address->u.prefix4,
1111 &oi->address->u.prefix4))
1112 changed = 1;
1113
1114 voi->address->u.prefix4 = oi->address->u.prefix4;
1115 voi->address->prefixlen = oi->address->prefixlen;
1116 }
1117
1118 break; /* We take the first interface. */
1119 }
1120
1121 rl = (struct router_lsa *)v->lsa;
1122
1123 /* use SPF determined backlink index in struct vertex
1124 * for virtual link destination address
1125 */
1126 if (vp && vp->backlink >= 0) {
1127 if (!IPV4_ADDR_SAME(&vl_data->peer_addr,
1128 &rl->link[vp->backlink].link_data))
1129 changed = 1;
1130 vl_data->peer_addr = rl->link[vp->backlink].link_data;
1131 } else {
1132 /* This is highly odd, there is no backlink index
1133 * there should be due to the ospf_spf_has_link() check
1134 * in SPF. Lets warn and try pick a link anyway.
1135 */
1136 zlog_info("ospf_vl_set_params: No backlink for %s!",
1137 vl_data->vl_oi->ifp->name);
1138 for (i = 0; i < ntohs(rl->links); i++) {
1139 switch (rl->link[i].type) {
1140 case LSA_LINK_TYPE_VIRTUALLINK:
1141 if (IS_DEBUG_OSPF_EVENT)
1142 zlog_debug(
1143 "found back link through VL");
1144 /* fallthru */
1145 case LSA_LINK_TYPE_TRANSIT:
1146 case LSA_LINK_TYPE_POINTOPOINT:
1147 if (!IPV4_ADDR_SAME(&vl_data->peer_addr,
1148 &rl->link[i].link_data))
1149 changed = 1;
1150 vl_data->peer_addr = rl->link[i].link_data;
1151 }
1152 }
1153 }
1154
1155 if (IS_DEBUG_OSPF_EVENT)
1156 zlog_debug("%s: %s peer address: %pI4, cost: %d,%schanged",
1157 __func__, vl_data->vl_oi->ifp->name,
1158 &vl_data->peer_addr, voi->output_cost,
1159 (changed ? " " : " un"));
1160
1161 return changed;
1162 }
1163
1164
1165 void ospf_vl_up_check(struct ospf_area *area, struct in_addr rid,
1166 struct vertex *v)
1167 {
1168 struct ospf *ospf = area->ospf;
1169 struct listnode *node;
1170 struct ospf_vl_data *vl_data;
1171 struct ospf_interface *oi;
1172
1173 if (IS_DEBUG_OSPF_EVENT) {
1174 zlog_debug("%s: Start", __func__);
1175 zlog_debug("%s: Router ID is %pI4 Area is %pI4", __func__, &rid,
1176 &area->area_id);
1177 }
1178
1179 for (ALL_LIST_ELEMENTS_RO(ospf->vlinks, node, vl_data)) {
1180 if (IS_DEBUG_OSPF_EVENT) {
1181 zlog_debug("%s: considering VL, %s in area %pI4",
1182 __func__, vl_data->vl_oi->ifp->name,
1183 &vl_data->vl_area_id);
1184 zlog_debug("%s: peer ID: %pI4", __func__,
1185 &vl_data->vl_peer);
1186 }
1187
1188 if (IPV4_ADDR_SAME(&vl_data->vl_peer, &rid)
1189 && IPV4_ADDR_SAME(&vl_data->vl_area_id, &area->area_id)) {
1190 oi = vl_data->vl_oi;
1191 SET_FLAG(vl_data->flags, OSPF_VL_FLAG_APPROVED);
1192
1193 if (IS_DEBUG_OSPF_EVENT)
1194 zlog_debug("%s: this VL matched", __func__);
1195
1196 if (oi->state == ISM_Down) {
1197 if (IS_DEBUG_OSPF_EVENT)
1198 zlog_debug(
1199 "%s: VL is down, waking it up",
1200 __func__);
1201 SET_FLAG(oi->ifp->flags, IFF_UP);
1202 OSPF_ISM_EVENT_EXECUTE(oi, ISM_InterfaceUp);
1203 }
1204
1205 if (ospf_vl_set_params(area, vl_data, v)) {
1206 if (IS_DEBUG_OSPF(ism, ISM_EVENTS))
1207 zlog_debug(
1208 "%s: VL cost change, scheduling router lsa refresh",
1209 __func__);
1210 if (ospf->backbone)
1211 ospf_router_lsa_update_area(
1212 ospf->backbone);
1213 else if (IS_DEBUG_OSPF(ism, ISM_EVENTS))
1214 zlog_debug(
1215 "%s: VL cost change, no backbone!",
1216 __func__);
1217 }
1218 }
1219 }
1220 }
1221
1222 void ospf_vl_unapprove(struct ospf *ospf)
1223 {
1224 struct listnode *node;
1225 struct ospf_vl_data *vl_data;
1226
1227 for (ALL_LIST_ELEMENTS_RO(ospf->vlinks, node, vl_data))
1228 UNSET_FLAG(vl_data->flags, OSPF_VL_FLAG_APPROVED);
1229 }
1230
1231 void ospf_vl_shut_unapproved(struct ospf *ospf)
1232 {
1233 struct listnode *node, *nnode;
1234 struct ospf_vl_data *vl_data;
1235
1236 for (ALL_LIST_ELEMENTS(ospf->vlinks, node, nnode, vl_data))
1237 if (!CHECK_FLAG(vl_data->flags, OSPF_VL_FLAG_APPROVED))
1238 ospf_vl_shutdown(vl_data);
1239 }
1240
1241 int ospf_full_virtual_nbrs(struct ospf_area *area)
1242 {
1243 if (IS_DEBUG_OSPF_EVENT) {
1244 zlog_debug(
1245 "counting fully adjacent virtual neighbors in area %pI4",
1246 &area->area_id);
1247 zlog_debug("there are %d of them", area->full_vls);
1248 }
1249
1250 return area->full_vls;
1251 }
1252
1253 int ospf_vls_in_area(struct ospf_area *area)
1254 {
1255 struct listnode *node;
1256 struct ospf_vl_data *vl_data;
1257 int c = 0;
1258
1259 for (ALL_LIST_ELEMENTS_RO(area->ospf->vlinks, node, vl_data))
1260 if (IPV4_ADDR_SAME(&vl_data->vl_area_id, &area->area_id))
1261 c++;
1262
1263 return c;
1264 }
1265
1266
1267 struct crypt_key *ospf_crypt_key_new(void)
1268 {
1269 return XCALLOC(MTYPE_OSPF_CRYPT_KEY, sizeof(struct crypt_key));
1270 }
1271
1272 void ospf_crypt_key_add(struct list *crypt, struct crypt_key *ck)
1273 {
1274 listnode_add(crypt, ck);
1275 }
1276
1277 struct crypt_key *ospf_crypt_key_lookup(struct list *auth_crypt, uint8_t key_id)
1278 {
1279 struct listnode *node;
1280 struct crypt_key *ck;
1281
1282 for (ALL_LIST_ELEMENTS_RO(auth_crypt, node, ck))
1283 if (ck->key_id == key_id)
1284 return ck;
1285
1286 return NULL;
1287 }
1288
1289 int ospf_crypt_key_delete(struct list *auth_crypt, uint8_t key_id)
1290 {
1291 struct listnode *node, *nnode;
1292 struct crypt_key *ck;
1293
1294 for (ALL_LIST_ELEMENTS(auth_crypt, node, nnode, ck)) {
1295 if (ck->key_id == key_id) {
1296 listnode_delete(auth_crypt, ck);
1297 XFREE(MTYPE_OSPF_CRYPT_KEY, ck);
1298 return 1;
1299 }
1300 }
1301
1302 return 0;
1303 }
1304
1305 uint8_t ospf_default_iftype(struct interface *ifp)
1306 {
1307 if (if_is_pointopoint(ifp))
1308 return OSPF_IFTYPE_POINTOPOINT;
1309 else if (if_is_loopback(ifp))
1310 return OSPF_IFTYPE_LOOPBACK;
1311 else
1312 return OSPF_IFTYPE_BROADCAST;
1313 }
1314
1315 void ospf_if_interface(struct interface *ifp)
1316 {
1317 hook_call(ospf_if_update, ifp);
1318 }
1319
1320 uint32_t ospf_if_count_area_params(struct interface *ifp)
1321 {
1322 struct ospf_if_params *params;
1323 struct route_node *rn;
1324 uint32_t count = 0;
1325
1326 params = IF_DEF_PARAMS(ifp);
1327 if (OSPF_IF_PARAM_CONFIGURED(params, if_area))
1328 count++;
1329
1330 for (rn = route_top(IF_OIFS_PARAMS(ifp)); rn; rn = route_next(rn))
1331 if ((params = rn->info)
1332 && OSPF_IF_PARAM_CONFIGURED(params, if_area))
1333 count++;
1334
1335 return count;
1336 }
1337
1338 static int ospf_ifp_create(struct interface *ifp)
1339 {
1340 struct ospf *ospf = NULL;
1341 struct ospf_if_info *oii;
1342
1343 if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE))
1344 zlog_debug(
1345 "Zebra: interface add %s vrf %s[%u] index %d flags %llx metric %d mtu %d speed %u",
1346 ifp->name, ifp->vrf->name, ifp->vrf->vrf_id,
1347 ifp->ifindex, (unsigned long long)ifp->flags,
1348 ifp->metric, ifp->mtu, ifp->speed);
1349
1350 assert(ifp->info);
1351
1352 oii = ifp->info;
1353 oii->curr_mtu = ifp->mtu;
1354
1355 if (IF_DEF_PARAMS(ifp)
1356 && !OSPF_IF_PARAM_CONFIGURED(IF_DEF_PARAMS(ifp), type)) {
1357 SET_IF_PARAM(IF_DEF_PARAMS(ifp), type);
1358 IF_DEF_PARAMS(ifp)->type = ospf_default_iftype(ifp);
1359 }
1360
1361 ospf = ifp->vrf->info;
1362 if (!ospf)
1363 return 0;
1364
1365 if (ospf_if_count_area_params(ifp) > 0)
1366 ospf_interface_area_set(ospf, ifp);
1367
1368 ospf_if_recalculate_output_cost(ifp);
1369
1370 ospf_if_update(ospf, ifp);
1371
1372 if (HAS_LINK_PARAMS(ifp))
1373 ospf_mpls_te_update_if(ifp);
1374
1375 hook_call(ospf_if_update, ifp);
1376
1377 return 0;
1378 }
1379
1380 static int ospf_ifp_up(struct interface *ifp)
1381 {
1382 struct ospf_interface *oi;
1383 struct route_node *rn;
1384 struct ospf_if_info *oii = ifp->info;
1385
1386 ospf_if_recalculate_output_cost(ifp);
1387
1388 if (oii && oii->curr_mtu != ifp->mtu) {
1389 if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE))
1390 zlog_debug(
1391 "Zebra: Interface[%s] MTU change %u -> %u.",
1392 ifp->name, oii->curr_mtu, ifp->mtu);
1393
1394 oii->curr_mtu = ifp->mtu;
1395 /* Must reset the interface (simulate down/up) when MTU
1396 * changes. */
1397 ospf_if_reset(ifp);
1398
1399 return 0;
1400 }
1401
1402 if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE))
1403 zlog_debug("Zebra: Interface[%s] state change to up.",
1404 ifp->name);
1405
1406 for (rn = route_top(IF_OIFS(ifp)); rn; rn = route_next(rn)) {
1407 if ((oi = rn->info) == NULL)
1408 continue;
1409
1410 ospf_if_up(oi);
1411 }
1412
1413 if (HAS_LINK_PARAMS(ifp))
1414 ospf_mpls_te_update_if(ifp);
1415
1416 return 0;
1417 }
1418
1419 static int ospf_ifp_down(struct interface *ifp)
1420 {
1421 struct ospf_interface *oi;
1422 struct route_node *node;
1423
1424 if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE))
1425 zlog_debug("Zebra: Interface[%s] state change to down.",
1426 ifp->name);
1427
1428 for (node = route_top(IF_OIFS(ifp)); node; node = route_next(node)) {
1429 if ((oi = node->info) == NULL)
1430 continue;
1431 ospf_if_down(oi);
1432 }
1433
1434 return 0;
1435 }
1436
1437 static int ospf_ifp_destroy(struct interface *ifp)
1438 {
1439 struct ospf *ospf;
1440 struct route_node *rn;
1441
1442 if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE))
1443 zlog_debug(
1444 "Zebra: interface delete %s vrf %s[%u] index %d flags %llx metric %d mtu %d",
1445 ifp->name, ifp->vrf->name, ifp->vrf->vrf_id,
1446 ifp->ifindex, (unsigned long long)ifp->flags,
1447 ifp->metric, ifp->mtu);
1448
1449 hook_call(ospf_if_delete, ifp);
1450
1451 ospf = ifp->vrf->info;
1452 if (ospf) {
1453 if (ospf_if_count_area_params(ifp) > 0)
1454 ospf_interface_area_unset(ospf, ifp);
1455 }
1456
1457 for (rn = route_top(IF_OIFS(ifp)); rn; rn = route_next(rn))
1458 if (rn->info)
1459 ospf_if_free((struct ospf_interface *)rn->info);
1460
1461 return 0;
1462 }
1463
1464 /* Resetting ospf hello timer */
1465 void ospf_reset_hello_timer(struct interface *ifp, struct in_addr addr,
1466 bool is_addr)
1467 {
1468 struct route_node *rn;
1469
1470 if (is_addr) {
1471 struct prefix p;
1472 struct ospf_interface *oi = NULL;
1473
1474 p.u.prefix4 = addr;
1475 p.family = AF_INET;
1476 p.prefixlen = IPV4_MAX_BITLEN;
1477
1478 oi = ospf_if_table_lookup(ifp, &p);
1479
1480 if (oi) {
1481 /* Send hello before restart the hello timer
1482 * to avoid session flaps in case of bigger
1483 * hello interval configurations.
1484 */
1485 ospf_hello_send(oi);
1486
1487 /* Restart hello timer for this interface */
1488 THREAD_OFF(oi->t_hello);
1489 OSPF_HELLO_TIMER_ON(oi);
1490 }
1491
1492 return;
1493 }
1494
1495 for (rn = route_top(IF_OIFS(ifp)); rn; rn = route_next(rn)) {
1496 struct ospf_interface *oi = rn->info;
1497
1498 if (!oi)
1499 continue;
1500
1501 /* If hello interval configured on this oi, don't restart. */
1502 if (OSPF_IF_PARAM_CONFIGURED(oi->params, v_hello))
1503 continue;
1504
1505 /* Send hello before restart the hello timer
1506 * to avoid session flaps in case of bigger
1507 * hello interval configurations.
1508 */
1509 ospf_hello_send(oi);
1510
1511 /* Restart the hello timer. */
1512 THREAD_OFF(oi->t_hello);
1513 OSPF_HELLO_TIMER_ON(oi);
1514 }
1515 }
1516
1517 void ospf_if_init(void)
1518 {
1519 if_zapi_callbacks(ospf_ifp_create, ospf_ifp_up,
1520 ospf_ifp_down, ospf_ifp_destroy);
1521
1522 /* Initialize Zebra interface data structure. */
1523 hook_register_prio(if_add, 0, ospf_if_new_hook);
1524 hook_register_prio(if_del, 0, ospf_if_delete_hook);
1525 }