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