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