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