]> git.proxmox.com Git - mirror_frr.git/blob - ospfd/ospf_interface.c
Merge pull request #2483 from pacovn/clang_scan_bgpd_updgrp_deref
[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
36 #include "ospfd/ospfd.h"
37 #include "ospfd/ospf_spf.h"
38 #include "ospfd/ospf_interface.h"
39 #include "ospfd/ospf_ism.h"
40 #include "ospfd/ospf_asbr.h"
41 #include "ospfd/ospf_lsa.h"
42 #include "ospfd/ospf_lsdb.h"
43 #include "ospfd/ospf_neighbor.h"
44 #include "ospfd/ospf_nsm.h"
45 #include "ospfd/ospf_packet.h"
46 #include "ospfd/ospf_abr.h"
47 #include "ospfd/ospf_network.h"
48 #include "ospfd/ospf_dump.h"
49
50 DEFINE_QOBJ_TYPE(ospf_interface)
51 DEFINE_HOOK(ospf_vl_add, (struct ospf_vl_data * vd), (vd))
52 DEFINE_HOOK(ospf_vl_delete, (struct ospf_vl_data * vd), (vd))
53
54 int ospf_interface_neighbor_count(struct ospf_interface *oi)
55 {
56 int count = 0;
57 struct route_node *rn;
58 struct ospf_neighbor *nbr = NULL;
59
60 for (rn = route_top(oi->nbrs); rn; rn = route_next(rn)) {
61 nbr = rn->info;
62 if (nbr) {
63 /* Do not show myself. */
64 if (nbr == oi->nbr_self)
65 continue;
66 /* Down state is not shown. */
67 if (nbr->state == NSM_Down)
68 continue;
69 count++;
70 }
71 }
72
73 return count;
74 }
75
76 int ospf_if_get_output_cost(struct ospf_interface *oi)
77 {
78 /* If all else fails, use default OSPF cost */
79 uint32_t cost;
80 uint32_t bw, refbw;
81
82 /* ifp speed and bw can be 0 in some platforms, use ospf default bw
83 if bw is configured under interface it would be used.
84 */
85 if (!oi->ifp->bandwidth && oi->ifp->speed)
86 bw = oi->ifp->speed;
87 else
88 bw = oi->ifp->bandwidth ? oi->ifp->bandwidth
89 : OSPF_DEFAULT_BANDWIDTH;
90 refbw = oi->ospf->ref_bandwidth;
91
92 /* A specifed ip ospf cost overrides a calculated one. */
93 if (OSPF_IF_PARAM_CONFIGURED(IF_DEF_PARAMS(oi->ifp), output_cost_cmd)
94 || OSPF_IF_PARAM_CONFIGURED(oi->params, output_cost_cmd))
95 cost = OSPF_IF_PARAM(oi, output_cost_cmd);
96 /* See if a cost can be calculated from the zebra processes
97 interface bandwidth field. */
98 else {
99 cost = (uint32_t)((double)refbw / (double)bw + (double)0.5);
100 if (cost < 1)
101 cost = 1;
102 else if (cost > 65535)
103 cost = 65535;
104 }
105
106 return cost;
107 }
108
109 void ospf_if_recalculate_output_cost(struct interface *ifp)
110 {
111 uint32_t newcost;
112 struct route_node *rn;
113
114 for (rn = route_top(IF_OIFS(ifp)); rn; rn = route_next(rn)) {
115 struct ospf_interface *oi;
116
117 if ((oi = rn->info) == NULL)
118 continue;
119
120 newcost = ospf_if_get_output_cost(oi);
121
122 /* Is actual output cost changed? */
123 if (oi->output_cost != newcost) {
124 oi->output_cost = newcost;
125 ospf_router_lsa_update_area(oi->area);
126 }
127 }
128 }
129
130 /* Simulate down/up on the interface. This is needed, for example, when
131 the MTU changes. */
132 void ospf_if_reset(struct interface *ifp)
133 {
134 struct route_node *rn;
135
136 for (rn = route_top(IF_OIFS(ifp)); rn; rn = route_next(rn)) {
137 struct ospf_interface *oi;
138
139 if ((oi = rn->info) == NULL)
140 continue;
141
142 ospf_if_down(oi);
143 ospf_if_up(oi);
144 }
145 }
146
147 void ospf_if_reset_variables(struct ospf_interface *oi)
148 {
149 /* Set default values. */
150 /* don't clear this flag. oi->flag = OSPF_IF_DISABLE; */
151
152 if (oi->vl_data)
153 oi->type = OSPF_IFTYPE_VIRTUALLINK;
154 else
155 /* preserve network-type */
156 if (oi->type != OSPF_IFTYPE_NBMA)
157 oi->type = OSPF_IFTYPE_BROADCAST;
158
159 oi->state = ISM_Down;
160
161 oi->crypt_seqnum = 0;
162
163 /* This must be short, (less than RxmtInterval)
164 - RFC 2328 Section 13.5 para 3. Set to 1 second to avoid Acks being
165 held back for too long - MAG */
166 oi->v_ls_ack = 1;
167 }
168
169 /* lookup oi for specified prefix/ifp */
170 struct ospf_interface *ospf_if_table_lookup(struct interface *ifp,
171 struct prefix *prefix)
172 {
173 struct prefix p;
174 struct route_node *rn;
175 struct ospf_interface *rninfo = NULL;
176
177 p = *prefix;
178 p.prefixlen = IPV4_MAX_PREFIXLEN;
179
180 /* route_node_get implicitely locks */
181 if ((rn = route_node_lookup(IF_OIFS(ifp), &p))) {
182 rninfo = (struct ospf_interface *)rn->info;
183 route_unlock_node(rn);
184 }
185
186 return rninfo;
187 }
188
189 static void ospf_add_to_if(struct interface *ifp, struct ospf_interface *oi)
190 {
191 struct route_node *rn;
192 struct prefix p;
193
194 p = *oi->address;
195 p.prefixlen = IPV4_MAX_PREFIXLEN;
196 apply_mask(&p);
197
198 rn = route_node_get(IF_OIFS(ifp), &p);
199 /* rn->info should either be NULL or equal to this oi
200 * as route_node_get may return an existing node
201 */
202 assert(!rn->info || rn->info == oi);
203 rn->info = oi;
204 }
205
206 static void ospf_delete_from_if(struct interface *ifp,
207 struct ospf_interface *oi)
208 {
209 struct route_node *rn;
210 struct prefix p;
211
212 p = *oi->address;
213 p.prefixlen = IPV4_MAX_PREFIXLEN;
214
215 rn = route_node_lookup(IF_OIFS(oi->ifp), &p);
216 assert(rn);
217 assert(rn->info);
218 rn->info = NULL;
219 route_unlock_node(rn);
220 route_unlock_node(rn);
221 }
222
223 struct ospf_interface *ospf_if_new(struct ospf *ospf, struct interface *ifp,
224 struct prefix *p)
225 {
226 struct ospf_interface *oi;
227
228 if ((oi = ospf_if_table_lookup(ifp, p)) == NULL) {
229 oi = XCALLOC(MTYPE_OSPF_IF, sizeof(struct ospf_interface));
230 memset(oi, 0, sizeof(struct ospf_interface));
231 } else
232 return oi;
233
234 /* Set zebra interface pointer. */
235 oi->ifp = ifp;
236 oi->address = p;
237
238 ospf_add_to_if(ifp, oi);
239 listnode_add(ospf->oiflist, oi);
240
241 /* Initialize neighbor list. */
242 oi->nbrs = route_table_init();
243
244 /* Initialize static neighbor list. */
245 oi->nbr_nbma = list_new();
246
247 /* Initialize Link State Acknowledgment list. */
248 oi->ls_ack = list_new();
249 oi->ls_ack_direct.ls_ack = list_new();
250
251 /* Set default values. */
252 ospf_if_reset_variables(oi);
253
254 /* Set pseudo neighbor to Null */
255 oi->nbr_self = NULL;
256
257 oi->ls_upd_queue = route_table_init();
258 oi->t_ls_upd_event = NULL;
259 oi->t_ls_ack_direct = NULL;
260
261 oi->crypt_seqnum = time(NULL);
262
263 ospf_opaque_type9_lsa_init(oi);
264
265 oi->ospf = ospf;
266
267 ospf_if_stream_set(oi);
268
269 QOBJ_REG(oi, ospf_interface);
270
271 if (IS_DEBUG_OSPF_EVENT)
272 zlog_debug("%s: ospf interface %s vrf %s id %u created",
273 __PRETTY_FUNCTION__, ifp->name,
274 ospf_vrf_id_to_name(ospf->vrf_id), ospf->vrf_id);
275
276 return oi;
277 }
278
279 /* Restore an interface to its pre UP state
280 Used from ism_interface_down only */
281 void ospf_if_cleanup(struct ospf_interface *oi)
282 {
283 struct route_node *rn;
284 struct listnode *node, *nnode;
285 struct ospf_neighbor *nbr;
286 struct ospf_nbr_nbma *nbr_nbma;
287 struct ospf_lsa *lsa;
288
289 /* oi->nbrs and oi->nbr_nbma should be deleted on InterfaceDown event */
290 /* delete all static neighbors attached to this interface */
291 for (ALL_LIST_ELEMENTS(oi->nbr_nbma, node, nnode, nbr_nbma)) {
292 OSPF_POLL_TIMER_OFF(nbr_nbma->t_poll);
293
294 if (nbr_nbma->nbr) {
295 nbr_nbma->nbr->nbr_nbma = NULL;
296 nbr_nbma->nbr = NULL;
297 }
298
299 nbr_nbma->oi = NULL;
300
301 listnode_delete(oi->nbr_nbma, nbr_nbma);
302 }
303
304 /* send Neighbor event KillNbr to all associated neighbors. */
305 for (rn = route_top(oi->nbrs); rn; rn = route_next(rn))
306 if ((nbr = rn->info) != NULL)
307 if (nbr != oi->nbr_self)
308 OSPF_NSM_EVENT_EXECUTE(nbr, NSM_KillNbr);
309
310 /* Cleanup Link State Acknowlegdment list. */
311 for (ALL_LIST_ELEMENTS(oi->ls_ack, node, nnode, lsa))
312 ospf_lsa_unlock(&lsa); /* oi->ls_ack */
313 list_delete_all_node(oi->ls_ack);
314
315 oi->crypt_seqnum = 0;
316
317 /* Empty link state update queue */
318 ospf_ls_upd_queue_empty(oi);
319
320 /* Reset pseudo neighbor. */
321 ospf_nbr_self_reset(oi, oi->ospf->router_id);
322 }
323
324 void ospf_if_free(struct ospf_interface *oi)
325 {
326 ospf_if_down(oi);
327
328 if (oi->obuf)
329 ospf_fifo_free(oi->obuf);
330
331 assert(oi->state == ISM_Down);
332
333 ospf_opaque_type9_lsa_term(oi);
334
335 QOBJ_UNREG(oi);
336
337 /* Free Pseudo Neighbour */
338 ospf_nbr_delete(oi->nbr_self);
339
340 route_table_finish(oi->nbrs);
341 route_table_finish(oi->ls_upd_queue);
342
343 /* Free any lists that should be freed */
344 list_delete_and_null(&oi->nbr_nbma);
345
346 list_delete_and_null(&oi->ls_ack);
347 list_delete_and_null(&oi->ls_ack_direct.ls_ack);
348
349 if (IS_DEBUG_OSPF_EVENT)
350 zlog_debug("%s: ospf interface %s vrf %s id %u deleted",
351 __PRETTY_FUNCTION__, oi->ifp->name,
352 ospf_vrf_id_to_name(oi->ifp->vrf_id),
353 oi->ifp->vrf_id);
354
355 ospf_delete_from_if(oi->ifp, oi);
356
357 listnode_delete(oi->ospf->oiflist, oi);
358 listnode_delete(oi->area->oiflist, oi);
359
360 thread_cancel_event(master, oi);
361
362 memset(oi, 0, sizeof(*oi));
363 XFREE(MTYPE_OSPF_IF, oi);
364 }
365
366 int ospf_if_is_up(struct ospf_interface *oi)
367 {
368 return if_is_up(oi->ifp);
369 }
370
371 struct ospf_interface *ospf_if_exists(struct ospf_interface *oic)
372 {
373 struct listnode *node;
374 struct ospf *ospf;
375 struct ospf_interface *oi;
376
377 if (!oic)
378 return NULL;
379
380 ospf = oic->ospf;
381 if (ospf == NULL)
382 return NULL;
383
384 for (ALL_LIST_ELEMENTS_RO(ospf->oiflist, node, oi))
385 if (oi == oic)
386 return oi;
387
388 return NULL;
389 }
390
391 /* Lookup OSPF interface by router LSA posistion */
392 struct ospf_interface *ospf_if_lookup_by_lsa_pos(struct ospf_area *area,
393 int lsa_pos)
394 {
395 struct listnode *node;
396 struct ospf_interface *oi;
397
398 for (ALL_LIST_ELEMENTS_RO(area->oiflist, node, oi)) {
399 if (lsa_pos >= oi->lsa_pos_beg && lsa_pos < oi->lsa_pos_end)
400 return oi;
401 }
402 return NULL;
403 }
404
405 struct ospf_interface *ospf_if_lookup_by_local_addr(struct ospf *ospf,
406 struct interface *ifp,
407 struct in_addr address)
408 {
409 struct listnode *node;
410 struct ospf_interface *oi;
411
412 for (ALL_LIST_ELEMENTS_RO(ospf->oiflist, node, oi))
413 if (oi->type != OSPF_IFTYPE_VIRTUALLINK) {
414 if (ifp && oi->ifp != ifp)
415 continue;
416
417 if (IPV4_ADDR_SAME(&address, &oi->address->u.prefix4))
418 return oi;
419 }
420
421 return NULL;
422 }
423
424 struct ospf_interface *ospf_if_lookup_by_prefix(struct ospf *ospf,
425 struct prefix_ipv4 *p)
426 {
427 struct listnode *node;
428 struct ospf_interface *oi;
429
430 /* Check each Interface. */
431 for (ALL_LIST_ELEMENTS_RO(ospf->oiflist, node, oi)) {
432 if (oi->type != OSPF_IFTYPE_VIRTUALLINK) {
433 struct prefix ptmp;
434
435 prefix_copy(&ptmp, CONNECTED_PREFIX(oi->connected));
436 apply_mask(&ptmp);
437 if (prefix_same(&ptmp, (struct prefix *)p))
438 return oi;
439 }
440 }
441 return NULL;
442 }
443
444 /* determine receiving interface by ifp and source address */
445 struct ospf_interface *ospf_if_lookup_recv_if(struct ospf *ospf,
446 struct in_addr src,
447 struct interface *ifp)
448 {
449 struct route_node *rn;
450 struct prefix_ipv4 addr;
451 struct ospf_interface *oi, *match;
452
453 addr.family = AF_INET;
454 addr.prefix = src;
455 addr.prefixlen = IPV4_MAX_BITLEN;
456
457 match = NULL;
458
459 for (rn = route_top(IF_OIFS(ifp)); rn; rn = route_next(rn)) {
460 oi = rn->info;
461
462 if (!oi) /* oi can be NULL for PtP aliases */
463 continue;
464
465 if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
466 continue;
467
468 if (if_is_loopback(oi->ifp) || if_is_vrf(oi->ifp))
469 continue;
470
471 if (CHECK_FLAG(oi->connected->flags, ZEBRA_IFA_UNNUMBERED))
472 match = oi;
473 else if (prefix_match(CONNECTED_PREFIX(oi->connected),
474 (struct prefix *)&addr)) {
475 if ((match == NULL) || (match->address->prefixlen
476 < oi->address->prefixlen))
477 match = oi;
478 }
479 }
480
481 return match;
482 }
483
484 static void ospf_if_reset_stats(struct ospf_interface *oi)
485 {
486 oi->hello_in = oi->hello_out = 0;
487 oi->db_desc_in = oi->db_desc_out = 0;
488 oi->ls_req_in = oi->ls_req_out = 0;
489 oi->ls_upd_in = oi->ls_upd_out = 0;
490 oi->ls_ack_in = oi->ls_ack_out = 0;
491 }
492
493 void ospf_if_stream_set(struct ospf_interface *oi)
494 {
495 /* set output fifo queue. */
496 if (oi->obuf == NULL)
497 oi->obuf = ospf_fifo_new();
498 }
499
500 void ospf_if_stream_unset(struct ospf_interface *oi)
501 {
502 struct ospf *ospf = oi->ospf;
503
504 if (oi->obuf) {
505 /* flush the interface packet queue */
506 ospf_fifo_flush(oi->obuf);
507 /*reset protocol stats */
508 ospf_if_reset_stats(oi);
509
510 if (oi->on_write_q) {
511 listnode_delete(ospf->oi_write_q, oi);
512 if (list_isempty(ospf->oi_write_q))
513 OSPF_TIMER_OFF(ospf->t_write);
514 oi->on_write_q = 0;
515 }
516 }
517 }
518
519
520 static struct ospf_if_params *ospf_new_if_params(void)
521 {
522 struct ospf_if_params *oip;
523
524 oip = XCALLOC(MTYPE_OSPF_IF_PARAMS, sizeof(struct ospf_if_params));
525
526 if (!oip)
527 return NULL;
528
529 UNSET_IF_PARAM(oip, output_cost_cmd);
530 UNSET_IF_PARAM(oip, transmit_delay);
531 UNSET_IF_PARAM(oip, retransmit_interval);
532 UNSET_IF_PARAM(oip, passive_interface);
533 UNSET_IF_PARAM(oip, v_hello);
534 UNSET_IF_PARAM(oip, fast_hello);
535 UNSET_IF_PARAM(oip, v_wait);
536 UNSET_IF_PARAM(oip, priority);
537 UNSET_IF_PARAM(oip, type);
538 UNSET_IF_PARAM(oip, auth_simple);
539 UNSET_IF_PARAM(oip, auth_crypt);
540 UNSET_IF_PARAM(oip, auth_type);
541
542 oip->auth_crypt = list_new();
543
544 oip->network_lsa_seqnum = htonl(OSPF_INITIAL_SEQUENCE_NUMBER);
545
546 return oip;
547 }
548
549 void ospf_del_if_params(struct ospf_if_params *oip)
550 {
551 list_delete_and_null(&oip->auth_crypt);
552 bfd_info_free(&(oip->bfd_info));
553 XFREE(MTYPE_OSPF_IF_PARAMS, oip);
554 }
555
556 void ospf_free_if_params(struct interface *ifp, struct in_addr addr)
557 {
558 struct ospf_if_params *oip;
559 struct prefix_ipv4 p;
560 struct route_node *rn;
561
562 p.family = AF_INET;
563 p.prefixlen = IPV4_MAX_PREFIXLEN;
564 p.prefix = addr;
565 rn = route_node_lookup(IF_OIFS_PARAMS(ifp), (struct prefix *)&p);
566 if (!rn || !rn->info)
567 return;
568
569 oip = rn->info;
570 route_unlock_node(rn);
571
572 if (!OSPF_IF_PARAM_CONFIGURED(oip, output_cost_cmd)
573 && !OSPF_IF_PARAM_CONFIGURED(oip, transmit_delay)
574 && !OSPF_IF_PARAM_CONFIGURED(oip, retransmit_interval)
575 && !OSPF_IF_PARAM_CONFIGURED(oip, passive_interface)
576 && !OSPF_IF_PARAM_CONFIGURED(oip, v_hello)
577 && !OSPF_IF_PARAM_CONFIGURED(oip, fast_hello)
578 && !OSPF_IF_PARAM_CONFIGURED(oip, v_wait)
579 && !OSPF_IF_PARAM_CONFIGURED(oip, priority)
580 && !OSPF_IF_PARAM_CONFIGURED(oip, type)
581 && !OSPF_IF_PARAM_CONFIGURED(oip, auth_simple)
582 && !OSPF_IF_PARAM_CONFIGURED(oip, auth_type)
583 && listcount(oip->auth_crypt) == 0
584 && ntohl(oip->network_lsa_seqnum) != OSPF_INITIAL_SEQUENCE_NUMBER) {
585 ospf_del_if_params(oip);
586 rn->info = NULL;
587 route_unlock_node(rn);
588 }
589 }
590
591 struct ospf_if_params *ospf_lookup_if_params(struct interface *ifp,
592 struct in_addr addr)
593 {
594 struct prefix_ipv4 p;
595 struct route_node *rn;
596
597 p.family = AF_INET;
598 p.prefixlen = IPV4_MAX_PREFIXLEN;
599 p.prefix = addr;
600
601 rn = route_node_lookup(IF_OIFS_PARAMS(ifp), (struct prefix *)&p);
602
603 if (rn) {
604 route_unlock_node(rn);
605 return rn->info;
606 }
607
608 return NULL;
609 }
610
611 struct ospf_if_params *ospf_get_if_params(struct interface *ifp,
612 struct in_addr addr)
613 {
614 struct prefix_ipv4 p;
615 struct route_node *rn;
616
617 p.family = AF_INET;
618 p.prefixlen = IPV4_MAX_PREFIXLEN;
619 p.prefix = addr;
620 apply_mask_ipv4(&p);
621
622 rn = route_node_get(IF_OIFS_PARAMS(ifp), (struct prefix *)&p);
623
624 if (rn->info == NULL)
625 rn->info = ospf_new_if_params();
626 else
627 route_unlock_node(rn);
628
629 return rn->info;
630 }
631
632 void ospf_if_update_params(struct interface *ifp, struct in_addr addr)
633 {
634 struct route_node *rn;
635 struct ospf_interface *oi;
636
637 for (rn = route_top(IF_OIFS(ifp)); rn; rn = route_next(rn)) {
638 if ((oi = rn->info) == NULL)
639 continue;
640
641 if (IPV4_ADDR_SAME(&oi->address->u.prefix4, &addr))
642 oi->params = ospf_lookup_if_params(
643 ifp, oi->address->u.prefix4);
644 }
645 }
646
647 int ospf_if_new_hook(struct interface *ifp)
648 {
649 int rc = 0;
650
651 ifp->info = XCALLOC(MTYPE_OSPF_IF_INFO, sizeof(struct ospf_if_info));
652
653 IF_OIFS(ifp) = route_table_init();
654 IF_OIFS_PARAMS(ifp) = route_table_init();
655
656 IF_DEF_PARAMS(ifp) = ospf_new_if_params();
657
658 SET_IF_PARAM(IF_DEF_PARAMS(ifp), transmit_delay);
659 IF_DEF_PARAMS(ifp)->transmit_delay = OSPF_TRANSMIT_DELAY_DEFAULT;
660
661 SET_IF_PARAM(IF_DEF_PARAMS(ifp), retransmit_interval);
662 IF_DEF_PARAMS(ifp)->retransmit_interval =
663 OSPF_RETRANSMIT_INTERVAL_DEFAULT;
664
665 SET_IF_PARAM(IF_DEF_PARAMS(ifp), priority);
666 IF_DEF_PARAMS(ifp)->priority = OSPF_ROUTER_PRIORITY_DEFAULT;
667
668 IF_DEF_PARAMS(ifp)->mtu_ignore = OSPF_MTU_IGNORE_DEFAULT;
669
670 SET_IF_PARAM(IF_DEF_PARAMS(ifp), v_hello);
671 IF_DEF_PARAMS(ifp)->v_hello = OSPF_HELLO_INTERVAL_DEFAULT;
672
673 SET_IF_PARAM(IF_DEF_PARAMS(ifp), fast_hello);
674 IF_DEF_PARAMS(ifp)->fast_hello = OSPF_FAST_HELLO_DEFAULT;
675
676 SET_IF_PARAM(IF_DEF_PARAMS(ifp), v_wait);
677 IF_DEF_PARAMS(ifp)->v_wait = OSPF_ROUTER_DEAD_INTERVAL_DEFAULT;
678
679 SET_IF_PARAM(IF_DEF_PARAMS(ifp), auth_simple);
680 memset(IF_DEF_PARAMS(ifp)->auth_simple, 0, OSPF_AUTH_SIMPLE_SIZE);
681
682 SET_IF_PARAM(IF_DEF_PARAMS(ifp), auth_type);
683 IF_DEF_PARAMS(ifp)->auth_type = OSPF_AUTH_NOTSET;
684
685 rc = ospf_opaque_new_if(ifp);
686 return rc;
687 }
688
689 static int ospf_if_delete_hook(struct interface *ifp)
690 {
691 int rc = 0;
692 struct route_node *rn;
693 rc = ospf_opaque_del_if(ifp);
694
695 route_table_finish(IF_OIFS(ifp));
696
697 for (rn = route_top(IF_OIFS_PARAMS(ifp)); rn; rn = route_next(rn))
698 if (rn->info)
699 ospf_del_if_params(rn->info);
700 route_table_finish(IF_OIFS_PARAMS(ifp));
701
702 ospf_del_if_params((struct ospf_if_params *)IF_DEF_PARAMS(ifp));
703 XFREE(MTYPE_OSPF_IF_INFO, ifp->info);
704 ifp->info = NULL;
705
706 return rc;
707 }
708
709 int ospf_if_is_enable(struct ospf_interface *oi)
710 {
711 if (!(if_is_loopback(oi->ifp) || if_is_vrf(oi->ifp)))
712 if (if_is_up(oi->ifp))
713 return 1;
714
715 return 0;
716 }
717
718 void ospf_if_set_multicast(struct ospf_interface *oi)
719 {
720 if ((oi->state > ISM_Loopback) && (oi->type != OSPF_IFTYPE_LOOPBACK)
721 && (oi->type != OSPF_IFTYPE_VIRTUALLINK)
722 && (OSPF_IF_PASSIVE_STATUS(oi) == OSPF_IF_ACTIVE)) {
723 /* The interface should belong to the OSPF-all-routers group. */
724 if (!OI_MEMBER_CHECK(oi, MEMBER_ALLROUTERS)
725 && (ospf_if_add_allspfrouters(oi->ospf, oi->address,
726 oi->ifp->ifindex)
727 >= 0))
728 /* Set the flag only if the system call to join
729 * succeeded. */
730 OI_MEMBER_JOINED(oi, MEMBER_ALLROUTERS);
731 } else {
732 /* The interface should NOT belong to the OSPF-all-routers
733 * group. */
734 if (OI_MEMBER_CHECK(oi, MEMBER_ALLROUTERS)) {
735 /* Only actually drop if this is the last reference */
736 if (OI_MEMBER_COUNT(oi, MEMBER_ALLROUTERS) == 1)
737 ospf_if_drop_allspfrouters(oi->ospf,
738 oi->address,
739 oi->ifp->ifindex);
740 /* Unset the flag regardless of whether the system call
741 to leave
742 the group succeeded, since it's much safer to assume
743 that
744 we are not a member. */
745 OI_MEMBER_LEFT(oi, MEMBER_ALLROUTERS);
746 }
747 }
748
749 if (((oi->type == OSPF_IFTYPE_BROADCAST)
750 || (oi->type == OSPF_IFTYPE_POINTOPOINT))
751 && ((oi->state == ISM_DR) || (oi->state == ISM_Backup))
752 && (OSPF_IF_PASSIVE_STATUS(oi) == OSPF_IF_ACTIVE)) {
753 /* The interface should belong to the OSPF-designated-routers
754 * group. */
755 if (!OI_MEMBER_CHECK(oi, MEMBER_DROUTERS)
756 && (ospf_if_add_alldrouters(oi->ospf, oi->address,
757 oi->ifp->ifindex)
758 >= 0))
759 /* Set the flag only if the system call to join
760 * succeeded. */
761 OI_MEMBER_JOINED(oi, MEMBER_DROUTERS);
762 } else {
763 /* The interface should NOT belong to the
764 * OSPF-designated-routers group */
765 if (OI_MEMBER_CHECK(oi, MEMBER_DROUTERS)) {
766 /* drop only if last reference */
767 if (OI_MEMBER_COUNT(oi, MEMBER_DROUTERS) == 1)
768 ospf_if_drop_alldrouters(oi->ospf, oi->address,
769 oi->ifp->ifindex);
770
771 /* Unset the flag regardless of whether the system call
772 to leave
773 the group succeeded, since it's much safer to assume
774 that
775 we are not a member. */
776 OI_MEMBER_LEFT(oi, MEMBER_DROUTERS);
777 }
778 }
779 }
780
781 int ospf_if_up(struct ospf_interface *oi)
782 {
783 if (oi == NULL)
784 return 0;
785
786 if (oi->type == OSPF_IFTYPE_LOOPBACK)
787 OSPF_ISM_EVENT_SCHEDULE(oi, ISM_LoopInd);
788 else {
789 OSPF_ISM_EVENT_SCHEDULE(oi, ISM_InterfaceUp);
790 }
791
792 return 1;
793 }
794
795 int ospf_if_down(struct ospf_interface *oi)
796 {
797 if (oi == NULL)
798 return 0;
799
800 OSPF_ISM_EVENT_EXECUTE(oi, ISM_InterfaceDown);
801 /* delete position in router LSA */
802 oi->lsa_pos_beg = 0;
803 oi->lsa_pos_end = 0;
804 /* Shutdown packet reception and sending */
805 ospf_if_stream_unset(oi);
806
807 return 1;
808 }
809
810
811 /* Virtual Link related functions. */
812
813 struct ospf_vl_data *ospf_vl_data_new(struct ospf_area *area,
814 struct in_addr vl_peer)
815 {
816 struct ospf_vl_data *vl_data;
817
818 vl_data = XCALLOC(MTYPE_OSPF_VL_DATA, sizeof(struct ospf_vl_data));
819
820 vl_data->vl_peer.s_addr = vl_peer.s_addr;
821 vl_data->vl_area_id = area->area_id;
822 vl_data->vl_area_id_fmt = area->area_id_fmt;
823
824 return vl_data;
825 }
826
827 void ospf_vl_data_free(struct ospf_vl_data *vl_data)
828 {
829 XFREE(MTYPE_OSPF_VL_DATA, vl_data);
830 }
831
832 unsigned int vlink_count = 0;
833
834 struct ospf_interface *ospf_vl_new(struct ospf *ospf,
835 struct ospf_vl_data *vl_data)
836 {
837 struct ospf_interface *voi;
838 struct interface *vi;
839 char ifname[INTERFACE_NAMSIZ];
840 struct ospf_area *area;
841 struct in_addr area_id;
842 struct connected *co;
843 struct prefix_ipv4 *p;
844
845 if (IS_DEBUG_OSPF_EVENT)
846 zlog_debug("ospf_vl_new(): Start");
847 if (vlink_count == OSPF_VL_MAX_COUNT) {
848 if (IS_DEBUG_OSPF_EVENT)
849 zlog_debug(
850 "ospf_vl_new(): Alarm: "
851 "cannot create more than OSPF_MAX_VL_COUNT virtual links");
852 return NULL;
853 }
854
855 if (IS_DEBUG_OSPF_EVENT)
856 zlog_debug(
857 "ospf_vl_new(): creating pseudo zebra interface vrf id %u",
858 ospf->vrf_id);
859
860 snprintf(ifname, sizeof(ifname), "VLINK%u", vlink_count);
861 vi = if_create(ifname, ospf->vrf_id);
862 /*
863 * if_create sets ZEBRA_INTERFACE_LINKDETECTION
864 * virtual links don't need this.
865 */
866 UNSET_FLAG(vi->status, ZEBRA_INTERFACE_LINKDETECTION);
867 co = connected_new();
868 co->ifp = vi;
869 listnode_add(vi->connected, co);
870
871 p = prefix_ipv4_new();
872 p->family = AF_INET;
873 p->prefix.s_addr = 0;
874 p->prefixlen = 0;
875
876 co->address = (struct prefix *)p;
877
878 voi = ospf_if_new(ospf, vi, co->address);
879 if (voi == NULL) {
880 if (IS_DEBUG_OSPF_EVENT)
881 zlog_debug(
882 "ospf_vl_new(): Alarm: OSPF int structure is not created");
883 return NULL;
884 }
885 voi->connected = co;
886 voi->vl_data = vl_data;
887 voi->ifp->mtu = OSPF_VL_MTU;
888 voi->type = OSPF_IFTYPE_VIRTUALLINK;
889
890 vlink_count++;
891 if (IS_DEBUG_OSPF_EVENT)
892 zlog_debug("ospf_vl_new(): Created name: %s", ifname);
893 if (IS_DEBUG_OSPF_EVENT)
894 zlog_debug("ospf_vl_new(): set if->name to %s", vi->name);
895
896 area_id.s_addr = 0;
897 area = ospf_area_get(ospf, area_id);
898 voi->area = area;
899
900 if (IS_DEBUG_OSPF_EVENT)
901 zlog_debug(
902 "ospf_vl_new(): set associated area to the backbone");
903
904 /* Add pseudo neighbor. */
905 ospf_nbr_self_reset(voi, voi->ospf->router_id);
906
907 ospf_area_add_if(voi->area, voi);
908
909 ospf_if_stream_set(voi);
910
911 if (IS_DEBUG_OSPF_EVENT)
912 zlog_debug("ospf_vl_new(): Stop");
913 return voi;
914 }
915
916 static void ospf_vl_if_delete(struct ospf_vl_data *vl_data)
917 {
918 struct interface *ifp = vl_data->vl_oi->ifp;
919 vl_data->vl_oi->address->u.prefix4.s_addr = 0;
920 vl_data->vl_oi->address->prefixlen = 0;
921 ospf_if_free(vl_data->vl_oi);
922 if_delete(ifp);
923 vlink_count--;
924 }
925
926 /* Look up vl_data for given peer, optionally qualified to be in the
927 * specified area. NULL area returns first found..
928 */
929 struct ospf_vl_data *ospf_vl_lookup(struct ospf *ospf, struct ospf_area *area,
930 struct in_addr vl_peer)
931 {
932 struct ospf_vl_data *vl_data;
933 struct listnode *node;
934
935 if (IS_DEBUG_OSPF_EVENT) {
936 zlog_debug("%s: Looking for %s", __func__, inet_ntoa(vl_peer));
937 if (area)
938 zlog_debug("%s: in area %s", __func__,
939 inet_ntoa(area->area_id));
940 }
941
942 for (ALL_LIST_ELEMENTS_RO(ospf->vlinks, node, vl_data)) {
943 if (IS_DEBUG_OSPF_EVENT)
944 zlog_debug("%s: VL %s, peer %s", __func__,
945 vl_data->vl_oi->ifp->name,
946 inet_ntoa(vl_data->vl_peer));
947
948 if (area
949 && !IPV4_ADDR_SAME(&vl_data->vl_area_id, &area->area_id))
950 continue;
951
952 if (IPV4_ADDR_SAME(&vl_data->vl_peer, &vl_peer))
953 return vl_data;
954 }
955
956 return NULL;
957 }
958
959 static void ospf_vl_shutdown(struct ospf_vl_data *vl_data)
960 {
961 struct ospf_interface *oi;
962
963 if ((oi = vl_data->vl_oi) == NULL)
964 return;
965
966 oi->address->u.prefix4.s_addr = 0;
967 oi->address->prefixlen = 0;
968
969 UNSET_FLAG(oi->ifp->flags, IFF_UP);
970 /* OSPF_ISM_EVENT_SCHEDULE (oi, ISM_InterfaceDown); */
971 OSPF_ISM_EVENT_EXECUTE(oi, ISM_InterfaceDown);
972 }
973
974 void ospf_vl_add(struct ospf *ospf, struct ospf_vl_data *vl_data)
975 {
976 listnode_add(ospf->vlinks, vl_data);
977 hook_call(ospf_vl_add, vl_data);
978 }
979
980 void ospf_vl_delete(struct ospf *ospf, struct ospf_vl_data *vl_data)
981 {
982 ospf_vl_shutdown(vl_data);
983 ospf_vl_if_delete(vl_data);
984
985 hook_call(ospf_vl_delete, vl_data);
986 listnode_delete(ospf->vlinks, vl_data);
987
988 ospf_vl_data_free(vl_data);
989 }
990
991 static int ospf_vl_set_params(struct ospf_vl_data *vl_data, struct vertex *v)
992 {
993 int changed = 0;
994 struct ospf_interface *voi;
995 struct listnode *node;
996 struct vertex_parent *vp = NULL;
997 unsigned int i;
998 struct router_lsa *rl;
999
1000 voi = vl_data->vl_oi;
1001
1002 if (voi->output_cost != v->distance) {
1003
1004 voi->output_cost = v->distance;
1005 changed = 1;
1006 }
1007
1008 for (ALL_LIST_ELEMENTS_RO(v->parents, node, vp)) {
1009 vl_data->nexthop.oi = vp->nexthop->oi;
1010 vl_data->nexthop.router = vp->nexthop->router;
1011
1012 if (!IPV4_ADDR_SAME(&voi->address->u.prefix4,
1013 &vl_data->nexthop.oi->address->u.prefix4))
1014 changed = 1;
1015
1016 voi->address->u.prefix4 =
1017 vl_data->nexthop.oi->address->u.prefix4;
1018 voi->address->prefixlen =
1019 vl_data->nexthop.oi->address->prefixlen;
1020
1021 break; /* We take the first interface. */
1022 }
1023
1024 rl = (struct router_lsa *)v->lsa;
1025
1026 /* use SPF determined backlink index in struct vertex
1027 * for virtual link destination address
1028 */
1029 if (vp && vp->backlink >= 0) {
1030 if (!IPV4_ADDR_SAME(&vl_data->peer_addr,
1031 &rl->link[vp->backlink].link_data))
1032 changed = 1;
1033 vl_data->peer_addr = rl->link[vp->backlink].link_data;
1034 } else {
1035 /* This is highly odd, there is no backlink index
1036 * there should be due to the ospf_spf_has_link() check
1037 * in SPF. Lets warn and try pick a link anyway.
1038 */
1039 zlog_warn("ospf_vl_set_params: No backlink for %s!",
1040 vl_data->vl_oi->ifp->name);
1041 for (i = 0; i < ntohs(rl->links); i++) {
1042 switch (rl->link[i].type) {
1043 case LSA_LINK_TYPE_VIRTUALLINK:
1044 if (IS_DEBUG_OSPF_EVENT)
1045 zlog_debug(
1046 "found back link through VL");
1047 /* fallthru */
1048 case LSA_LINK_TYPE_TRANSIT:
1049 case LSA_LINK_TYPE_POINTOPOINT:
1050 if (!IPV4_ADDR_SAME(&vl_data->peer_addr,
1051 &rl->link[i].link_data))
1052 changed = 1;
1053 vl_data->peer_addr = rl->link[i].link_data;
1054 }
1055 }
1056 }
1057
1058 if (IS_DEBUG_OSPF_EVENT)
1059 zlog_debug("%s: %s peer address: %s, cost: %d,%schanged",
1060 __func__, vl_data->vl_oi->ifp->name,
1061 inet_ntoa(vl_data->peer_addr), voi->output_cost,
1062 (changed ? " " : " un"));
1063
1064 return changed;
1065 }
1066
1067
1068 void ospf_vl_up_check(struct ospf_area *area, struct in_addr rid,
1069 struct vertex *v)
1070 {
1071 struct ospf *ospf = area->ospf;
1072 struct listnode *node;
1073 struct ospf_vl_data *vl_data;
1074 struct ospf_interface *oi;
1075
1076 if (IS_DEBUG_OSPF_EVENT) {
1077 zlog_debug("ospf_vl_up_check(): Start");
1078 zlog_debug("ospf_vl_up_check(): Router ID is %s",
1079 inet_ntoa(rid));
1080 zlog_debug("ospf_vl_up_check(): Area is %s",
1081 inet_ntoa(area->area_id));
1082 }
1083
1084 for (ALL_LIST_ELEMENTS_RO(ospf->vlinks, node, vl_data)) {
1085 if (IS_DEBUG_OSPF_EVENT) {
1086 zlog_debug("%s: considering VL, %s in area %s",
1087 __func__, vl_data->vl_oi->ifp->name,
1088 inet_ntoa(vl_data->vl_area_id));
1089 zlog_debug("%s: peer ID: %s", __func__,
1090 inet_ntoa(vl_data->vl_peer));
1091 }
1092
1093 if (IPV4_ADDR_SAME(&vl_data->vl_peer, &rid)
1094 && IPV4_ADDR_SAME(&vl_data->vl_area_id, &area->area_id)) {
1095 oi = vl_data->vl_oi;
1096 SET_FLAG(vl_data->flags, OSPF_VL_FLAG_APPROVED);
1097
1098 if (IS_DEBUG_OSPF_EVENT)
1099 zlog_debug(
1100 "ospf_vl_up_check(): this VL matched");
1101
1102 if (oi->state == ISM_Down) {
1103 if (IS_DEBUG_OSPF_EVENT)
1104 zlog_debug(
1105 "ospf_vl_up_check(): VL is down, waking it up");
1106 SET_FLAG(oi->ifp->flags, IFF_UP);
1107 OSPF_ISM_EVENT_EXECUTE(oi, ISM_InterfaceUp);
1108 }
1109
1110 if (ospf_vl_set_params(vl_data, v)) {
1111 if (IS_DEBUG_OSPF(ism, ISM_EVENTS))
1112 zlog_debug(
1113 "ospf_vl_up_check: VL cost change,"
1114 " scheduling router lsa refresh");
1115 if (ospf->backbone)
1116 ospf_router_lsa_update_area(
1117 ospf->backbone);
1118 else if (IS_DEBUG_OSPF(ism, ISM_EVENTS))
1119 zlog_debug(
1120 "ospf_vl_up_check: VL cost change, no backbone!");
1121 }
1122 }
1123 }
1124 }
1125
1126 void ospf_vl_unapprove(struct ospf *ospf)
1127 {
1128 struct listnode *node;
1129 struct ospf_vl_data *vl_data;
1130
1131 for (ALL_LIST_ELEMENTS_RO(ospf->vlinks, node, vl_data))
1132 UNSET_FLAG(vl_data->flags, OSPF_VL_FLAG_APPROVED);
1133 }
1134
1135 void ospf_vl_shut_unapproved(struct ospf *ospf)
1136 {
1137 struct listnode *node, *nnode;
1138 struct ospf_vl_data *vl_data;
1139
1140 for (ALL_LIST_ELEMENTS(ospf->vlinks, node, nnode, vl_data))
1141 if (!CHECK_FLAG(vl_data->flags, OSPF_VL_FLAG_APPROVED))
1142 ospf_vl_shutdown(vl_data);
1143 }
1144
1145 int ospf_full_virtual_nbrs(struct ospf_area *area)
1146 {
1147 if (IS_DEBUG_OSPF_EVENT) {
1148 zlog_debug(
1149 "counting fully adjacent virtual neighbors in area %s",
1150 inet_ntoa(area->area_id));
1151 zlog_debug("there are %d of them", area->full_vls);
1152 }
1153
1154 return area->full_vls;
1155 }
1156
1157 int ospf_vls_in_area(struct ospf_area *area)
1158 {
1159 struct listnode *node;
1160 struct ospf_vl_data *vl_data;
1161 int c = 0;
1162
1163 for (ALL_LIST_ELEMENTS_RO(area->ospf->vlinks, node, vl_data))
1164 if (IPV4_ADDR_SAME(&vl_data->vl_area_id, &area->area_id))
1165 c++;
1166
1167 return c;
1168 }
1169
1170
1171 struct crypt_key *ospf_crypt_key_new()
1172 {
1173 return XCALLOC(MTYPE_OSPF_CRYPT_KEY, sizeof(struct crypt_key));
1174 }
1175
1176 void ospf_crypt_key_add(struct list *crypt, struct crypt_key *ck)
1177 {
1178 listnode_add(crypt, ck);
1179 }
1180
1181 struct crypt_key *ospf_crypt_key_lookup(struct list *auth_crypt, uint8_t key_id)
1182 {
1183 struct listnode *node;
1184 struct crypt_key *ck;
1185
1186 for (ALL_LIST_ELEMENTS_RO(auth_crypt, node, ck))
1187 if (ck->key_id == key_id)
1188 return ck;
1189
1190 return NULL;
1191 }
1192
1193 int ospf_crypt_key_delete(struct list *auth_crypt, uint8_t key_id)
1194 {
1195 struct listnode *node, *nnode;
1196 struct crypt_key *ck;
1197
1198 for (ALL_LIST_ELEMENTS(auth_crypt, node, nnode, ck)) {
1199 if (ck->key_id == key_id) {
1200 listnode_delete(auth_crypt, ck);
1201 XFREE(MTYPE_OSPF_CRYPT_KEY, ck);
1202 return 1;
1203 }
1204 }
1205
1206 return 0;
1207 }
1208
1209 uint8_t ospf_default_iftype(struct interface *ifp)
1210 {
1211 if (if_is_pointopoint(ifp))
1212 return OSPF_IFTYPE_POINTOPOINT;
1213 else if (if_is_loopback(ifp) || if_is_vrf(ifp))
1214 return OSPF_IFTYPE_LOOPBACK;
1215 else
1216 return OSPF_IFTYPE_BROADCAST;
1217 }
1218
1219 void ospf_if_init()
1220 {
1221 /* Initialize Zebra interface data structure. */
1222 hook_register_prio(if_add, 0, ospf_if_new_hook);
1223 hook_register_prio(if_del, 0, ospf_if_delete_hook);
1224 }