]> git.proxmox.com Git - mirror_frr.git/blame - ospfd/ospf_interface.c
ospfd, ospf6d: introduce the "graceful-restart hello-delay" command
[mirror_frr.git] / ospfd / ospf_interface.c
CommitLineData
acddc0ed 1// SPDX-License-Identifier: GPL-2.0-or-later
718e3744 2/*
3 * OSPF Interface functions.
4 * Copyright (C) 1999, 2000 Toshiaki Takada
718e3744 5 */
6
7#include <zebra.h>
8
24a58196 9#include "frrevent.h"
718e3744 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"
fce7f209 18#include "network.h"
c0325cfa 19#include "zclient.h"
7f342629 20#include "bfd.h"
132a782e 21#include "ldp_sync.h"
718e3744 22
68980084 23#include "ospfd/ospfd.h"
659f4e40 24#include "ospfd/ospf_bfd.h"
718e3744 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"
718e3744 35#include "ospfd/ospf_network.h"
36#include "ospfd/ospf_dump.h"
132a782e 37#include "ospfd/ospf_ldp_sync.h"
effee187 38#include "ospfd/ospf_route.h"
0b4124c1 39#include "ospfd/ospf_te.h"
718e3744 40
96244aca 41DEFINE_QOBJ_TYPE(ospf_interface);
8451921b
DL
42DEFINE_HOOK(ospf_vl_add, (struct ospf_vl_data * vd), (vd));
43DEFINE_HOOK(ospf_vl_delete, (struct ospf_vl_data * vd), (vd));
44DEFINE_HOOK(ospf_if_update, (struct interface * ifp), (ifp));
45DEFINE_HOOK(ospf_if_delete, (struct interface * ifp), (ifp));
6b0655a2 46
2bc7673f
CS
47int 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;
fe61ceae
DS
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++;
2bc7673f
CS
65 }
66
67 return count;
68}
69
d62a17ae 70int ospf_if_get_output_cost(struct ospf_interface *oi)
718e3744 71{
d62a17ae 72 /* If all else fails, use default OSPF cost */
d7c0a89a
QY
73 uint32_t cost;
74 uint32_t bw, refbw;
d62a17ae 75
132a782e 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
d62a17ae 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
bd4f51b1 92 /* A specified ip ospf cost overrides a calculated one. */
d62a17ae 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 {
d7c0a89a 99 cost = (uint32_t)((double)refbw / (double)bw + (double)0.5);
d62a17ae 100 if (cost < 1)
101 cost = 1;
102 else if (cost > 65535)
103 cost = 65535;
104 }
105
106 return cost;
718e3744 107}
108
d62a17ae 109void ospf_if_recalculate_output_cost(struct interface *ifp)
718e3744 110{
d7c0a89a 111 uint32_t newcost;
d62a17ae 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 }
718e3744 127 }
718e3744 128}
129
d62a17ae 130/* Simulate down/up on the interface. This is needed, for example, when
a608bbf2 131 the MTU changes. */
d62a17ae 132void ospf_if_reset(struct interface *ifp)
a608bbf2 133{
d62a17ae 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 }
a608bbf2 145}
146
d62a17ae 147void ospf_if_reset_variables(struct ospf_interface *oi)
718e3744 148{
d62a17ae 149 /* Set default values. */
150 /* don't clear this flag. oi->flag = OSPF_IF_DISABLE; */
718e3744 151
d62a17ae 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;
718e3744 158
d62a17ae 159 oi->state = ISM_Down;
718e3744 160
d62a17ae 161 oi->crypt_seqnum = 0;
718e3744 162
d62a17ae 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;
718e3744 167}
168
20916fba 169/* lookup oi for specified prefix/ifp */
d62a17ae 170struct ospf_interface *ospf_if_table_lookup(struct interface *ifp,
171 struct prefix *prefix)
20916fba 172{
d62a17ae 173 struct prefix p;
174 struct route_node *rn;
175 struct ospf_interface *rninfo = NULL;
176
177 p = *prefix;
936fbaef 178 p.prefixlen = IPV4_MAX_BITLEN;
d62a17ae 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;
20916fba 187}
188
d62a17ae 189static void ospf_add_to_if(struct interface *ifp, struct ospf_interface *oi)
718e3744 190{
d62a17ae 191 struct route_node *rn;
192 struct prefix p;
193
194 p = *oi->address;
936fbaef 195 p.prefixlen = IPV4_MAX_BITLEN;
a6435618 196 apply_mask(&p);
d62a17ae 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;
718e3744 204}
205
d62a17ae 206static void ospf_delete_from_if(struct interface *ifp,
207 struct ospf_interface *oi)
718e3744 208{
d62a17ae 209 struct route_node *rn;
210 struct prefix p;
211
212 p = *oi->address;
936fbaef 213 p.prefixlen = IPV4_MAX_BITLEN;
d62a17ae 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);
718e3744 221}
222
d62a17ae 223struct ospf_interface *ospf_if_new(struct ospf *ospf, struct interface *ifp,
224 struct prefix *p)
718e3744 225{
d62a17ae 226 struct ospf_interface *oi;
227
36a106e0
DS
228 oi = ospf_if_table_lookup(ifp, p);
229 if (oi)
d62a17ae 230 return oi;
231
36a106e0
DS
232 oi = XCALLOC(MTYPE_OSPF_IF, sizeof(struct ospf_interface));
233
234 oi->obuf = ospf_fifo_new();
235
d62a17ae 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
fce7f209 263 oi->crypt_seqnum = frr_sequence32_next();
d62a17ae 264
265 ospf_opaque_type9_lsa_init(oi);
266
267 oi->ospf = ospf;
4fc8a852 268
d62a17ae 269 QOBJ_REG(oi, ospf_interface);
270
b5a8894d
CS
271 if (IS_DEBUG_OSPF_EVENT)
272 zlog_debug("%s: ospf interface %s vrf %s id %u created",
15569c58
DA
273 __func__, ifp->name, ospf_get_name(ospf),
274 ospf->vrf_id);
b5a8894d 275
d62a17ae 276 return oi;
718e3744 277}
278
279/* Restore an interface to its pre UP state
280 Used from ism_interface_down only */
d62a17ae 281void ospf_if_cleanup(struct ospf_interface *oi)
718e3744 282{
d62a17ae 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)) {
e16d030c 292 EVENT_OFF(nbr_nbma->t_poll);
d62a17ae 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);
718e3744 302 }
303
d62a17ae 304 /* send Neighbor event KillNbr to all associated neighbors. */
fe61ceae 305 for (rn = route_top(oi->nbrs); rn; rn = route_next(rn)) {
d62a17ae 306 if ((nbr = rn->info) != NULL)
307 if (nbr != oi->nbr_self)
308 OSPF_NSM_EVENT_EXECUTE(nbr, NSM_KillNbr);
fe61ceae 309 }
d62a17ae 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);
718e3744 323}
324
d62a17ae 325void ospf_if_free(struct ospf_interface *oi)
718e3744 326{
d62a17ae 327 ospf_if_down(oi);
328
36a106e0 329 ospf_fifo_free(oi->obuf);
4fc8a852 330
d62a17ae 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);
718e3744 339
d62a17ae 340 route_table_finish(oi->nbrs);
341 route_table_finish(oi->ls_upd_queue);
718e3744 342
d62a17ae 343 /* Free any lists that should be freed */
6a154c88 344 list_delete(&oi->nbr_nbma);
718e3744 345
6a154c88
DL
346 list_delete(&oi->ls_ack);
347 list_delete(&oi->ls_ack_direct.ls_ack);
ae19c240 348
b5a8894d
CS
349 if (IS_DEBUG_OSPF_EVENT)
350 zlog_debug("%s: ospf interface %s vrf %s id %u deleted",
096f7609
IR
351 __func__, oi->ifp->name, oi->ifp->vrf->name,
352 oi->ifp->vrf->vrf_id);
b5a8894d 353
d62a17ae 354 ospf_delete_from_if(oi->ifp, oi);
718e3744 355
d62a17ae 356 listnode_delete(oi->ospf->oiflist, oi);
357 listnode_delete(oi->area->oiflist, oi);
718e3744 358
332beb64 359 event_cancel_event(master, oi);
cfd670f3 360
d62a17ae 361 memset(oi, 0, sizeof(*oi));
362 XFREE(MTYPE_OSPF_IF, oi);
718e3744 363}
364
d62a17ae 365int ospf_if_is_up(struct ospf_interface *oi)
718e3744 366{
d62a17ae 367 return if_is_up(oi->ifp);
718e3744 368}
369
d62a17ae 370struct ospf_interface *ospf_if_exists(struct ospf_interface *oic)
371{
372 struct listnode *node;
373 struct ospf *ospf;
374 struct ospf_interface *oi;
2db3d05d 375
b5a8894d
CS
376 if (!oic)
377 return NULL;
378
379 ospf = oic->ospf;
380 if (ospf == NULL)
d62a17ae 381 return NULL;
2db3d05d 382
d62a17ae 383 for (ALL_LIST_ELEMENTS_RO(ospf->oiflist, node, oi))
384 if (oi == oic)
385 return oi;
1eb8ef25 386
d62a17ae 387 return NULL;
2db3d05d 388}
389
c81ee5c9 390/* Lookup OSPF interface by router LSA posistion */
d62a17ae 391struct ospf_interface *ospf_if_lookup_by_lsa_pos(struct ospf_area *area,
392 int lsa_pos)
c81ee5c9 393{
d62a17ae 394 struct listnode *node;
395 struct ospf_interface *oi;
c81ee5c9 396
d62a17ae 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;
c81ee5c9
JT
402}
403
d62a17ae 404struct ospf_interface *ospf_if_lookup_by_local_addr(struct ospf *ospf,
405 struct interface *ifp,
406 struct in_addr address)
718e3744 407{
d62a17ae 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;
718e3744 421}
422
d62a17ae 423struct ospf_interface *ospf_if_lookup_by_prefix(struct ospf *ospf,
424 struct prefix_ipv4 *p)
718e3744 425{
d62a17ae 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 }
68980084 439 }
d62a17ae 440 return NULL;
718e3744 441}
442
05cf46ba 443/* determine receiving interface by ifp and source address */
d62a17ae 444struct ospf_interface *ospf_if_lookup_recv_if(struct ospf *ospf,
445 struct in_addr src,
446 struct interface *ifp)
718e3744 447{
d62a17ae 448 struct route_node *rn;
449 struct prefix_ipv4 addr;
5136e672 450 struct ospf_interface *oi, *match, *unnumbered_match;
d62a17ae 451
452 addr.family = AF_INET;
453 addr.prefix = src;
454 addr.prefixlen = IPV4_MAX_BITLEN;
455
5136e672 456 match = unnumbered_match = NULL;
d62a17ae 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
608c8870 467 if (if_is_loopback(oi->ifp))
d62a17ae 468 continue;
469
470 if (CHECK_FLAG(oi->connected->flags, ZEBRA_IFA_UNNUMBERED))
5136e672 471 unnumbered_match = oi;
d62a17ae 472 else if (prefix_match(CONNECTED_PREFIX(oi->connected),
473 (struct prefix *)&addr)) {
9d303b37
DL
474 if ((match == NULL) || (match->address->prefixlen
475 < oi->address->prefixlen))
d62a17ae 476 match = oi;
477 }
718e3744 478 }
718e3744 479
5136e672
DS
480 if (match)
481 return match;
482
483 return unnumbered_match;
718e3744 484}
6b0655a2 485
fb617d18
DS
486void 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))
e16d030c 495 EVENT_OFF(ospf->t_write);
fb617d18
DS
496 oi->on_write_q = 0;
497 }
498}
499
85c8ef06
CS
500static 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
d62a17ae 509void ospf_if_stream_unset(struct ospf_interface *oi)
718e3744 510{
36a106e0 511 /* flush the interface packet queue */
fb617d18 512 ospf_interface_fifo_flush(oi);
36a106e0
DS
513 /*reset protocol stats */
514 ospf_if_reset_stats(oi);
718e3744 515}
68980084 516
6b0655a2 517
d62a17ae 518static struct ospf_if_params *ospf_new_if_params(void)
718e3744 519{
d62a17ae 520 struct ospf_if_params *oip;
521
522 oip = XCALLOC(MTYPE_OSPF_IF_PARAMS, sizeof(struct ospf_if_params));
523
d62a17ae 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);
0c05ceae 530 UNSET_IF_PARAM(oip, v_gr_hello_delay);
d62a17ae 531 UNSET_IF_PARAM(oip, v_wait);
532 UNSET_IF_PARAM(oip, priority);
533 UNSET_IF_PARAM(oip, type);
534 UNSET_IF_PARAM(oip, auth_simple);
535 UNSET_IF_PARAM(oip, auth_crypt);
536 UNSET_IF_PARAM(oip, auth_type);
bf617538 537 UNSET_IF_PARAM(oip, if_area);
d62a17ae 538
539 oip->auth_crypt = list_new();
540
541 oip->network_lsa_seqnum = htonl(OSPF_INITIAL_SEQUENCE_NUMBER);
182d6bdc 542 oip->is_v_wait_set = false;
d62a17ae 543
bc97889b
AL
544 oip->ptp_dmvpn = 0;
545
d62a17ae 546 return oip;
718e3744 547}
548
659f4e40
RZ
549static void ospf_del_if_params(struct interface *ifp,
550 struct ospf_if_params *oip)
718e3744 551{
6a154c88 552 list_delete(&oip->auth_crypt);
659f4e40 553 ospf_interface_disable_bfd(ifp, oip);
132a782e 554 ldp_sync_info_free(&(oip->ldp_sync_info));
d62a17ae 555 XFREE(MTYPE_OSPF_IF_PARAMS, oip);
718e3744 556}
557
d62a17ae 558void ospf_free_if_params(struct interface *ifp, struct in_addr addr)
718e3744 559{
d62a17ae 560 struct ospf_if_params *oip;
561 struct prefix_ipv4 p;
562 struct route_node *rn;
563
564 p.family = AF_INET;
936fbaef 565 p.prefixlen = IPV4_MAX_BITLEN;
d62a17ae 566 p.prefix = addr;
567 rn = route_node_lookup(IF_OIFS_PARAMS(ifp), (struct prefix *)&p);
568 if (!rn || !rn->info)
569 return;
570
571 oip = rn->info;
572 route_unlock_node(rn);
573
574 if (!OSPF_IF_PARAM_CONFIGURED(oip, output_cost_cmd)
575 && !OSPF_IF_PARAM_CONFIGURED(oip, transmit_delay)
576 && !OSPF_IF_PARAM_CONFIGURED(oip, retransmit_interval)
577 && !OSPF_IF_PARAM_CONFIGURED(oip, passive_interface)
578 && !OSPF_IF_PARAM_CONFIGURED(oip, v_hello)
579 && !OSPF_IF_PARAM_CONFIGURED(oip, fast_hello)
580 && !OSPF_IF_PARAM_CONFIGURED(oip, v_wait)
581 && !OSPF_IF_PARAM_CONFIGURED(oip, priority)
582 && !OSPF_IF_PARAM_CONFIGURED(oip, type)
583 && !OSPF_IF_PARAM_CONFIGURED(oip, auth_simple)
584 && !OSPF_IF_PARAM_CONFIGURED(oip, auth_type)
bf617538 585 && !OSPF_IF_PARAM_CONFIGURED(oip, if_area)
2917d36b 586 && listcount(oip->auth_crypt) == 0) {
659f4e40 587 ospf_del_if_params(ifp, oip);
d62a17ae 588 rn->info = NULL;
589 route_unlock_node(rn);
590 }
718e3744 591}
592
d62a17ae 593struct ospf_if_params *ospf_lookup_if_params(struct interface *ifp,
594 struct in_addr addr)
718e3744 595{
d62a17ae 596 struct prefix_ipv4 p;
597 struct route_node *rn;
598
599 p.family = AF_INET;
936fbaef 600 p.prefixlen = IPV4_MAX_BITLEN;
d62a17ae 601 p.prefix = addr;
602
603 rn = route_node_lookup(IF_OIFS_PARAMS(ifp), (struct prefix *)&p);
604
605 if (rn) {
606 route_unlock_node(rn);
607 return rn->info;
608 }
609
610 return NULL;
718e3744 611}
612
d62a17ae 613struct ospf_if_params *ospf_get_if_params(struct interface *ifp,
614 struct in_addr addr)
718e3744 615{
d62a17ae 616 struct prefix_ipv4 p;
617 struct route_node *rn;
618
619 p.family = AF_INET;
936fbaef 620 p.prefixlen = IPV4_MAX_BITLEN;
d62a17ae 621 p.prefix = addr;
a6435618 622 apply_mask_ipv4(&p);
d62a17ae 623
624 rn = route_node_get(IF_OIFS_PARAMS(ifp), (struct prefix *)&p);
625
626 if (rn->info == NULL)
627 rn->info = ospf_new_if_params();
628 else
629 route_unlock_node(rn);
630
631 return rn->info;
718e3744 632}
633
d62a17ae 634void ospf_if_update_params(struct interface *ifp, struct in_addr addr)
718e3744 635{
d62a17ae 636 struct route_node *rn;
637 struct ospf_interface *oi;
638
639 for (rn = route_top(IF_OIFS(ifp)); rn; rn = route_next(rn)) {
640 if ((oi = rn->info) == NULL)
641 continue;
642
643 if (IPV4_ADDR_SAME(&oi->address->u.prefix4, &addr))
644 oi->params = ospf_lookup_if_params(
645 ifp, oi->address->u.prefix4);
646 }
718e3744 647}
648
d62a17ae 649int ospf_if_new_hook(struct interface *ifp)
718e3744 650{
d62a17ae 651 int rc = 0;
652
653 ifp->info = XCALLOC(MTYPE_OSPF_IF_INFO, sizeof(struct ospf_if_info));
654
04a0401f
MS
655 IF_OSPF_IF_INFO(ifp)->oii_fd = -1;
656
d62a17ae 657 IF_OIFS(ifp) = route_table_init();
658 IF_OIFS_PARAMS(ifp) = route_table_init();
659
660 IF_DEF_PARAMS(ifp) = ospf_new_if_params();
661
662 SET_IF_PARAM(IF_DEF_PARAMS(ifp), transmit_delay);
663 IF_DEF_PARAMS(ifp)->transmit_delay = OSPF_TRANSMIT_DELAY_DEFAULT;
664
665 SET_IF_PARAM(IF_DEF_PARAMS(ifp), retransmit_interval);
666 IF_DEF_PARAMS(ifp)->retransmit_interval =
667 OSPF_RETRANSMIT_INTERVAL_DEFAULT;
668
669 SET_IF_PARAM(IF_DEF_PARAMS(ifp), priority);
670 IF_DEF_PARAMS(ifp)->priority = OSPF_ROUTER_PRIORITY_DEFAULT;
671
672 IF_DEF_PARAMS(ifp)->mtu_ignore = OSPF_MTU_IGNORE_DEFAULT;
673
674 SET_IF_PARAM(IF_DEF_PARAMS(ifp), v_hello);
675 IF_DEF_PARAMS(ifp)->v_hello = OSPF_HELLO_INTERVAL_DEFAULT;
676
677 SET_IF_PARAM(IF_DEF_PARAMS(ifp), fast_hello);
678 IF_DEF_PARAMS(ifp)->fast_hello = OSPF_FAST_HELLO_DEFAULT;
679
0c05ceae
RW
680 SET_IF_PARAM(IF_DEF_PARAMS(ifp), v_gr_hello_delay);
681 IF_DEF_PARAMS(ifp)->v_gr_hello_delay = OSPF_HELLO_DELAY_DEFAULT;
682
d62a17ae 683 SET_IF_PARAM(IF_DEF_PARAMS(ifp), v_wait);
684 IF_DEF_PARAMS(ifp)->v_wait = OSPF_ROUTER_DEAD_INTERVAL_DEFAULT;
685
686 SET_IF_PARAM(IF_DEF_PARAMS(ifp), auth_simple);
687 memset(IF_DEF_PARAMS(ifp)->auth_simple, 0, OSPF_AUTH_SIMPLE_SIZE);
688
689 SET_IF_PARAM(IF_DEF_PARAMS(ifp), auth_type);
690 IF_DEF_PARAMS(ifp)->auth_type = OSPF_AUTH_NOTSET;
691
692 rc = ospf_opaque_new_if(ifp);
693 return rc;
718e3744 694}
695
d62a17ae 696static int ospf_if_delete_hook(struct interface *ifp)
718e3744 697{
d62a17ae 698 int rc = 0;
699 struct route_node *rn;
04a0401f
MS
700 struct ospf_if_info *oii;
701
d62a17ae 702 rc = ospf_opaque_del_if(ifp);
940b01aa 703
e2134a79
RZ
704 /*
705 * This function must be called before `route_table_finish` due to
706 * BFD integration need to iterate over the interface neighbors to
707 * remove all registrations.
708 */
709 ospf_del_if_params(ifp, IF_DEF_PARAMS(ifp));
710
d62a17ae 711 for (rn = route_top(IF_OIFS_PARAMS(ifp)); rn; rn = route_next(rn))
712 if (rn->info)
659f4e40 713 ospf_del_if_params(ifp, rn->info);
9ffde6e1
DS
714
715 route_table_finish(IF_OIFS(ifp));
d62a17ae 716 route_table_finish(IF_OIFS_PARAMS(ifp));
940b01aa 717
04a0401f
MS
718 /* Close per-interface socket */
719 oii = ifp->info;
720 if (oii && oii->oii_fd > 0) {
721 close(oii->oii_fd);
722 oii->oii_fd = -1;
723 }
724
d62a17ae 725 XFREE(MTYPE_OSPF_IF_INFO, ifp->info);
718e3744 726
d62a17ae 727 return rc;
718e3744 728}
729
d62a17ae 730int ospf_if_is_enable(struct ospf_interface *oi)
718e3744 731{
608c8870 732 if (!(if_is_loopback(oi->ifp)))
d62a17ae 733 if (if_is_up(oi->ifp))
734 return 1;
718e3744 735
d62a17ae 736 return 0;
718e3744 737}
738
d62a17ae 739void ospf_if_set_multicast(struct ospf_interface *oi)
ba6454ec 740{
d62a17ae 741 if ((oi->state > ISM_Loopback) && (oi->type != OSPF_IFTYPE_LOOPBACK)
742 && (oi->type != OSPF_IFTYPE_VIRTUALLINK)
743 && (OSPF_IF_PASSIVE_STATUS(oi) == OSPF_IF_ACTIVE)) {
744 /* The interface should belong to the OSPF-all-routers group. */
745 if (!OI_MEMBER_CHECK(oi, MEMBER_ALLROUTERS)
746 && (ospf_if_add_allspfrouters(oi->ospf, oi->address,
747 oi->ifp->ifindex)
748 >= 0))
749 /* Set the flag only if the system call to join
750 * succeeded. */
751 OI_MEMBER_JOINED(oi, MEMBER_ALLROUTERS);
752 } else {
753 /* The interface should NOT belong to the OSPF-all-routers
754 * group. */
755 if (OI_MEMBER_CHECK(oi, MEMBER_ALLROUTERS)) {
756 /* Only actually drop if this is the last reference */
757 if (OI_MEMBER_COUNT(oi, MEMBER_ALLROUTERS) == 1)
758 ospf_if_drop_allspfrouters(oi->ospf,
759 oi->address,
760 oi->ifp->ifindex);
761 /* Unset the flag regardless of whether the system call
762 to leave
763 the group succeeded, since it's much safer to assume
764 that
765 we are not a member. */
766 OI_MEMBER_LEFT(oi, MEMBER_ALLROUTERS);
767 }
768 }
769
770 if (((oi->type == OSPF_IFTYPE_BROADCAST)
771 || (oi->type == OSPF_IFTYPE_POINTOPOINT))
772 && ((oi->state == ISM_DR) || (oi->state == ISM_Backup))
773 && (OSPF_IF_PASSIVE_STATUS(oi) == OSPF_IF_ACTIVE)) {
774 /* The interface should belong to the OSPF-designated-routers
775 * group. */
776 if (!OI_MEMBER_CHECK(oi, MEMBER_DROUTERS)
777 && (ospf_if_add_alldrouters(oi->ospf, oi->address,
778 oi->ifp->ifindex)
779 >= 0))
780 /* Set the flag only if the system call to join
781 * succeeded. */
782 OI_MEMBER_JOINED(oi, MEMBER_DROUTERS);
783 } else {
784 /* The interface should NOT belong to the
785 * OSPF-designated-routers group */
786 if (OI_MEMBER_CHECK(oi, MEMBER_DROUTERS)) {
787 /* drop only if last reference */
788 if (OI_MEMBER_COUNT(oi, MEMBER_DROUTERS) == 1)
789 ospf_if_drop_alldrouters(oi->ospf, oi->address,
790 oi->ifp->ifindex);
791
792 /* Unset the flag regardless of whether the system call
793 to leave
794 the group succeeded, since it's much safer to assume
795 that
796 we are not a member. */
797 OI_MEMBER_LEFT(oi, MEMBER_DROUTERS);
798 }
799 }
ba6454ec 800}
801
d62a17ae 802int ospf_if_up(struct ospf_interface *oi)
718e3744 803{
d62a17ae 804 if (oi == NULL)
805 return 0;
806
807 if (oi->type == OSPF_IFTYPE_LOOPBACK)
808 OSPF_ISM_EVENT_SCHEDULE(oi, ISM_LoopInd);
809 else {
d62a17ae 810 OSPF_ISM_EVENT_SCHEDULE(oi, ISM_InterfaceUp);
811 }
812
813 return 1;
718e3744 814}
815
d62a17ae 816int ospf_if_down(struct ospf_interface *oi)
718e3744 817{
df074ec3 818 struct ospf *ospf;
effee187
DS
819 struct route_node *rn;
820 struct ospf_route *or;
821 struct listnode *nh;
822 struct ospf_path *op;
df074ec3 823
d62a17ae 824 if (oi == NULL)
825 return 0;
718e3744 826
df074ec3 827 ospf = oi->ospf;
828
829 /* Cease the HELPER role for all the neighbours
830 * of this interface.
831 */
832 if (ospf->is_helper_supported) {
833 struct route_node *rn = NULL;
834
835 if (ospf_interface_neighbor_count(oi)) {
836 for (rn = route_top(oi->nbrs); rn;
837 rn = route_next(rn)) {
838 struct ospf_neighbor *nbr = NULL;
839
840 if (!rn->info)
841 continue;
842
843 nbr = rn->info;
844
845 if (OSPF_GR_IS_ACTIVE_HELPER(nbr))
846 ospf_gr_helper_exit(
847 nbr, OSPF_GR_HELPER_TOPO_CHG);
848 }
849 }
850 }
851
d62a17ae 852 OSPF_ISM_EVENT_EXECUTE(oi, ISM_InterfaceDown);
853 /* delete position in router LSA */
854 oi->lsa_pos_beg = 0;
855 oi->lsa_pos_end = 0;
856 /* Shutdown packet reception and sending */
857 ospf_if_stream_unset(oi);
718e3744 858
d4e66f14
TS
859 if (!ospf->new_table)
860 return 1;
effee187
DS
861 for (rn = route_top(ospf->new_table); rn; rn = route_next(rn)) {
862 or = rn->info;
863
864 if (!or)
865 continue;
866
867 for (nh = listhead(or->paths); nh;
868 nh = listnextnode_unchecked(nh)) {
869 op = listgetdata(nh);
870 if (op->ifindex == oi->ifp->ifindex) {
871 or->changed = true;
872 break;
873 }
874 }
875 }
876
d62a17ae 877 return 1;
718e3744 878}
879
6b0655a2 880
718e3744 881/* Virtual Link related functions. */
882
d62a17ae 883struct ospf_vl_data *ospf_vl_data_new(struct ospf_area *area,
884 struct in_addr vl_peer)
718e3744 885{
d62a17ae 886 struct ospf_vl_data *vl_data;
718e3744 887
d62a17ae 888 vl_data = XCALLOC(MTYPE_OSPF_VL_DATA, sizeof(struct ospf_vl_data));
718e3744 889
d62a17ae 890 vl_data->vl_peer.s_addr = vl_peer.s_addr;
891 vl_data->vl_area_id = area->area_id;
892 vl_data->vl_area_id_fmt = area->area_id_fmt;
718e3744 893
d62a17ae 894 return vl_data;
718e3744 895}
896
d62a17ae 897void ospf_vl_data_free(struct ospf_vl_data *vl_data)
718e3744 898{
d62a17ae 899 XFREE(MTYPE_OSPF_VL_DATA, vl_data);
718e3744 900}
901
d7c0a89a 902unsigned int vlink_count = 0;
718e3744 903
d62a17ae 904struct ospf_interface *ospf_vl_new(struct ospf *ospf,
905 struct ospf_vl_data *vl_data)
718e3744 906{
d62a17ae 907 struct ospf_interface *voi;
908 struct interface *vi;
bcc24579 909 char ifname[INTERFACE_NAMSIZ];
d62a17ae 910 struct ospf_area *area;
911 struct in_addr area_id;
912 struct connected *co;
913 struct prefix_ipv4 *p;
914
915 if (IS_DEBUG_OSPF_EVENT)
29448075 916 zlog_debug("%s: (%s): Start", __func__, ospf_get_name(ospf));
d62a17ae 917 if (vlink_count == OSPF_VL_MAX_COUNT) {
918 if (IS_DEBUG_OSPF_EVENT)
919 zlog_debug(
29448075 920 "%s: Alarm: cannot create more than OSPF_MAX_VL_COUNT virtual links",
921 __func__);
922
d62a17ae 923 return NULL;
924 }
925
926 if (IS_DEBUG_OSPF_EVENT)
29448075 927 zlog_debug("%s: creating pseudo zebra interface vrf id %u",
928 __func__, ospf->vrf_id);
d62a17ae 929
2ec42b85 930 snprintf(ifname, sizeof(ifname), "VLINK%u", vlink_count);
f60a1188 931 vi = if_get_by_name(ifname, ospf->vrf_id, ospf->name);
d62a17ae 932 /*
f60a1188 933 * if_get_by_name sets ZEBRA_INTERFACE_LINKDETECTION
d62a17ae 934 * virtual links don't need this.
935 */
936 UNSET_FLAG(vi->status, ZEBRA_INTERFACE_LINKDETECTION);
937 co = connected_new();
938 co->ifp = vi;
939 listnode_add(vi->connected, co);
940
941 p = prefix_ipv4_new();
942 p->family = AF_INET;
975a328e 943 p->prefix.s_addr = INADDR_ANY;
d62a17ae 944 p->prefixlen = 0;
945
946 co->address = (struct prefix *)p;
947
948 voi = ospf_if_new(ospf, vi, co->address);
949 if (voi == NULL) {
950 if (IS_DEBUG_OSPF_EVENT)
951 zlog_debug(
29448075 952 "%s: Alarm: OSPF int structure is not created",
953 __func__);
954
d62a17ae 955 return NULL;
956 }
957 voi->connected = co;
958 voi->vl_data = vl_data;
959 voi->ifp->mtu = OSPF_VL_MTU;
960 voi->type = OSPF_IFTYPE_VIRTUALLINK;
961
962 vlink_count++;
963 if (IS_DEBUG_OSPF_EVENT)
29448075 964 zlog_debug("%s: Created name: %s set if->name to %s", __func__,
965 ifname, vi->name);
d62a17ae 966
975a328e 967 area_id.s_addr = INADDR_ANY;
d62a17ae 968 area = ospf_area_get(ospf, area_id);
969 voi->area = area;
970
971 if (IS_DEBUG_OSPF_EVENT)
29448075 972 zlog_debug("%s: set associated area to the backbone", __func__);
d62a17ae 973
974 /* Add pseudo neighbor. */
975 ospf_nbr_self_reset(voi, voi->ospf->router_id);
976
977 ospf_area_add_if(voi->area, voi);
978
d62a17ae 979 if (IS_DEBUG_OSPF_EVENT)
29448075 980 zlog_debug("%s: Stop", __func__);
d62a17ae 981 return voi;
718e3744 982}
983
d62a17ae 984static void ospf_vl_if_delete(struct ospf_vl_data *vl_data)
718e3744 985{
7a004ccf 986 struct interface *ifp = vl_data->vl_oi->ifp;
ce27a13e 987 struct vrf *vrf = ifp->vrf;
7a004ccf 988
975a328e 989 vl_data->vl_oi->address->u.prefix4.s_addr = INADDR_ANY;
d62a17ae 990 vl_data->vl_oi->address->prefixlen = 0;
991 ospf_if_free(vl_data->vl_oi);
7a004ccf 992 if_delete(&ifp);
ce27a13e
IR
993 if (!vrf_is_enabled(vrf))
994 vrf_delete(vrf);
718e3744 995}
996
19ce7d5e
PG
997/* for a defined area, count the number of configured vl
998 */
999int 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
9c27ef9b
PJ
1014/* Look up vl_data for given peer, optionally qualified to be in the
1015 * specified area. NULL area returns first found..
1016 */
d62a17ae 1017struct ospf_vl_data *ospf_vl_lookup(struct ospf *ospf, struct ospf_area *area,
1018 struct in_addr vl_peer)
718e3744 1019{
d62a17ae 1020 struct ospf_vl_data *vl_data;
1021 struct listnode *node;
1022
1023 if (IS_DEBUG_OSPF_EVENT) {
96b663a3 1024 zlog_debug("%s: Looking for %pI4", __func__, &vl_peer);
d62a17ae 1025 if (area)
96b663a3
MS
1026 zlog_debug("%s: in area %pI4", __func__,
1027 &area->area_id);
d62a17ae 1028 }
1029
1030 for (ALL_LIST_ELEMENTS_RO(ospf->vlinks, node, vl_data)) {
1031 if (IS_DEBUG_OSPF_EVENT)
96b663a3 1032 zlog_debug("%s: VL %s, peer %pI4", __func__,
d62a17ae 1033 vl_data->vl_oi->ifp->name,
96b663a3 1034 &vl_data->vl_peer);
d62a17ae 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;
718e3744 1045}
1046
d62a17ae 1047static void ospf_vl_shutdown(struct ospf_vl_data *vl_data)
718e3744 1048{
d62a17ae 1049 struct ospf_interface *oi;
718e3744 1050
d62a17ae 1051 if ((oi = vl_data->vl_oi) == NULL)
1052 return;
718e3744 1053
975a328e 1054 oi->address->u.prefix4.s_addr = INADDR_ANY;
d62a17ae 1055 oi->address->prefixlen = 0;
718e3744 1056
d62a17ae 1057 UNSET_FLAG(oi->ifp->flags, IFF_UP);
1058 /* OSPF_ISM_EVENT_SCHEDULE (oi, ISM_InterfaceDown); */
1059 OSPF_ISM_EVENT_EXECUTE(oi, ISM_InterfaceDown);
718e3744 1060}
1061
d62a17ae 1062void ospf_vl_add(struct ospf *ospf, struct ospf_vl_data *vl_data)
718e3744 1063{
d62a17ae 1064 listnode_add(ospf->vlinks, vl_data);
1065 hook_call(ospf_vl_add, vl_data);
718e3744 1066}
1067
d62a17ae 1068void ospf_vl_delete(struct ospf *ospf, struct ospf_vl_data *vl_data)
718e3744 1069{
d62a17ae 1070 ospf_vl_shutdown(vl_data);
1071 ospf_vl_if_delete(vl_data);
718e3744 1072
d62a17ae 1073 hook_call(ospf_vl_delete, vl_data);
1074 listnode_delete(ospf->vlinks, vl_data);
718e3744 1075
d62a17ae 1076 ospf_vl_data_free(vl_data);
718e3744 1077}
1078
1d376ff5
G
1079static int ospf_vl_set_params(struct ospf_area *area,
1080 struct ospf_vl_data *vl_data, struct vertex *v)
718e3744 1081{
d62a17ae 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;
1d376ff5 1088 struct ospf_interface *oi;
d62a17ae 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)) {
1d376ff5 1099 vl_data->nexthop.lsa_pos = vp->nexthop->lsa_pos;
d62a17ae 1100 vl_data->nexthop.router = vp->nexthop->router;
1101
1d376ff5
G
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);
d62a17ae 1109
1d376ff5
G
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 }
d62a17ae 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 */
266469eb 1136 zlog_info("ospf_vl_set_params: No backlink for %s!",
d62a17ae 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)
96b663a3 1156 zlog_debug("%s: %s peer address: %pI4, cost: %d,%schanged",
d62a17ae 1157 __func__, vl_data->vl_oi->ifp->name,
96b663a3 1158 &vl_data->peer_addr, voi->output_cost,
d62a17ae 1159 (changed ? " " : " un"));
1160
1161 return changed;
718e3744 1162}
1163
1164
d62a17ae 1165void ospf_vl_up_check(struct ospf_area *area, struct in_addr rid,
1166 struct vertex *v)
718e3744 1167{
d62a17ae 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) {
29448075 1174 zlog_debug("%s: Start", __func__);
1175 zlog_debug("%s: Router ID is %pI4 Area is %pI4", __func__, &rid,
96b663a3 1176 &area->area_id);
718e3744 1177 }
1178
d62a17ae 1179 for (ALL_LIST_ELEMENTS_RO(ospf->vlinks, node, vl_data)) {
1180 if (IS_DEBUG_OSPF_EVENT) {
96b663a3 1181 zlog_debug("%s: considering VL, %s in area %pI4",
d62a17ae 1182 __func__, vl_data->vl_oi->ifp->name,
96b663a3
MS
1183 &vl_data->vl_area_id);
1184 zlog_debug("%s: peer ID: %pI4", __func__,
1185 &vl_data->vl_peer);
d62a17ae 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)
29448075 1194 zlog_debug("%s: this VL matched", __func__);
d62a17ae 1195
1196 if (oi->state == ISM_Down) {
1197 if (IS_DEBUG_OSPF_EVENT)
1198 zlog_debug(
29448075 1199 "%s: VL is down, waking it up",
1200 __func__);
d62a17ae 1201 SET_FLAG(oi->ifp->flags, IFF_UP);
1202 OSPF_ISM_EVENT_EXECUTE(oi, ISM_InterfaceUp);
1203 }
1204
1d376ff5 1205 if (ospf_vl_set_params(area, vl_data, v)) {
d62a17ae 1206 if (IS_DEBUG_OSPF(ism, ISM_EVENTS))
1207 zlog_debug(
29448075 1208 "%s: VL cost change, scheduling router lsa refresh",
1209 __func__);
d62a17ae 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(
29448075 1215 "%s: VL cost change, no backbone!",
1216 __func__);
d62a17ae 1217 }
1218 }
1219 }
718e3744 1220}
1221
d62a17ae 1222void ospf_vl_unapprove(struct ospf *ospf)
718e3744 1223{
d62a17ae 1224 struct listnode *node;
1225 struct ospf_vl_data *vl_data;
718e3744 1226
d62a17ae 1227 for (ALL_LIST_ELEMENTS_RO(ospf->vlinks, node, vl_data))
1228 UNSET_FLAG(vl_data->flags, OSPF_VL_FLAG_APPROVED);
718e3744 1229}
1230
d62a17ae 1231void ospf_vl_shut_unapproved(struct ospf *ospf)
718e3744 1232{
d62a17ae 1233 struct listnode *node, *nnode;
1234 struct ospf_vl_data *vl_data;
718e3744 1235
d62a17ae 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);
718e3744 1239}
1240
d62a17ae 1241int ospf_full_virtual_nbrs(struct ospf_area *area)
718e3744 1242{
d62a17ae 1243 if (IS_DEBUG_OSPF_EVENT) {
1244 zlog_debug(
96b663a3
MS
1245 "counting fully adjacent virtual neighbors in area %pI4",
1246 &area->area_id);
d62a17ae 1247 zlog_debug("there are %d of them", area->full_vls);
1248 }
1249
1250 return area->full_vls;
718e3744 1251}
1252
d62a17ae 1253int ospf_vls_in_area(struct ospf_area *area)
718e3744 1254{
d62a17ae 1255 struct listnode *node;
1256 struct ospf_vl_data *vl_data;
1257 int c = 0;
718e3744 1258
d62a17ae 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++;
718e3744 1262
d62a17ae 1263 return c;
718e3744 1264}
1265
6b0655a2 1266
4d762f26 1267struct crypt_key *ospf_crypt_key_new(void)
718e3744 1268{
d62a17ae 1269 return XCALLOC(MTYPE_OSPF_CRYPT_KEY, sizeof(struct crypt_key));
718e3744 1270}
1271
d62a17ae 1272void ospf_crypt_key_add(struct list *crypt, struct crypt_key *ck)
718e3744 1273{
d62a17ae 1274 listnode_add(crypt, ck);
718e3744 1275}
1276
d7c0a89a 1277struct crypt_key *ospf_crypt_key_lookup(struct list *auth_crypt, uint8_t key_id)
718e3744 1278{
d62a17ae 1279 struct listnode *node;
1280 struct crypt_key *ck;
718e3744 1281
d62a17ae 1282 for (ALL_LIST_ELEMENTS_RO(auth_crypt, node, ck))
1283 if (ck->key_id == key_id)
1284 return ck;
718e3744 1285
d62a17ae 1286 return NULL;
718e3744 1287}
1288
d7c0a89a 1289int ospf_crypt_key_delete(struct list *auth_crypt, uint8_t key_id)
718e3744 1290{
d62a17ae 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;
718e3744 1303}
1304
d7c0a89a 1305uint8_t ospf_default_iftype(struct interface *ifp)
bc18d616 1306{
d62a17ae 1307 if (if_is_pointopoint(ifp))
1308 return OSPF_IFTYPE_POINTOPOINT;
608c8870 1309 else if (if_is_loopback(ifp))
d62a17ae 1310 return OSPF_IFTYPE_LOOPBACK;
1311 else
1312 return OSPF_IFTYPE_BROADCAST;
bc18d616 1313}
1314
ef7bd2a3
DS
1315void ospf_if_interface(struct interface *ifp)
1316{
1317 hook_call(ospf_if_update, ifp);
1318}
1319
cbf32f74 1320uint32_t ospf_if_count_area_params(struct interface *ifp)
138c5a74 1321{
cbf15681
IR
1322 struct ospf_if_params *params;
1323 struct route_node *rn;
1324 uint32_t count = 0;
cbf32f74
IR
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
1338static int ospf_ifp_create(struct interface *ifp)
1339{
1340 struct ospf *ospf = NULL;
2655301d 1341 struct ospf_if_info *oii;
ef7bd2a3
DS
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",
096f7609
IR
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);
ef7bd2a3
DS
1349
1350 assert(ifp->info);
1351
2655301d
DS
1352 oii = ifp->info;
1353 oii->curr_mtu = ifp->mtu;
1354
ef7bd2a3
DS
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
096f7609 1361 ospf = ifp->vrf->info;
ef7bd2a3
DS
1362 if (!ospf)
1363 return 0;
1364
cbf32f74 1365 if (ospf_if_count_area_params(ifp) > 0)
cbf15681 1366 ospf_interface_area_set(ospf, ifp);
cbf15681 1367
ef7bd2a3
DS
1368 ospf_if_recalculate_output_cost(ifp);
1369
1370 ospf_if_update(ospf, ifp);
1371
0b4124c1
KS
1372 if (HAS_LINK_PARAMS(ifp))
1373 ospf_mpls_te_update_if(ifp);
1374
ef7bd2a3
DS
1375 hook_call(ospf_if_update, ifp);
1376
138c5a74
DS
1377 return 0;
1378}
1379
1380static int ospf_ifp_up(struct interface *ifp)
1381{
ddbf3e60
DS
1382 struct ospf_interface *oi;
1383 struct route_node *rn;
85d25587 1384 struct ospf_if_info *oii = ifp->info;
04a0401f
MS
1385 struct ospf *ospf;
1386
1387 if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE))
1388 zlog_debug("Zebra: Interface[%s] state change to up.",
1389 ifp->name);
1390
1391 /* Open per-intf write socket if configured */
1392 ospf = ifp->vrf->info;
1393 if (ospf && ospf->intf_socket_enabled)
1394 ospf_ifp_sock_init(ifp);
ddbf3e60 1395
85d25587 1396 ospf_if_recalculate_output_cost(ifp);
ddbf3e60 1397
85d25587 1398 if (oii && oii->curr_mtu != ifp->mtu) {
ddbf3e60
DS
1399 if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE))
1400 zlog_debug(
85d25587
DS
1401 "Zebra: Interface[%s] MTU change %u -> %u.",
1402 ifp->name, oii->curr_mtu, ifp->mtu);
ddbf3e60 1403
85d25587
DS
1404 oii->curr_mtu = ifp->mtu;
1405 /* Must reset the interface (simulate down/up) when MTU
1406 * changes. */
1407 ospf_if_reset(ifp);
ddbf3e60 1408
ddbf3e60
DS
1409 return 0;
1410 }
1411
ddbf3e60
DS
1412 for (rn = route_top(IF_OIFS(ifp)); rn; rn = route_next(rn)) {
1413 if ((oi = rn->info) == NULL)
1414 continue;
1415
1416 ospf_if_up(oi);
1417 }
1418
0b4124c1
KS
1419 if (HAS_LINK_PARAMS(ifp))
1420 ospf_mpls_te_update_if(ifp);
1421
138c5a74
DS
1422 return 0;
1423}
1424
1425static int ospf_ifp_down(struct interface *ifp)
1426{
b0b69e59
DS
1427 struct ospf_interface *oi;
1428 struct route_node *node;
1429
1430 if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE))
1431 zlog_debug("Zebra: Interface[%s] state change to down.",
1432 ifp->name);
1433
1434 for (node = route_top(IF_OIFS(ifp)); node; node = route_next(node)) {
1435 if ((oi = node->info) == NULL)
1436 continue;
1437 ospf_if_down(oi);
1438 }
1439
04a0401f
MS
1440 /* Close per-interface write socket if configured */
1441 ospf_ifp_sock_close(ifp);
1442
138c5a74
DS
1443 return 0;
1444}
1445
1446static int ospf_ifp_destroy(struct interface *ifp)
1447{
cbf15681 1448 struct ospf *ospf;
3c3c3252
DS
1449 struct route_node *rn;
1450
1451 if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE))
1452 zlog_debug(
1453 "Zebra: interface delete %s vrf %s[%u] index %d flags %llx metric %d mtu %d",
096f7609
IR
1454 ifp->name, ifp->vrf->name, ifp->vrf->vrf_id,
1455 ifp->ifindex, (unsigned long long)ifp->flags,
1456 ifp->metric, ifp->mtu);
3c3c3252
DS
1457
1458 hook_call(ospf_if_delete, ifp);
1459
096f7609 1460 ospf = ifp->vrf->info;
cbf15681 1461 if (ospf) {
cbf32f74 1462 if (ospf_if_count_area_params(ifp) > 0)
cbf15681 1463 ospf_interface_area_unset(ospf, ifp);
cbf15681
IR
1464 }
1465
3c3c3252
DS
1466 for (rn = route_top(IF_OIFS(ifp)); rn; rn = route_next(rn))
1467 if (rn->info)
1468 ospf_if_free((struct ospf_interface *)rn->info);
1469
138c5a74
DS
1470 return 0;
1471}
1472
be418160 1473/* Resetting ospf hello timer */
1474void ospf_reset_hello_timer(struct interface *ifp, struct in_addr addr,
1475 bool is_addr)
1476{
1477 struct route_node *rn;
1478
1479 if (is_addr) {
1480 struct prefix p;
1481 struct ospf_interface *oi = NULL;
1482
1483 p.u.prefix4 = addr;
1484 p.family = AF_INET;
46a8c343 1485 p.prefixlen = IPV4_MAX_BITLEN;
be418160 1486
1487 oi = ospf_if_table_lookup(ifp, &p);
1488
1489 if (oi) {
1490 /* Send hello before restart the hello timer
1491 * to avoid session flaps in case of bigger
1492 * hello interval configurations.
1493 */
1494 ospf_hello_send(oi);
1495
1496 /* Restart hello timer for this interface */
e16d030c 1497 EVENT_OFF(oi->t_hello);
be418160 1498 OSPF_HELLO_TIMER_ON(oi);
1499 }
1500
1501 return;
1502 }
1503
1504 for (rn = route_top(IF_OIFS(ifp)); rn; rn = route_next(rn)) {
1505 struct ospf_interface *oi = rn->info;
1506
1507 if (!oi)
1508 continue;
1509
1510 /* If hello interval configured on this oi, don't restart. */
1511 if (OSPF_IF_PARAM_CONFIGURED(oi->params, v_hello))
1512 continue;
1513
1514 /* Send hello before restart the hello timer
1515 * to avoid session flaps in case of bigger
1516 * hello interval configurations.
1517 */
1518 ospf_hello_send(oi);
1519
1520 /* Restart the hello timer. */
e16d030c 1521 EVENT_OFF(oi->t_hello);
be418160 1522 OSPF_HELLO_TIMER_ON(oi);
1523 }
1524}
1525
4d762f26 1526void ospf_if_init(void)
718e3744 1527{
138c5a74
DS
1528 if_zapi_callbacks(ospf_ifp_create, ospf_ifp_up,
1529 ospf_ifp_down, ospf_ifp_destroy);
1530
d62a17ae 1531 /* Initialize Zebra interface data structure. */
ce19a04a
DL
1532 hook_register_prio(if_add, 0, ospf_if_new_hook);
1533 hook_register_prio(if_del, 0, ospf_if_delete_hook);
718e3744 1534}