]> git.proxmox.com Git - mirror_frr.git/blame - ospfd/ospf_interface.c
tests: Do not try establishing a connection from r1 to r2
[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);
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);
bf617538 536 UNSET_IF_PARAM(oip, if_area);
d62a17ae 537
538 oip->auth_crypt = list_new();
539
540 oip->network_lsa_seqnum = htonl(OSPF_INITIAL_SEQUENCE_NUMBER);
182d6bdc 541 oip->is_v_wait_set = false;
d62a17ae 542
bc97889b
AL
543 oip->ptp_dmvpn = 0;
544
d62a17ae 545 return oip;
718e3744 546}
547
659f4e40
RZ
548static void ospf_del_if_params(struct interface *ifp,
549 struct ospf_if_params *oip)
718e3744 550{
6a154c88 551 list_delete(&oip->auth_crypt);
659f4e40 552 ospf_interface_disable_bfd(ifp, oip);
132a782e 553 ldp_sync_info_free(&(oip->ldp_sync_info));
d62a17ae 554 XFREE(MTYPE_OSPF_IF_PARAMS, oip);
718e3744 555}
556
d62a17ae 557void ospf_free_if_params(struct interface *ifp, struct in_addr addr)
718e3744 558{
d62a17ae 559 struct ospf_if_params *oip;
560 struct prefix_ipv4 p;
561 struct route_node *rn;
562
563 p.family = AF_INET;
936fbaef 564 p.prefixlen = IPV4_MAX_BITLEN;
d62a17ae 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)
bf617538 584 && !OSPF_IF_PARAM_CONFIGURED(oip, if_area)
2917d36b 585 && listcount(oip->auth_crypt) == 0) {
659f4e40 586 ospf_del_if_params(ifp, oip);
d62a17ae 587 rn->info = NULL;
588 route_unlock_node(rn);
589 }
718e3744 590}
591
d62a17ae 592struct ospf_if_params *ospf_lookup_if_params(struct interface *ifp,
593 struct in_addr addr)
718e3744 594{
d62a17ae 595 struct prefix_ipv4 p;
596 struct route_node *rn;
597
598 p.family = AF_INET;
936fbaef 599 p.prefixlen = IPV4_MAX_BITLEN;
d62a17ae 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;
718e3744 610}
611
d62a17ae 612struct ospf_if_params *ospf_get_if_params(struct interface *ifp,
613 struct in_addr addr)
718e3744 614{
d62a17ae 615 struct prefix_ipv4 p;
616 struct route_node *rn;
617
618 p.family = AF_INET;
936fbaef 619 p.prefixlen = IPV4_MAX_BITLEN;
d62a17ae 620 p.prefix = addr;
a6435618 621 apply_mask_ipv4(&p);
d62a17ae 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;
718e3744 631}
632
d62a17ae 633void ospf_if_update_params(struct interface *ifp, struct in_addr addr)
718e3744 634{
d62a17ae 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 }
718e3744 646}
647
d62a17ae 648int ospf_if_new_hook(struct interface *ifp)
718e3744 649{
d62a17ae 650 int rc = 0;
651
652 ifp->info = XCALLOC(MTYPE_OSPF_IF_INFO, sizeof(struct ospf_if_info));
653
04a0401f
MS
654 IF_OSPF_IF_INFO(ifp)->oii_fd = -1;
655
d62a17ae 656 IF_OIFS(ifp) = route_table_init();
657 IF_OIFS_PARAMS(ifp) = route_table_init();
658
659 IF_DEF_PARAMS(ifp) = ospf_new_if_params();
660
661 SET_IF_PARAM(IF_DEF_PARAMS(ifp), transmit_delay);
662 IF_DEF_PARAMS(ifp)->transmit_delay = OSPF_TRANSMIT_DELAY_DEFAULT;
663
664 SET_IF_PARAM(IF_DEF_PARAMS(ifp), retransmit_interval);
665 IF_DEF_PARAMS(ifp)->retransmit_interval =
666 OSPF_RETRANSMIT_INTERVAL_DEFAULT;
667
668 SET_IF_PARAM(IF_DEF_PARAMS(ifp), priority);
669 IF_DEF_PARAMS(ifp)->priority = OSPF_ROUTER_PRIORITY_DEFAULT;
670
671 IF_DEF_PARAMS(ifp)->mtu_ignore = OSPF_MTU_IGNORE_DEFAULT;
672
673 SET_IF_PARAM(IF_DEF_PARAMS(ifp), v_hello);
674 IF_DEF_PARAMS(ifp)->v_hello = OSPF_HELLO_INTERVAL_DEFAULT;
675
676 SET_IF_PARAM(IF_DEF_PARAMS(ifp), fast_hello);
677 IF_DEF_PARAMS(ifp)->fast_hello = OSPF_FAST_HELLO_DEFAULT;
678
679 SET_IF_PARAM(IF_DEF_PARAMS(ifp), v_wait);
680 IF_DEF_PARAMS(ifp)->v_wait = OSPF_ROUTER_DEAD_INTERVAL_DEFAULT;
681
682 SET_IF_PARAM(IF_DEF_PARAMS(ifp), auth_simple);
683 memset(IF_DEF_PARAMS(ifp)->auth_simple, 0, OSPF_AUTH_SIMPLE_SIZE);
684
685 SET_IF_PARAM(IF_DEF_PARAMS(ifp), auth_type);
686 IF_DEF_PARAMS(ifp)->auth_type = OSPF_AUTH_NOTSET;
687
688 rc = ospf_opaque_new_if(ifp);
689 return rc;
718e3744 690}
691
d62a17ae 692static int ospf_if_delete_hook(struct interface *ifp)
718e3744 693{
d62a17ae 694 int rc = 0;
695 struct route_node *rn;
04a0401f
MS
696 struct ospf_if_info *oii;
697
d62a17ae 698 rc = ospf_opaque_del_if(ifp);
940b01aa 699
e2134a79
RZ
700 /*
701 * This function must be called before `route_table_finish` due to
702 * BFD integration need to iterate over the interface neighbors to
703 * remove all registrations.
704 */
705 ospf_del_if_params(ifp, IF_DEF_PARAMS(ifp));
706
d62a17ae 707 for (rn = route_top(IF_OIFS_PARAMS(ifp)); rn; rn = route_next(rn))
708 if (rn->info)
659f4e40 709 ospf_del_if_params(ifp, rn->info);
9ffde6e1
DS
710
711 route_table_finish(IF_OIFS(ifp));
d62a17ae 712 route_table_finish(IF_OIFS_PARAMS(ifp));
940b01aa 713
04a0401f
MS
714 /* Close per-interface socket */
715 oii = ifp->info;
716 if (oii && oii->oii_fd > 0) {
717 close(oii->oii_fd);
718 oii->oii_fd = -1;
719 }
720
d62a17ae 721 XFREE(MTYPE_OSPF_IF_INFO, ifp->info);
718e3744 722
d62a17ae 723 return rc;
718e3744 724}
725
d62a17ae 726int ospf_if_is_enable(struct ospf_interface *oi)
718e3744 727{
608c8870 728 if (!(if_is_loopback(oi->ifp)))
d62a17ae 729 if (if_is_up(oi->ifp))
730 return 1;
718e3744 731
d62a17ae 732 return 0;
718e3744 733}
734
d62a17ae 735void ospf_if_set_multicast(struct ospf_interface *oi)
ba6454ec 736{
d62a17ae 737 if ((oi->state > ISM_Loopback) && (oi->type != OSPF_IFTYPE_LOOPBACK)
738 && (oi->type != OSPF_IFTYPE_VIRTUALLINK)
739 && (OSPF_IF_PASSIVE_STATUS(oi) == OSPF_IF_ACTIVE)) {
740 /* The interface should belong to the OSPF-all-routers group. */
741 if (!OI_MEMBER_CHECK(oi, MEMBER_ALLROUTERS)
742 && (ospf_if_add_allspfrouters(oi->ospf, oi->address,
743 oi->ifp->ifindex)
744 >= 0))
745 /* Set the flag only if the system call to join
746 * succeeded. */
747 OI_MEMBER_JOINED(oi, MEMBER_ALLROUTERS);
748 } else {
749 /* The interface should NOT belong to the OSPF-all-routers
750 * group. */
751 if (OI_MEMBER_CHECK(oi, MEMBER_ALLROUTERS)) {
752 /* Only actually drop if this is the last reference */
753 if (OI_MEMBER_COUNT(oi, MEMBER_ALLROUTERS) == 1)
754 ospf_if_drop_allspfrouters(oi->ospf,
755 oi->address,
756 oi->ifp->ifindex);
757 /* Unset the flag regardless of whether the system call
758 to leave
759 the group succeeded, since it's much safer to assume
760 that
761 we are not a member. */
762 OI_MEMBER_LEFT(oi, MEMBER_ALLROUTERS);
763 }
764 }
765
766 if (((oi->type == OSPF_IFTYPE_BROADCAST)
767 || (oi->type == OSPF_IFTYPE_POINTOPOINT))
768 && ((oi->state == ISM_DR) || (oi->state == ISM_Backup))
769 && (OSPF_IF_PASSIVE_STATUS(oi) == OSPF_IF_ACTIVE)) {
770 /* The interface should belong to the OSPF-designated-routers
771 * group. */
772 if (!OI_MEMBER_CHECK(oi, MEMBER_DROUTERS)
773 && (ospf_if_add_alldrouters(oi->ospf, oi->address,
774 oi->ifp->ifindex)
775 >= 0))
776 /* Set the flag only if the system call to join
777 * succeeded. */
778 OI_MEMBER_JOINED(oi, MEMBER_DROUTERS);
779 } else {
780 /* The interface should NOT belong to the
781 * OSPF-designated-routers group */
782 if (OI_MEMBER_CHECK(oi, MEMBER_DROUTERS)) {
783 /* drop only if last reference */
784 if (OI_MEMBER_COUNT(oi, MEMBER_DROUTERS) == 1)
785 ospf_if_drop_alldrouters(oi->ospf, oi->address,
786 oi->ifp->ifindex);
787
788 /* Unset the flag regardless of whether the system call
789 to leave
790 the group succeeded, since it's much safer to assume
791 that
792 we are not a member. */
793 OI_MEMBER_LEFT(oi, MEMBER_DROUTERS);
794 }
795 }
ba6454ec 796}
797
d62a17ae 798int ospf_if_up(struct ospf_interface *oi)
718e3744 799{
d62a17ae 800 if (oi == NULL)
801 return 0;
802
803 if (oi->type == OSPF_IFTYPE_LOOPBACK)
804 OSPF_ISM_EVENT_SCHEDULE(oi, ISM_LoopInd);
805 else {
d62a17ae 806 OSPF_ISM_EVENT_SCHEDULE(oi, ISM_InterfaceUp);
807 }
808
809 return 1;
718e3744 810}
811
d62a17ae 812int ospf_if_down(struct ospf_interface *oi)
718e3744 813{
df074ec3 814 struct ospf *ospf;
effee187
DS
815 struct route_node *rn;
816 struct ospf_route *or;
817 struct listnode *nh;
818 struct ospf_path *op;
df074ec3 819
d62a17ae 820 if (oi == NULL)
821 return 0;
718e3744 822
df074ec3 823 ospf = oi->ospf;
824
825 /* Cease the HELPER role for all the neighbours
826 * of this interface.
827 */
828 if (ospf->is_helper_supported) {
829 struct route_node *rn = NULL;
830
831 if (ospf_interface_neighbor_count(oi)) {
832 for (rn = route_top(oi->nbrs); rn;
833 rn = route_next(rn)) {
834 struct ospf_neighbor *nbr = NULL;
835
836 if (!rn->info)
837 continue;
838
839 nbr = rn->info;
840
841 if (OSPF_GR_IS_ACTIVE_HELPER(nbr))
842 ospf_gr_helper_exit(
843 nbr, OSPF_GR_HELPER_TOPO_CHG);
844 }
845 }
846 }
847
d62a17ae 848 OSPF_ISM_EVENT_EXECUTE(oi, ISM_InterfaceDown);
849 /* delete position in router LSA */
850 oi->lsa_pos_beg = 0;
851 oi->lsa_pos_end = 0;
852 /* Shutdown packet reception and sending */
853 ospf_if_stream_unset(oi);
718e3744 854
d4e66f14
TS
855 if (!ospf->new_table)
856 return 1;
effee187
DS
857 for (rn = route_top(ospf->new_table); rn; rn = route_next(rn)) {
858 or = rn->info;
859
860 if (!or)
861 continue;
862
863 for (nh = listhead(or->paths); nh;
864 nh = listnextnode_unchecked(nh)) {
865 op = listgetdata(nh);
866 if (op->ifindex == oi->ifp->ifindex) {
867 or->changed = true;
868 break;
869 }
870 }
871 }
872
d62a17ae 873 return 1;
718e3744 874}
875
6b0655a2 876
718e3744 877/* Virtual Link related functions. */
878
d62a17ae 879struct ospf_vl_data *ospf_vl_data_new(struct ospf_area *area,
880 struct in_addr vl_peer)
718e3744 881{
d62a17ae 882 struct ospf_vl_data *vl_data;
718e3744 883
d62a17ae 884 vl_data = XCALLOC(MTYPE_OSPF_VL_DATA, sizeof(struct ospf_vl_data));
718e3744 885
d62a17ae 886 vl_data->vl_peer.s_addr = vl_peer.s_addr;
887 vl_data->vl_area_id = area->area_id;
888 vl_data->vl_area_id_fmt = area->area_id_fmt;
718e3744 889
d62a17ae 890 return vl_data;
718e3744 891}
892
d62a17ae 893void ospf_vl_data_free(struct ospf_vl_data *vl_data)
718e3744 894{
d62a17ae 895 XFREE(MTYPE_OSPF_VL_DATA, vl_data);
718e3744 896}
897
d7c0a89a 898unsigned int vlink_count = 0;
718e3744 899
d62a17ae 900struct ospf_interface *ospf_vl_new(struct ospf *ospf,
901 struct ospf_vl_data *vl_data)
718e3744 902{
d62a17ae 903 struct ospf_interface *voi;
904 struct interface *vi;
bcc24579 905 char ifname[INTERFACE_NAMSIZ];
d62a17ae 906 struct ospf_area *area;
907 struct in_addr area_id;
908 struct connected *co;
909 struct prefix_ipv4 *p;
910
911 if (IS_DEBUG_OSPF_EVENT)
29448075 912 zlog_debug("%s: (%s): Start", __func__, ospf_get_name(ospf));
d62a17ae 913 if (vlink_count == OSPF_VL_MAX_COUNT) {
914 if (IS_DEBUG_OSPF_EVENT)
915 zlog_debug(
29448075 916 "%s: Alarm: cannot create more than OSPF_MAX_VL_COUNT virtual links",
917 __func__);
918
d62a17ae 919 return NULL;
920 }
921
922 if (IS_DEBUG_OSPF_EVENT)
29448075 923 zlog_debug("%s: creating pseudo zebra interface vrf id %u",
924 __func__, ospf->vrf_id);
d62a17ae 925
2ec42b85 926 snprintf(ifname, sizeof(ifname), "VLINK%u", vlink_count);
f60a1188 927 vi = if_get_by_name(ifname, ospf->vrf_id, ospf->name);
d62a17ae 928 /*
f60a1188 929 * if_get_by_name sets ZEBRA_INTERFACE_LINKDETECTION
d62a17ae 930 * virtual links don't need this.
931 */
932 UNSET_FLAG(vi->status, ZEBRA_INTERFACE_LINKDETECTION);
933 co = connected_new();
934 co->ifp = vi;
935 listnode_add(vi->connected, co);
936
937 p = prefix_ipv4_new();
938 p->family = AF_INET;
975a328e 939 p->prefix.s_addr = INADDR_ANY;
d62a17ae 940 p->prefixlen = 0;
941
942 co->address = (struct prefix *)p;
943
944 voi = ospf_if_new(ospf, vi, co->address);
945 if (voi == NULL) {
946 if (IS_DEBUG_OSPF_EVENT)
947 zlog_debug(
29448075 948 "%s: Alarm: OSPF int structure is not created",
949 __func__);
950
d62a17ae 951 return NULL;
952 }
953 voi->connected = co;
954 voi->vl_data = vl_data;
955 voi->ifp->mtu = OSPF_VL_MTU;
956 voi->type = OSPF_IFTYPE_VIRTUALLINK;
957
958 vlink_count++;
959 if (IS_DEBUG_OSPF_EVENT)
29448075 960 zlog_debug("%s: Created name: %s set if->name to %s", __func__,
961 ifname, vi->name);
d62a17ae 962
975a328e 963 area_id.s_addr = INADDR_ANY;
d62a17ae 964 area = ospf_area_get(ospf, area_id);
965 voi->area = area;
966
967 if (IS_DEBUG_OSPF_EVENT)
29448075 968 zlog_debug("%s: set associated area to the backbone", __func__);
d62a17ae 969
970 /* Add pseudo neighbor. */
971 ospf_nbr_self_reset(voi, voi->ospf->router_id);
972
973 ospf_area_add_if(voi->area, voi);
974
d62a17ae 975 if (IS_DEBUG_OSPF_EVENT)
29448075 976 zlog_debug("%s: Stop", __func__);
d62a17ae 977 return voi;
718e3744 978}
979
d62a17ae 980static void ospf_vl_if_delete(struct ospf_vl_data *vl_data)
718e3744 981{
7a004ccf 982 struct interface *ifp = vl_data->vl_oi->ifp;
ce27a13e 983 struct vrf *vrf = ifp->vrf;
7a004ccf 984
975a328e 985 vl_data->vl_oi->address->u.prefix4.s_addr = INADDR_ANY;
d62a17ae 986 vl_data->vl_oi->address->prefixlen = 0;
987 ospf_if_free(vl_data->vl_oi);
7a004ccf 988 if_delete(&ifp);
ce27a13e
IR
989 if (!vrf_is_enabled(vrf))
990 vrf_delete(vrf);
718e3744 991}
992
19ce7d5e
PG
993/* for a defined area, count the number of configured vl
994 */
995int ospf_vl_count(struct ospf *ospf, struct ospf_area *area)
996{
997 int count = 0;
998 struct ospf_vl_data *vl_data;
999 struct listnode *node;
1000
1001 for (ALL_LIST_ELEMENTS_RO(ospf->vlinks, node, vl_data)) {
1002 if (area
1003 && !IPV4_ADDR_SAME(&vl_data->vl_area_id, &area->area_id))
1004 continue;
1005 count++;
1006 }
1007 return count;
1008}
1009
9c27ef9b
PJ
1010/* Look up vl_data for given peer, optionally qualified to be in the
1011 * specified area. NULL area returns first found..
1012 */
d62a17ae 1013struct ospf_vl_data *ospf_vl_lookup(struct ospf *ospf, struct ospf_area *area,
1014 struct in_addr vl_peer)
718e3744 1015{
d62a17ae 1016 struct ospf_vl_data *vl_data;
1017 struct listnode *node;
1018
1019 if (IS_DEBUG_OSPF_EVENT) {
96b663a3 1020 zlog_debug("%s: Looking for %pI4", __func__, &vl_peer);
d62a17ae 1021 if (area)
96b663a3
MS
1022 zlog_debug("%s: in area %pI4", __func__,
1023 &area->area_id);
d62a17ae 1024 }
1025
1026 for (ALL_LIST_ELEMENTS_RO(ospf->vlinks, node, vl_data)) {
1027 if (IS_DEBUG_OSPF_EVENT)
96b663a3 1028 zlog_debug("%s: VL %s, peer %pI4", __func__,
d62a17ae 1029 vl_data->vl_oi->ifp->name,
96b663a3 1030 &vl_data->vl_peer);
d62a17ae 1031
1032 if (area
1033 && !IPV4_ADDR_SAME(&vl_data->vl_area_id, &area->area_id))
1034 continue;
1035
1036 if (IPV4_ADDR_SAME(&vl_data->vl_peer, &vl_peer))
1037 return vl_data;
1038 }
1039
1040 return NULL;
718e3744 1041}
1042
d62a17ae 1043static void ospf_vl_shutdown(struct ospf_vl_data *vl_data)
718e3744 1044{
d62a17ae 1045 struct ospf_interface *oi;
718e3744 1046
d62a17ae 1047 if ((oi = vl_data->vl_oi) == NULL)
1048 return;
718e3744 1049
975a328e 1050 oi->address->u.prefix4.s_addr = INADDR_ANY;
d62a17ae 1051 oi->address->prefixlen = 0;
718e3744 1052
d62a17ae 1053 UNSET_FLAG(oi->ifp->flags, IFF_UP);
1054 /* OSPF_ISM_EVENT_SCHEDULE (oi, ISM_InterfaceDown); */
1055 OSPF_ISM_EVENT_EXECUTE(oi, ISM_InterfaceDown);
718e3744 1056}
1057
d62a17ae 1058void ospf_vl_add(struct ospf *ospf, struct ospf_vl_data *vl_data)
718e3744 1059{
d62a17ae 1060 listnode_add(ospf->vlinks, vl_data);
1061 hook_call(ospf_vl_add, vl_data);
718e3744 1062}
1063
d62a17ae 1064void ospf_vl_delete(struct ospf *ospf, struct ospf_vl_data *vl_data)
718e3744 1065{
d62a17ae 1066 ospf_vl_shutdown(vl_data);
1067 ospf_vl_if_delete(vl_data);
718e3744 1068
d62a17ae 1069 hook_call(ospf_vl_delete, vl_data);
1070 listnode_delete(ospf->vlinks, vl_data);
718e3744 1071
d62a17ae 1072 ospf_vl_data_free(vl_data);
718e3744 1073}
1074
1d376ff5
G
1075static int ospf_vl_set_params(struct ospf_area *area,
1076 struct ospf_vl_data *vl_data, struct vertex *v)
718e3744 1077{
d62a17ae 1078 int changed = 0;
1079 struct ospf_interface *voi;
1080 struct listnode *node;
1081 struct vertex_parent *vp = NULL;
1082 unsigned int i;
1083 struct router_lsa *rl;
1d376ff5 1084 struct ospf_interface *oi;
d62a17ae 1085
1086 voi = vl_data->vl_oi;
1087
1088 if (voi->output_cost != v->distance) {
1089
1090 voi->output_cost = v->distance;
1091 changed = 1;
1092 }
1093
1094 for (ALL_LIST_ELEMENTS_RO(v->parents, node, vp)) {
1d376ff5 1095 vl_data->nexthop.lsa_pos = vp->nexthop->lsa_pos;
d62a17ae 1096 vl_data->nexthop.router = vp->nexthop->router;
1097
1d376ff5
G
1098 /*
1099 * Only deal with interface data when the local
1100 * (calculating) node is the SPF root node
1101 */
1102 if (!area->spf_dry_run) {
1103 oi = ospf_if_lookup_by_lsa_pos(
1104 area, vl_data->nexthop.lsa_pos);
d62a17ae 1105
1d376ff5
G
1106 if (!IPV4_ADDR_SAME(&voi->address->u.prefix4,
1107 &oi->address->u.prefix4))
1108 changed = 1;
1109
1110 voi->address->u.prefix4 = oi->address->u.prefix4;
1111 voi->address->prefixlen = oi->address->prefixlen;
1112 }
d62a17ae 1113
1114 break; /* We take the first interface. */
1115 }
1116
1117 rl = (struct router_lsa *)v->lsa;
1118
1119 /* use SPF determined backlink index in struct vertex
1120 * for virtual link destination address
1121 */
1122 if (vp && vp->backlink >= 0) {
1123 if (!IPV4_ADDR_SAME(&vl_data->peer_addr,
1124 &rl->link[vp->backlink].link_data))
1125 changed = 1;
1126 vl_data->peer_addr = rl->link[vp->backlink].link_data;
1127 } else {
1128 /* This is highly odd, there is no backlink index
1129 * there should be due to the ospf_spf_has_link() check
1130 * in SPF. Lets warn and try pick a link anyway.
1131 */
266469eb 1132 zlog_info("ospf_vl_set_params: No backlink for %s!",
d62a17ae 1133 vl_data->vl_oi->ifp->name);
1134 for (i = 0; i < ntohs(rl->links); i++) {
1135 switch (rl->link[i].type) {
1136 case LSA_LINK_TYPE_VIRTUALLINK:
1137 if (IS_DEBUG_OSPF_EVENT)
1138 zlog_debug(
1139 "found back link through VL");
1140 /* fallthru */
1141 case LSA_LINK_TYPE_TRANSIT:
1142 case LSA_LINK_TYPE_POINTOPOINT:
1143 if (!IPV4_ADDR_SAME(&vl_data->peer_addr,
1144 &rl->link[i].link_data))
1145 changed = 1;
1146 vl_data->peer_addr = rl->link[i].link_data;
1147 }
1148 }
1149 }
1150
1151 if (IS_DEBUG_OSPF_EVENT)
96b663a3 1152 zlog_debug("%s: %s peer address: %pI4, cost: %d,%schanged",
d62a17ae 1153 __func__, vl_data->vl_oi->ifp->name,
96b663a3 1154 &vl_data->peer_addr, voi->output_cost,
d62a17ae 1155 (changed ? " " : " un"));
1156
1157 return changed;
718e3744 1158}
1159
1160
d62a17ae 1161void ospf_vl_up_check(struct ospf_area *area, struct in_addr rid,
1162 struct vertex *v)
718e3744 1163{
d62a17ae 1164 struct ospf *ospf = area->ospf;
1165 struct listnode *node;
1166 struct ospf_vl_data *vl_data;
1167 struct ospf_interface *oi;
1168
1169 if (IS_DEBUG_OSPF_EVENT) {
29448075 1170 zlog_debug("%s: Start", __func__);
1171 zlog_debug("%s: Router ID is %pI4 Area is %pI4", __func__, &rid,
96b663a3 1172 &area->area_id);
718e3744 1173 }
1174
d62a17ae 1175 for (ALL_LIST_ELEMENTS_RO(ospf->vlinks, node, vl_data)) {
1176 if (IS_DEBUG_OSPF_EVENT) {
96b663a3 1177 zlog_debug("%s: considering VL, %s in area %pI4",
d62a17ae 1178 __func__, vl_data->vl_oi->ifp->name,
96b663a3
MS
1179 &vl_data->vl_area_id);
1180 zlog_debug("%s: peer ID: %pI4", __func__,
1181 &vl_data->vl_peer);
d62a17ae 1182 }
1183
1184 if (IPV4_ADDR_SAME(&vl_data->vl_peer, &rid)
1185 && IPV4_ADDR_SAME(&vl_data->vl_area_id, &area->area_id)) {
1186 oi = vl_data->vl_oi;
1187 SET_FLAG(vl_data->flags, OSPF_VL_FLAG_APPROVED);
1188
1189 if (IS_DEBUG_OSPF_EVENT)
29448075 1190 zlog_debug("%s: this VL matched", __func__);
d62a17ae 1191
1192 if (oi->state == ISM_Down) {
1193 if (IS_DEBUG_OSPF_EVENT)
1194 zlog_debug(
29448075 1195 "%s: VL is down, waking it up",
1196 __func__);
d62a17ae 1197 SET_FLAG(oi->ifp->flags, IFF_UP);
1198 OSPF_ISM_EVENT_EXECUTE(oi, ISM_InterfaceUp);
1199 }
1200
1d376ff5 1201 if (ospf_vl_set_params(area, vl_data, v)) {
d62a17ae 1202 if (IS_DEBUG_OSPF(ism, ISM_EVENTS))
1203 zlog_debug(
29448075 1204 "%s: VL cost change, scheduling router lsa refresh",
1205 __func__);
d62a17ae 1206 if (ospf->backbone)
1207 ospf_router_lsa_update_area(
1208 ospf->backbone);
1209 else if (IS_DEBUG_OSPF(ism, ISM_EVENTS))
1210 zlog_debug(
29448075 1211 "%s: VL cost change, no backbone!",
1212 __func__);
d62a17ae 1213 }
1214 }
1215 }
718e3744 1216}
1217
d62a17ae 1218void ospf_vl_unapprove(struct ospf *ospf)
718e3744 1219{
d62a17ae 1220 struct listnode *node;
1221 struct ospf_vl_data *vl_data;
718e3744 1222
d62a17ae 1223 for (ALL_LIST_ELEMENTS_RO(ospf->vlinks, node, vl_data))
1224 UNSET_FLAG(vl_data->flags, OSPF_VL_FLAG_APPROVED);
718e3744 1225}
1226
d62a17ae 1227void ospf_vl_shut_unapproved(struct ospf *ospf)
718e3744 1228{
d62a17ae 1229 struct listnode *node, *nnode;
1230 struct ospf_vl_data *vl_data;
718e3744 1231
d62a17ae 1232 for (ALL_LIST_ELEMENTS(ospf->vlinks, node, nnode, vl_data))
1233 if (!CHECK_FLAG(vl_data->flags, OSPF_VL_FLAG_APPROVED))
1234 ospf_vl_shutdown(vl_data);
718e3744 1235}
1236
d62a17ae 1237int ospf_full_virtual_nbrs(struct ospf_area *area)
718e3744 1238{
d62a17ae 1239 if (IS_DEBUG_OSPF_EVENT) {
1240 zlog_debug(
96b663a3
MS
1241 "counting fully adjacent virtual neighbors in area %pI4",
1242 &area->area_id);
d62a17ae 1243 zlog_debug("there are %d of them", area->full_vls);
1244 }
1245
1246 return area->full_vls;
718e3744 1247}
1248
d62a17ae 1249int ospf_vls_in_area(struct ospf_area *area)
718e3744 1250{
d62a17ae 1251 struct listnode *node;
1252 struct ospf_vl_data *vl_data;
1253 int c = 0;
718e3744 1254
d62a17ae 1255 for (ALL_LIST_ELEMENTS_RO(area->ospf->vlinks, node, vl_data))
1256 if (IPV4_ADDR_SAME(&vl_data->vl_area_id, &area->area_id))
1257 c++;
718e3744 1258
d62a17ae 1259 return c;
718e3744 1260}
1261
6b0655a2 1262
4d762f26 1263struct crypt_key *ospf_crypt_key_new(void)
718e3744 1264{
d62a17ae 1265 return XCALLOC(MTYPE_OSPF_CRYPT_KEY, sizeof(struct crypt_key));
718e3744 1266}
1267
d62a17ae 1268void ospf_crypt_key_add(struct list *crypt, struct crypt_key *ck)
718e3744 1269{
d62a17ae 1270 listnode_add(crypt, ck);
718e3744 1271}
1272
d7c0a89a 1273struct crypt_key *ospf_crypt_key_lookup(struct list *auth_crypt, uint8_t key_id)
718e3744 1274{
d62a17ae 1275 struct listnode *node;
1276 struct crypt_key *ck;
718e3744 1277
d62a17ae 1278 for (ALL_LIST_ELEMENTS_RO(auth_crypt, node, ck))
1279 if (ck->key_id == key_id)
1280 return ck;
718e3744 1281
d62a17ae 1282 return NULL;
718e3744 1283}
1284
d7c0a89a 1285int ospf_crypt_key_delete(struct list *auth_crypt, uint8_t key_id)
718e3744 1286{
d62a17ae 1287 struct listnode *node, *nnode;
1288 struct crypt_key *ck;
1289
1290 for (ALL_LIST_ELEMENTS(auth_crypt, node, nnode, ck)) {
1291 if (ck->key_id == key_id) {
1292 listnode_delete(auth_crypt, ck);
1293 XFREE(MTYPE_OSPF_CRYPT_KEY, ck);
1294 return 1;
1295 }
1296 }
1297
1298 return 0;
718e3744 1299}
1300
d7c0a89a 1301uint8_t ospf_default_iftype(struct interface *ifp)
bc18d616 1302{
d62a17ae 1303 if (if_is_pointopoint(ifp))
1304 return OSPF_IFTYPE_POINTOPOINT;
608c8870 1305 else if (if_is_loopback(ifp))
d62a17ae 1306 return OSPF_IFTYPE_LOOPBACK;
1307 else
1308 return OSPF_IFTYPE_BROADCAST;
bc18d616 1309}
1310
ef7bd2a3
DS
1311void ospf_if_interface(struct interface *ifp)
1312{
1313 hook_call(ospf_if_update, ifp);
1314}
1315
cbf32f74 1316uint32_t ospf_if_count_area_params(struct interface *ifp)
138c5a74 1317{
cbf15681
IR
1318 struct ospf_if_params *params;
1319 struct route_node *rn;
1320 uint32_t count = 0;
cbf32f74
IR
1321
1322 params = IF_DEF_PARAMS(ifp);
1323 if (OSPF_IF_PARAM_CONFIGURED(params, if_area))
1324 count++;
1325
1326 for (rn = route_top(IF_OIFS_PARAMS(ifp)); rn; rn = route_next(rn))
1327 if ((params = rn->info)
1328 && OSPF_IF_PARAM_CONFIGURED(params, if_area))
1329 count++;
1330
1331 return count;
1332}
1333
1334static int ospf_ifp_create(struct interface *ifp)
1335{
1336 struct ospf *ospf = NULL;
2655301d 1337 struct ospf_if_info *oii;
ef7bd2a3
DS
1338
1339 if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE))
1340 zlog_debug(
1341 "Zebra: interface add %s vrf %s[%u] index %d flags %llx metric %d mtu %d speed %u",
096f7609
IR
1342 ifp->name, ifp->vrf->name, ifp->vrf->vrf_id,
1343 ifp->ifindex, (unsigned long long)ifp->flags,
1344 ifp->metric, ifp->mtu, ifp->speed);
ef7bd2a3
DS
1345
1346 assert(ifp->info);
1347
2655301d
DS
1348 oii = ifp->info;
1349 oii->curr_mtu = ifp->mtu;
1350
ef7bd2a3
DS
1351 if (IF_DEF_PARAMS(ifp)
1352 && !OSPF_IF_PARAM_CONFIGURED(IF_DEF_PARAMS(ifp), type)) {
1353 SET_IF_PARAM(IF_DEF_PARAMS(ifp), type);
1354 IF_DEF_PARAMS(ifp)->type = ospf_default_iftype(ifp);
1355 }
1356
096f7609 1357 ospf = ifp->vrf->info;
ef7bd2a3
DS
1358 if (!ospf)
1359 return 0;
1360
cbf32f74 1361 if (ospf_if_count_area_params(ifp) > 0)
cbf15681 1362 ospf_interface_area_set(ospf, ifp);
cbf15681 1363
ef7bd2a3
DS
1364 ospf_if_recalculate_output_cost(ifp);
1365
1366 ospf_if_update(ospf, ifp);
1367
0b4124c1
KS
1368 if (HAS_LINK_PARAMS(ifp))
1369 ospf_mpls_te_update_if(ifp);
1370
ef7bd2a3
DS
1371 hook_call(ospf_if_update, ifp);
1372
138c5a74
DS
1373 return 0;
1374}
1375
1376static int ospf_ifp_up(struct interface *ifp)
1377{
ddbf3e60
DS
1378 struct ospf_interface *oi;
1379 struct route_node *rn;
85d25587 1380 struct ospf_if_info *oii = ifp->info;
04a0401f
MS
1381 struct ospf *ospf;
1382
1383 if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE))
1384 zlog_debug("Zebra: Interface[%s] state change to up.",
1385 ifp->name);
1386
1387 /* Open per-intf write socket if configured */
1388 ospf = ifp->vrf->info;
1389 if (ospf && ospf->intf_socket_enabled)
1390 ospf_ifp_sock_init(ifp);
ddbf3e60 1391
85d25587 1392 ospf_if_recalculate_output_cost(ifp);
ddbf3e60 1393
85d25587 1394 if (oii && oii->curr_mtu != ifp->mtu) {
ddbf3e60
DS
1395 if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE))
1396 zlog_debug(
85d25587
DS
1397 "Zebra: Interface[%s] MTU change %u -> %u.",
1398 ifp->name, oii->curr_mtu, ifp->mtu);
ddbf3e60 1399
85d25587
DS
1400 oii->curr_mtu = ifp->mtu;
1401 /* Must reset the interface (simulate down/up) when MTU
1402 * changes. */
1403 ospf_if_reset(ifp);
ddbf3e60 1404
ddbf3e60
DS
1405 return 0;
1406 }
1407
ddbf3e60
DS
1408 for (rn = route_top(IF_OIFS(ifp)); rn; rn = route_next(rn)) {
1409 if ((oi = rn->info) == NULL)
1410 continue;
1411
1412 ospf_if_up(oi);
1413 }
1414
0b4124c1
KS
1415 if (HAS_LINK_PARAMS(ifp))
1416 ospf_mpls_te_update_if(ifp);
1417
138c5a74
DS
1418 return 0;
1419}
1420
1421static int ospf_ifp_down(struct interface *ifp)
1422{
b0b69e59
DS
1423 struct ospf_interface *oi;
1424 struct route_node *node;
1425
1426 if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE))
1427 zlog_debug("Zebra: Interface[%s] state change to down.",
1428 ifp->name);
1429
1430 for (node = route_top(IF_OIFS(ifp)); node; node = route_next(node)) {
1431 if ((oi = node->info) == NULL)
1432 continue;
1433 ospf_if_down(oi);
1434 }
1435
04a0401f
MS
1436 /* Close per-interface write socket if configured */
1437 ospf_ifp_sock_close(ifp);
1438
138c5a74
DS
1439 return 0;
1440}
1441
1442static int ospf_ifp_destroy(struct interface *ifp)
1443{
cbf15681 1444 struct ospf *ospf;
3c3c3252
DS
1445 struct route_node *rn;
1446
1447 if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE))
1448 zlog_debug(
1449 "Zebra: interface delete %s vrf %s[%u] index %d flags %llx metric %d mtu %d",
096f7609
IR
1450 ifp->name, ifp->vrf->name, ifp->vrf->vrf_id,
1451 ifp->ifindex, (unsigned long long)ifp->flags,
1452 ifp->metric, ifp->mtu);
3c3c3252
DS
1453
1454 hook_call(ospf_if_delete, ifp);
1455
096f7609 1456 ospf = ifp->vrf->info;
cbf15681 1457 if (ospf) {
cbf32f74 1458 if (ospf_if_count_area_params(ifp) > 0)
cbf15681 1459 ospf_interface_area_unset(ospf, ifp);
cbf15681
IR
1460 }
1461
3c3c3252
DS
1462 for (rn = route_top(IF_OIFS(ifp)); rn; rn = route_next(rn))
1463 if (rn->info)
1464 ospf_if_free((struct ospf_interface *)rn->info);
1465
138c5a74
DS
1466 return 0;
1467}
1468
be418160 1469/* Resetting ospf hello timer */
1470void ospf_reset_hello_timer(struct interface *ifp, struct in_addr addr,
1471 bool is_addr)
1472{
1473 struct route_node *rn;
1474
1475 if (is_addr) {
1476 struct prefix p;
1477 struct ospf_interface *oi = NULL;
1478
1479 p.u.prefix4 = addr;
1480 p.family = AF_INET;
46a8c343 1481 p.prefixlen = IPV4_MAX_BITLEN;
be418160 1482
1483 oi = ospf_if_table_lookup(ifp, &p);
1484
1485 if (oi) {
1486 /* Send hello before restart the hello timer
1487 * to avoid session flaps in case of bigger
1488 * hello interval configurations.
1489 */
1490 ospf_hello_send(oi);
1491
1492 /* Restart hello timer for this interface */
e16d030c 1493 EVENT_OFF(oi->t_hello);
be418160 1494 OSPF_HELLO_TIMER_ON(oi);
1495 }
1496
1497 return;
1498 }
1499
1500 for (rn = route_top(IF_OIFS(ifp)); rn; rn = route_next(rn)) {
1501 struct ospf_interface *oi = rn->info;
1502
1503 if (!oi)
1504 continue;
1505
1506 /* If hello interval configured on this oi, don't restart. */
1507 if (OSPF_IF_PARAM_CONFIGURED(oi->params, v_hello))
1508 continue;
1509
1510 /* Send hello before restart the hello timer
1511 * to avoid session flaps in case of bigger
1512 * hello interval configurations.
1513 */
1514 ospf_hello_send(oi);
1515
1516 /* Restart the hello timer. */
e16d030c 1517 EVENT_OFF(oi->t_hello);
be418160 1518 OSPF_HELLO_TIMER_ON(oi);
1519 }
1520}
1521
4d762f26 1522void ospf_if_init(void)
718e3744 1523{
138c5a74
DS
1524 if_zapi_callbacks(ospf_ifp_create, ospf_ifp_up,
1525 ospf_ifp_down, ospf_ifp_destroy);
1526
d62a17ae 1527 /* Initialize Zebra interface data structure. */
ce19a04a
DL
1528 hook_register_prio(if_add, 0, ospf_if_new_hook);
1529 hook_register_prio(if_del, 0, ospf_if_delete_hook);
718e3744 1530}