]> git.proxmox.com Git - mirror_frr.git/blob - ospfd/ospf_interface.c
*: Add camelCase JSON keys in addition to PascalCase
[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 specifed 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 OSPF_POLL_TIMER_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 OSPF_TIMER_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 for (rn = route_top(ospf->new_table); rn; rn = route_next(rn)) {
849 or = rn->info;
850
851 if (!or)
852 continue;
853
854 for (nh = listhead(or->paths); nh;
855 nh = listnextnode_unchecked(nh)) {
856 op = listgetdata(nh);
857 if (op->ifindex == oi->ifp->ifindex) {
858 or->changed = true;
859 break;
860 }
861 }
862 }
863
864 return 1;
865 }
866
867
868 /* Virtual Link related functions. */
869
870 struct ospf_vl_data *ospf_vl_data_new(struct ospf_area *area,
871 struct in_addr vl_peer)
872 {
873 struct ospf_vl_data *vl_data;
874
875 vl_data = XCALLOC(MTYPE_OSPF_VL_DATA, sizeof(struct ospf_vl_data));
876
877 vl_data->vl_peer.s_addr = vl_peer.s_addr;
878 vl_data->vl_area_id = area->area_id;
879 vl_data->vl_area_id_fmt = area->area_id_fmt;
880
881 return vl_data;
882 }
883
884 void ospf_vl_data_free(struct ospf_vl_data *vl_data)
885 {
886 XFREE(MTYPE_OSPF_VL_DATA, vl_data);
887 }
888
889 unsigned int vlink_count = 0;
890
891 struct ospf_interface *ospf_vl_new(struct ospf *ospf,
892 struct ospf_vl_data *vl_data)
893 {
894 struct ospf_interface *voi;
895 struct interface *vi;
896 char ifname[INTERFACE_NAMSIZ];
897 struct ospf_area *area;
898 struct in_addr area_id;
899 struct connected *co;
900 struct prefix_ipv4 *p;
901
902 if (IS_DEBUG_OSPF_EVENT)
903 zlog_debug("ospf_vl_new()(%s): Start", ospf_get_name(ospf));
904 if (vlink_count == OSPF_VL_MAX_COUNT) {
905 if (IS_DEBUG_OSPF_EVENT)
906 zlog_debug(
907 "ospf_vl_new(): Alarm: cannot create more than OSPF_MAX_VL_COUNT virtual links");
908 return NULL;
909 }
910
911 if (IS_DEBUG_OSPF_EVENT)
912 zlog_debug(
913 "ospf_vl_new(): creating pseudo zebra interface vrf id %u",
914 ospf->vrf_id);
915
916 snprintf(ifname, sizeof(ifname), "VLINK%u", vlink_count);
917 vi = if_get_by_name(ifname, ospf->vrf_id, ospf->name);
918 /*
919 * if_get_by_name sets ZEBRA_INTERFACE_LINKDETECTION
920 * virtual links don't need this.
921 */
922 UNSET_FLAG(vi->status, ZEBRA_INTERFACE_LINKDETECTION);
923 co = connected_new();
924 co->ifp = vi;
925 listnode_add(vi->connected, co);
926
927 p = prefix_ipv4_new();
928 p->family = AF_INET;
929 p->prefix.s_addr = INADDR_ANY;
930 p->prefixlen = 0;
931
932 co->address = (struct prefix *)p;
933
934 voi = ospf_if_new(ospf, vi, co->address);
935 if (voi == NULL) {
936 if (IS_DEBUG_OSPF_EVENT)
937 zlog_debug(
938 "ospf_vl_new(): Alarm: OSPF int structure is not created");
939 return NULL;
940 }
941 voi->connected = co;
942 voi->vl_data = vl_data;
943 voi->ifp->mtu = OSPF_VL_MTU;
944 voi->type = OSPF_IFTYPE_VIRTUALLINK;
945
946 vlink_count++;
947 if (IS_DEBUG_OSPF_EVENT)
948 zlog_debug("ospf_vl_new(): Created name: %s", ifname);
949 if (IS_DEBUG_OSPF_EVENT)
950 zlog_debug("ospf_vl_new(): set if->name to %s", vi->name);
951
952 area_id.s_addr = INADDR_ANY;
953 area = ospf_area_get(ospf, area_id);
954 voi->area = area;
955
956 if (IS_DEBUG_OSPF_EVENT)
957 zlog_debug(
958 "ospf_vl_new(): set associated area to the backbone");
959
960 /* Add pseudo neighbor. */
961 ospf_nbr_self_reset(voi, voi->ospf->router_id);
962
963 ospf_area_add_if(voi->area, voi);
964
965 if (IS_DEBUG_OSPF_EVENT)
966 zlog_debug("ospf_vl_new(): Stop");
967 return voi;
968 }
969
970 static void ospf_vl_if_delete(struct ospf_vl_data *vl_data)
971 {
972 struct interface *ifp = vl_data->vl_oi->ifp;
973 struct vrf *vrf = ifp->vrf;
974
975 vl_data->vl_oi->address->u.prefix4.s_addr = INADDR_ANY;
976 vl_data->vl_oi->address->prefixlen = 0;
977 ospf_if_free(vl_data->vl_oi);
978 if_delete(&ifp);
979 if (!vrf_is_enabled(vrf))
980 vrf_delete(vrf);
981 vlink_count--;
982 }
983
984 /* for a defined area, count the number of configured vl
985 */
986 int ospf_vl_count(struct ospf *ospf, struct ospf_area *area)
987 {
988 int count = 0;
989 struct ospf_vl_data *vl_data;
990 struct listnode *node;
991
992 for (ALL_LIST_ELEMENTS_RO(ospf->vlinks, node, vl_data)) {
993 if (area
994 && !IPV4_ADDR_SAME(&vl_data->vl_area_id, &area->area_id))
995 continue;
996 count++;
997 }
998 return count;
999 }
1000
1001 /* Look up vl_data for given peer, optionally qualified to be in the
1002 * specified area. NULL area returns first found..
1003 */
1004 struct ospf_vl_data *ospf_vl_lookup(struct ospf *ospf, struct ospf_area *area,
1005 struct in_addr vl_peer)
1006 {
1007 struct ospf_vl_data *vl_data;
1008 struct listnode *node;
1009
1010 if (IS_DEBUG_OSPF_EVENT) {
1011 zlog_debug("%s: Looking for %pI4", __func__, &vl_peer);
1012 if (area)
1013 zlog_debug("%s: in area %pI4", __func__,
1014 &area->area_id);
1015 }
1016
1017 for (ALL_LIST_ELEMENTS_RO(ospf->vlinks, node, vl_data)) {
1018 if (IS_DEBUG_OSPF_EVENT)
1019 zlog_debug("%s: VL %s, peer %pI4", __func__,
1020 vl_data->vl_oi->ifp->name,
1021 &vl_data->vl_peer);
1022
1023 if (area
1024 && !IPV4_ADDR_SAME(&vl_data->vl_area_id, &area->area_id))
1025 continue;
1026
1027 if (IPV4_ADDR_SAME(&vl_data->vl_peer, &vl_peer))
1028 return vl_data;
1029 }
1030
1031 return NULL;
1032 }
1033
1034 static void ospf_vl_shutdown(struct ospf_vl_data *vl_data)
1035 {
1036 struct ospf_interface *oi;
1037
1038 if ((oi = vl_data->vl_oi) == NULL)
1039 return;
1040
1041 oi->address->u.prefix4.s_addr = INADDR_ANY;
1042 oi->address->prefixlen = 0;
1043
1044 UNSET_FLAG(oi->ifp->flags, IFF_UP);
1045 /* OSPF_ISM_EVENT_SCHEDULE (oi, ISM_InterfaceDown); */
1046 OSPF_ISM_EVENT_EXECUTE(oi, ISM_InterfaceDown);
1047 }
1048
1049 void ospf_vl_add(struct ospf *ospf, struct ospf_vl_data *vl_data)
1050 {
1051 listnode_add(ospf->vlinks, vl_data);
1052 hook_call(ospf_vl_add, vl_data);
1053 }
1054
1055 void ospf_vl_delete(struct ospf *ospf, struct ospf_vl_data *vl_data)
1056 {
1057 ospf_vl_shutdown(vl_data);
1058 ospf_vl_if_delete(vl_data);
1059
1060 hook_call(ospf_vl_delete, vl_data);
1061 listnode_delete(ospf->vlinks, vl_data);
1062
1063 ospf_vl_data_free(vl_data);
1064 }
1065
1066 static int ospf_vl_set_params(struct ospf_area *area,
1067 struct ospf_vl_data *vl_data, struct vertex *v)
1068 {
1069 int changed = 0;
1070 struct ospf_interface *voi;
1071 struct listnode *node;
1072 struct vertex_parent *vp = NULL;
1073 unsigned int i;
1074 struct router_lsa *rl;
1075 struct ospf_interface *oi;
1076
1077 voi = vl_data->vl_oi;
1078
1079 if (voi->output_cost != v->distance) {
1080
1081 voi->output_cost = v->distance;
1082 changed = 1;
1083 }
1084
1085 for (ALL_LIST_ELEMENTS_RO(v->parents, node, vp)) {
1086 vl_data->nexthop.lsa_pos = vp->nexthop->lsa_pos;
1087 vl_data->nexthop.router = vp->nexthop->router;
1088
1089 /*
1090 * Only deal with interface data when the local
1091 * (calculating) node is the SPF root node
1092 */
1093 if (!area->spf_dry_run) {
1094 oi = ospf_if_lookup_by_lsa_pos(
1095 area, vl_data->nexthop.lsa_pos);
1096
1097 if (!IPV4_ADDR_SAME(&voi->address->u.prefix4,
1098 &oi->address->u.prefix4))
1099 changed = 1;
1100
1101 voi->address->u.prefix4 = oi->address->u.prefix4;
1102 voi->address->prefixlen = oi->address->prefixlen;
1103 }
1104
1105 break; /* We take the first interface. */
1106 }
1107
1108 rl = (struct router_lsa *)v->lsa;
1109
1110 /* use SPF determined backlink index in struct vertex
1111 * for virtual link destination address
1112 */
1113 if (vp && vp->backlink >= 0) {
1114 if (!IPV4_ADDR_SAME(&vl_data->peer_addr,
1115 &rl->link[vp->backlink].link_data))
1116 changed = 1;
1117 vl_data->peer_addr = rl->link[vp->backlink].link_data;
1118 } else {
1119 /* This is highly odd, there is no backlink index
1120 * there should be due to the ospf_spf_has_link() check
1121 * in SPF. Lets warn and try pick a link anyway.
1122 */
1123 zlog_info("ospf_vl_set_params: No backlink for %s!",
1124 vl_data->vl_oi->ifp->name);
1125 for (i = 0; i < ntohs(rl->links); i++) {
1126 switch (rl->link[i].type) {
1127 case LSA_LINK_TYPE_VIRTUALLINK:
1128 if (IS_DEBUG_OSPF_EVENT)
1129 zlog_debug(
1130 "found back link through VL");
1131 /* fallthru */
1132 case LSA_LINK_TYPE_TRANSIT:
1133 case LSA_LINK_TYPE_POINTOPOINT:
1134 if (!IPV4_ADDR_SAME(&vl_data->peer_addr,
1135 &rl->link[i].link_data))
1136 changed = 1;
1137 vl_data->peer_addr = rl->link[i].link_data;
1138 }
1139 }
1140 }
1141
1142 if (IS_DEBUG_OSPF_EVENT)
1143 zlog_debug("%s: %s peer address: %pI4, cost: %d,%schanged",
1144 __func__, vl_data->vl_oi->ifp->name,
1145 &vl_data->peer_addr, voi->output_cost,
1146 (changed ? " " : " un"));
1147
1148 return changed;
1149 }
1150
1151
1152 void ospf_vl_up_check(struct ospf_area *area, struct in_addr rid,
1153 struct vertex *v)
1154 {
1155 struct ospf *ospf = area->ospf;
1156 struct listnode *node;
1157 struct ospf_vl_data *vl_data;
1158 struct ospf_interface *oi;
1159
1160 if (IS_DEBUG_OSPF_EVENT) {
1161 zlog_debug("ospf_vl_up_check(): Start");
1162 zlog_debug("ospf_vl_up_check(): Router ID is %pI4",
1163 &rid);
1164 zlog_debug("ospf_vl_up_check(): Area is %pI4",
1165 &area->area_id);
1166 }
1167
1168 for (ALL_LIST_ELEMENTS_RO(ospf->vlinks, node, vl_data)) {
1169 if (IS_DEBUG_OSPF_EVENT) {
1170 zlog_debug("%s: considering VL, %s in area %pI4",
1171 __func__, vl_data->vl_oi->ifp->name,
1172 &vl_data->vl_area_id);
1173 zlog_debug("%s: peer ID: %pI4", __func__,
1174 &vl_data->vl_peer);
1175 }
1176
1177 if (IPV4_ADDR_SAME(&vl_data->vl_peer, &rid)
1178 && IPV4_ADDR_SAME(&vl_data->vl_area_id, &area->area_id)) {
1179 oi = vl_data->vl_oi;
1180 SET_FLAG(vl_data->flags, OSPF_VL_FLAG_APPROVED);
1181
1182 if (IS_DEBUG_OSPF_EVENT)
1183 zlog_debug(
1184 "ospf_vl_up_check(): this VL matched");
1185
1186 if (oi->state == ISM_Down) {
1187 if (IS_DEBUG_OSPF_EVENT)
1188 zlog_debug(
1189 "ospf_vl_up_check(): VL is down, waking it up");
1190 SET_FLAG(oi->ifp->flags, IFF_UP);
1191 OSPF_ISM_EVENT_EXECUTE(oi, ISM_InterfaceUp);
1192 }
1193
1194 if (ospf_vl_set_params(area, vl_data, v)) {
1195 if (IS_DEBUG_OSPF(ism, ISM_EVENTS))
1196 zlog_debug(
1197 "ospf_vl_up_check: VL cost change, scheduling router lsa refresh");
1198 if (ospf->backbone)
1199 ospf_router_lsa_update_area(
1200 ospf->backbone);
1201 else if (IS_DEBUG_OSPF(ism, ISM_EVENTS))
1202 zlog_debug(
1203 "ospf_vl_up_check: VL cost change, no backbone!");
1204 }
1205 }
1206 }
1207 }
1208
1209 void ospf_vl_unapprove(struct ospf *ospf)
1210 {
1211 struct listnode *node;
1212 struct ospf_vl_data *vl_data;
1213
1214 for (ALL_LIST_ELEMENTS_RO(ospf->vlinks, node, vl_data))
1215 UNSET_FLAG(vl_data->flags, OSPF_VL_FLAG_APPROVED);
1216 }
1217
1218 void ospf_vl_shut_unapproved(struct ospf *ospf)
1219 {
1220 struct listnode *node, *nnode;
1221 struct ospf_vl_data *vl_data;
1222
1223 for (ALL_LIST_ELEMENTS(ospf->vlinks, node, nnode, vl_data))
1224 if (!CHECK_FLAG(vl_data->flags, OSPF_VL_FLAG_APPROVED))
1225 ospf_vl_shutdown(vl_data);
1226 }
1227
1228 int ospf_full_virtual_nbrs(struct ospf_area *area)
1229 {
1230 if (IS_DEBUG_OSPF_EVENT) {
1231 zlog_debug(
1232 "counting fully adjacent virtual neighbors in area %pI4",
1233 &area->area_id);
1234 zlog_debug("there are %d of them", area->full_vls);
1235 }
1236
1237 return area->full_vls;
1238 }
1239
1240 int ospf_vls_in_area(struct ospf_area *area)
1241 {
1242 struct listnode *node;
1243 struct ospf_vl_data *vl_data;
1244 int c = 0;
1245
1246 for (ALL_LIST_ELEMENTS_RO(area->ospf->vlinks, node, vl_data))
1247 if (IPV4_ADDR_SAME(&vl_data->vl_area_id, &area->area_id))
1248 c++;
1249
1250 return c;
1251 }
1252
1253
1254 struct crypt_key *ospf_crypt_key_new(void)
1255 {
1256 return XCALLOC(MTYPE_OSPF_CRYPT_KEY, sizeof(struct crypt_key));
1257 }
1258
1259 void ospf_crypt_key_add(struct list *crypt, struct crypt_key *ck)
1260 {
1261 listnode_add(crypt, ck);
1262 }
1263
1264 struct crypt_key *ospf_crypt_key_lookup(struct list *auth_crypt, uint8_t key_id)
1265 {
1266 struct listnode *node;
1267 struct crypt_key *ck;
1268
1269 for (ALL_LIST_ELEMENTS_RO(auth_crypt, node, ck))
1270 if (ck->key_id == key_id)
1271 return ck;
1272
1273 return NULL;
1274 }
1275
1276 int ospf_crypt_key_delete(struct list *auth_crypt, uint8_t key_id)
1277 {
1278 struct listnode *node, *nnode;
1279 struct crypt_key *ck;
1280
1281 for (ALL_LIST_ELEMENTS(auth_crypt, node, nnode, ck)) {
1282 if (ck->key_id == key_id) {
1283 listnode_delete(auth_crypt, ck);
1284 XFREE(MTYPE_OSPF_CRYPT_KEY, ck);
1285 return 1;
1286 }
1287 }
1288
1289 return 0;
1290 }
1291
1292 uint8_t ospf_default_iftype(struct interface *ifp)
1293 {
1294 if (if_is_pointopoint(ifp))
1295 return OSPF_IFTYPE_POINTOPOINT;
1296 else if (if_is_loopback(ifp))
1297 return OSPF_IFTYPE_LOOPBACK;
1298 else
1299 return OSPF_IFTYPE_BROADCAST;
1300 }
1301
1302 void ospf_if_interface(struct interface *ifp)
1303 {
1304 hook_call(ospf_if_update, ifp);
1305 }
1306
1307 uint32_t ospf_if_count_area_params(struct interface *ifp)
1308 {
1309 struct ospf_if_params *params;
1310 struct route_node *rn;
1311 uint32_t count = 0;
1312
1313 params = IF_DEF_PARAMS(ifp);
1314 if (OSPF_IF_PARAM_CONFIGURED(params, if_area))
1315 count++;
1316
1317 for (rn = route_top(IF_OIFS_PARAMS(ifp)); rn; rn = route_next(rn))
1318 if ((params = rn->info)
1319 && OSPF_IF_PARAM_CONFIGURED(params, if_area))
1320 count++;
1321
1322 return count;
1323 }
1324
1325 static int ospf_ifp_create(struct interface *ifp)
1326 {
1327 struct ospf *ospf = NULL;
1328 struct ospf_if_info *oii;
1329
1330 if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE))
1331 zlog_debug(
1332 "Zebra: interface add %s vrf %s[%u] index %d flags %llx metric %d mtu %d speed %u",
1333 ifp->name, ifp->vrf->name, ifp->vrf->vrf_id,
1334 ifp->ifindex, (unsigned long long)ifp->flags,
1335 ifp->metric, ifp->mtu, ifp->speed);
1336
1337 assert(ifp->info);
1338
1339 oii = ifp->info;
1340 oii->curr_mtu = ifp->mtu;
1341
1342 if (IF_DEF_PARAMS(ifp)
1343 && !OSPF_IF_PARAM_CONFIGURED(IF_DEF_PARAMS(ifp), type)) {
1344 SET_IF_PARAM(IF_DEF_PARAMS(ifp), type);
1345 IF_DEF_PARAMS(ifp)->type = ospf_default_iftype(ifp);
1346 }
1347
1348 ospf = ifp->vrf->info;
1349 if (!ospf)
1350 return 0;
1351
1352 if (ospf_if_count_area_params(ifp) > 0)
1353 ospf_interface_area_set(ospf, ifp);
1354
1355 ospf_if_recalculate_output_cost(ifp);
1356
1357 ospf_if_update(ospf, ifp);
1358
1359 if (HAS_LINK_PARAMS(ifp))
1360 ospf_mpls_te_update_if(ifp);
1361
1362 hook_call(ospf_if_update, ifp);
1363
1364 return 0;
1365 }
1366
1367 static int ospf_ifp_up(struct interface *ifp)
1368 {
1369 struct ospf_interface *oi;
1370 struct route_node *rn;
1371 struct ospf_if_info *oii = ifp->info;
1372
1373 ospf_if_recalculate_output_cost(ifp);
1374
1375 if (oii && oii->curr_mtu != ifp->mtu) {
1376 if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE))
1377 zlog_debug(
1378 "Zebra: Interface[%s] MTU change %u -> %u.",
1379 ifp->name, oii->curr_mtu, ifp->mtu);
1380
1381 oii->curr_mtu = ifp->mtu;
1382 /* Must reset the interface (simulate down/up) when MTU
1383 * changes. */
1384 ospf_if_reset(ifp);
1385
1386 return 0;
1387 }
1388
1389 if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE))
1390 zlog_debug("Zebra: Interface[%s] state change to up.",
1391 ifp->name);
1392
1393 for (rn = route_top(IF_OIFS(ifp)); rn; rn = route_next(rn)) {
1394 if ((oi = rn->info) == NULL)
1395 continue;
1396
1397 ospf_if_up(oi);
1398 }
1399
1400 if (HAS_LINK_PARAMS(ifp))
1401 ospf_mpls_te_update_if(ifp);
1402
1403 return 0;
1404 }
1405
1406 static int ospf_ifp_down(struct interface *ifp)
1407 {
1408 struct ospf_interface *oi;
1409 struct route_node *node;
1410
1411 if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE))
1412 zlog_debug("Zebra: Interface[%s] state change to down.",
1413 ifp->name);
1414
1415 for (node = route_top(IF_OIFS(ifp)); node; node = route_next(node)) {
1416 if ((oi = node->info) == NULL)
1417 continue;
1418 ospf_if_down(oi);
1419 }
1420
1421 return 0;
1422 }
1423
1424 static int ospf_ifp_destroy(struct interface *ifp)
1425 {
1426 struct ospf *ospf;
1427 struct route_node *rn;
1428
1429 if (IS_DEBUG_OSPF(zebra, ZEBRA_INTERFACE))
1430 zlog_debug(
1431 "Zebra: interface delete %s vrf %s[%u] index %d flags %llx metric %d mtu %d",
1432 ifp->name, ifp->vrf->name, ifp->vrf->vrf_id,
1433 ifp->ifindex, (unsigned long long)ifp->flags,
1434 ifp->metric, ifp->mtu);
1435
1436 hook_call(ospf_if_delete, ifp);
1437
1438 ospf = ifp->vrf->info;
1439 if (ospf) {
1440 if (ospf_if_count_area_params(ifp) > 0)
1441 ospf_interface_area_unset(ospf, ifp);
1442 }
1443
1444 for (rn = route_top(IF_OIFS(ifp)); rn; rn = route_next(rn))
1445 if (rn->info)
1446 ospf_if_free((struct ospf_interface *)rn->info);
1447
1448 return 0;
1449 }
1450
1451 /* Resetting ospf hello timer */
1452 void ospf_reset_hello_timer(struct interface *ifp, struct in_addr addr,
1453 bool is_addr)
1454 {
1455 struct route_node *rn;
1456
1457 if (is_addr) {
1458 struct prefix p;
1459 struct ospf_interface *oi = NULL;
1460
1461 p.u.prefix4 = addr;
1462 p.family = AF_INET;
1463 p.prefixlen = IPV4_MAX_BITLEN;
1464
1465 oi = ospf_if_table_lookup(ifp, &p);
1466
1467 if (oi) {
1468 /* Send hello before restart the hello timer
1469 * to avoid session flaps in case of bigger
1470 * hello interval configurations.
1471 */
1472 ospf_hello_send(oi);
1473
1474 /* Restart hello timer for this interface */
1475 OSPF_ISM_TIMER_OFF(oi->t_hello);
1476 OSPF_HELLO_TIMER_ON(oi);
1477 }
1478
1479 return;
1480 }
1481
1482 for (rn = route_top(IF_OIFS(ifp)); rn; rn = route_next(rn)) {
1483 struct ospf_interface *oi = rn->info;
1484
1485 if (!oi)
1486 continue;
1487
1488 /* If hello interval configured on this oi, don't restart. */
1489 if (OSPF_IF_PARAM_CONFIGURED(oi->params, v_hello))
1490 continue;
1491
1492 /* Send hello before restart the hello timer
1493 * to avoid session flaps in case of bigger
1494 * hello interval configurations.
1495 */
1496 ospf_hello_send(oi);
1497
1498 /* Restart the hello timer. */
1499 OSPF_ISM_TIMER_OFF(oi->t_hello);
1500 OSPF_HELLO_TIMER_ON(oi);
1501 }
1502 }
1503
1504 void ospf_if_init(void)
1505 {
1506 if_zapi_callbacks(ospf_ifp_create, ospf_ifp_up,
1507 ospf_ifp_down, ospf_ifp_destroy);
1508
1509 /* Initialize Zebra interface data structure. */
1510 hook_register_prio(if_add, 0, ospf_if_new_hook);
1511 hook_register_prio(if_del, 0, ospf_if_delete_hook);
1512 }