]> git.proxmox.com Git - mirror_frr.git/blob - ospfd/ospf_interface.c
Merge pull request #4866 from ton31337/feature/apply_route-map_for_aggregate-address
[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 oi = ospf_if_table_lookup(ifp, p);
229 if (oi)
230 return oi;
231
232 oi = XCALLOC(MTYPE_OSPF_IF, sizeof(struct ospf_interface));
233
234 oi->obuf = ospf_fifo_new();
235
236 /* Set zebra interface pointer. */
237 oi->ifp = ifp;
238 oi->address = p;
239
240 ospf_add_to_if(ifp, oi);
241 listnode_add(ospf->oiflist, oi);
242
243 /* Initialize neighbor list. */
244 oi->nbrs = route_table_init();
245
246 /* Initialize static neighbor list. */
247 oi->nbr_nbma = list_new();
248
249 /* Initialize Link State Acknowledgment list. */
250 oi->ls_ack = list_new();
251 oi->ls_ack_direct.ls_ack = list_new();
252
253 /* Set default values. */
254 ospf_if_reset_variables(oi);
255
256 /* Set pseudo neighbor to Null */
257 oi->nbr_self = NULL;
258
259 oi->ls_upd_queue = route_table_init();
260 oi->t_ls_upd_event = NULL;
261 oi->t_ls_ack_direct = NULL;
262
263 oi->crypt_seqnum = time(NULL);
264
265 ospf_opaque_type9_lsa_init(oi);
266
267 oi->ospf = ospf;
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 ospf_fifo_free(oi->obuf);
329
330 assert(oi->state == ISM_Down);
331
332 ospf_opaque_type9_lsa_term(oi);
333
334 QOBJ_UNREG(oi);
335
336 /* Free Pseudo Neighbour */
337 ospf_nbr_delete(oi->nbr_self);
338
339 route_table_finish(oi->nbrs);
340 route_table_finish(oi->ls_upd_queue);
341
342 /* Free any lists that should be freed */
343 list_delete(&oi->nbr_nbma);
344
345 list_delete(&oi->ls_ack);
346 list_delete(&oi->ls_ack_direct.ls_ack);
347
348 if (IS_DEBUG_OSPF_EVENT)
349 zlog_debug("%s: ospf interface %s vrf %s id %u deleted",
350 __PRETTY_FUNCTION__, oi->ifp->name,
351 ospf_vrf_id_to_name(oi->ifp->vrf_id),
352 oi->ifp->vrf_id);
353
354 ospf_delete_from_if(oi->ifp, oi);
355
356 listnode_delete(oi->ospf->oiflist, oi);
357 listnode_delete(oi->area->oiflist, oi);
358
359 thread_cancel_event(master, oi);
360
361 memset(oi, 0, sizeof(*oi));
362 XFREE(MTYPE_OSPF_IF, oi);
363 }
364
365 int ospf_if_is_up(struct ospf_interface *oi)
366 {
367 return if_is_up(oi->ifp);
368 }
369
370 struct ospf_interface *ospf_if_exists(struct ospf_interface *oic)
371 {
372 struct listnode *node;
373 struct ospf *ospf;
374 struct ospf_interface *oi;
375
376 if (!oic)
377 return NULL;
378
379 ospf = oic->ospf;
380 if (ospf == NULL)
381 return NULL;
382
383 for (ALL_LIST_ELEMENTS_RO(ospf->oiflist, node, oi))
384 if (oi == oic)
385 return oi;
386
387 return NULL;
388 }
389
390 /* Lookup OSPF interface by router LSA posistion */
391 struct ospf_interface *ospf_if_lookup_by_lsa_pos(struct ospf_area *area,
392 int lsa_pos)
393 {
394 struct listnode *node;
395 struct ospf_interface *oi;
396
397 for (ALL_LIST_ELEMENTS_RO(area->oiflist, node, oi)) {
398 if (lsa_pos >= oi->lsa_pos_beg && lsa_pos < oi->lsa_pos_end)
399 return oi;
400 }
401 return NULL;
402 }
403
404 struct ospf_interface *ospf_if_lookup_by_local_addr(struct ospf *ospf,
405 struct interface *ifp,
406 struct in_addr address)
407 {
408 struct listnode *node;
409 struct ospf_interface *oi;
410
411 for (ALL_LIST_ELEMENTS_RO(ospf->oiflist, node, oi))
412 if (oi->type != OSPF_IFTYPE_VIRTUALLINK) {
413 if (ifp && oi->ifp != ifp)
414 continue;
415
416 if (IPV4_ADDR_SAME(&address, &oi->address->u.prefix4))
417 return oi;
418 }
419
420 return NULL;
421 }
422
423 struct ospf_interface *ospf_if_lookup_by_prefix(struct ospf *ospf,
424 struct prefix_ipv4 *p)
425 {
426 struct listnode *node;
427 struct ospf_interface *oi;
428
429 /* Check each Interface. */
430 for (ALL_LIST_ELEMENTS_RO(ospf->oiflist, node, oi)) {
431 if (oi->type != OSPF_IFTYPE_VIRTUALLINK) {
432 struct prefix ptmp;
433
434 prefix_copy(&ptmp, CONNECTED_PREFIX(oi->connected));
435 apply_mask(&ptmp);
436 if (prefix_same(&ptmp, (struct prefix *)p))
437 return oi;
438 }
439 }
440 return NULL;
441 }
442
443 /* determine receiving interface by ifp and source address */
444 struct ospf_interface *ospf_if_lookup_recv_if(struct ospf *ospf,
445 struct in_addr src,
446 struct interface *ifp)
447 {
448 struct route_node *rn;
449 struct prefix_ipv4 addr;
450 struct ospf_interface *oi, *match;
451
452 addr.family = AF_INET;
453 addr.prefix = src;
454 addr.prefixlen = IPV4_MAX_BITLEN;
455
456 match = NULL;
457
458 for (rn = route_top(IF_OIFS(ifp)); rn; rn = route_next(rn)) {
459 oi = rn->info;
460
461 if (!oi) /* oi can be NULL for PtP aliases */
462 continue;
463
464 if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
465 continue;
466
467 if (if_is_loopback(oi->ifp) || if_is_vrf(oi->ifp))
468 continue;
469
470 if (CHECK_FLAG(oi->connected->flags, ZEBRA_IFA_UNNUMBERED))
471 match = oi;
472 else if (prefix_match(CONNECTED_PREFIX(oi->connected),
473 (struct prefix *)&addr)) {
474 if ((match == NULL) || (match->address->prefixlen
475 < oi->address->prefixlen))
476 match = oi;
477 }
478 }
479
480 return match;
481 }
482
483 static void ospf_if_reset_stats(struct ospf_interface *oi)
484 {
485 oi->hello_in = oi->hello_out = 0;
486 oi->db_desc_in = oi->db_desc_out = 0;
487 oi->ls_req_in = oi->ls_req_out = 0;
488 oi->ls_upd_in = oi->ls_upd_out = 0;
489 oi->ls_ack_in = oi->ls_ack_out = 0;
490 }
491
492 void ospf_if_stream_unset(struct ospf_interface *oi)
493 {
494 struct ospf *ospf = oi->ospf;
495
496 /* flush the interface packet queue */
497 ospf_fifo_flush(oi->obuf);
498 /*reset protocol stats */
499 ospf_if_reset_stats(oi);
500
501 if (oi->on_write_q) {
502 listnode_delete(ospf->oi_write_q, oi);
503 if (list_isempty(ospf->oi_write_q))
504 OSPF_TIMER_OFF(ospf->t_write);
505 oi->on_write_q = 0;
506 }
507 }
508
509
510 static struct ospf_if_params *ospf_new_if_params(void)
511 {
512 struct ospf_if_params *oip;
513
514 oip = XCALLOC(MTYPE_OSPF_IF_PARAMS, sizeof(struct ospf_if_params));
515
516 UNSET_IF_PARAM(oip, output_cost_cmd);
517 UNSET_IF_PARAM(oip, transmit_delay);
518 UNSET_IF_PARAM(oip, retransmit_interval);
519 UNSET_IF_PARAM(oip, passive_interface);
520 UNSET_IF_PARAM(oip, v_hello);
521 UNSET_IF_PARAM(oip, fast_hello);
522 UNSET_IF_PARAM(oip, v_wait);
523 UNSET_IF_PARAM(oip, priority);
524 UNSET_IF_PARAM(oip, type);
525 UNSET_IF_PARAM(oip, auth_simple);
526 UNSET_IF_PARAM(oip, auth_crypt);
527 UNSET_IF_PARAM(oip, auth_type);
528
529 oip->auth_crypt = list_new();
530
531 oip->network_lsa_seqnum = htonl(OSPF_INITIAL_SEQUENCE_NUMBER);
532
533 return oip;
534 }
535
536 void ospf_del_if_params(struct ospf_if_params *oip)
537 {
538 list_delete(&oip->auth_crypt);
539 bfd_info_free(&(oip->bfd_info));
540 XFREE(MTYPE_OSPF_IF_PARAMS, oip);
541 }
542
543 void ospf_free_if_params(struct interface *ifp, struct in_addr addr)
544 {
545 struct ospf_if_params *oip;
546 struct prefix_ipv4 p;
547 struct route_node *rn;
548
549 p.family = AF_INET;
550 p.prefixlen = IPV4_MAX_PREFIXLEN;
551 p.prefix = addr;
552 rn = route_node_lookup(IF_OIFS_PARAMS(ifp), (struct prefix *)&p);
553 if (!rn || !rn->info)
554 return;
555
556 oip = rn->info;
557 route_unlock_node(rn);
558
559 if (!OSPF_IF_PARAM_CONFIGURED(oip, output_cost_cmd)
560 && !OSPF_IF_PARAM_CONFIGURED(oip, transmit_delay)
561 && !OSPF_IF_PARAM_CONFIGURED(oip, retransmit_interval)
562 && !OSPF_IF_PARAM_CONFIGURED(oip, passive_interface)
563 && !OSPF_IF_PARAM_CONFIGURED(oip, v_hello)
564 && !OSPF_IF_PARAM_CONFIGURED(oip, fast_hello)
565 && !OSPF_IF_PARAM_CONFIGURED(oip, v_wait)
566 && !OSPF_IF_PARAM_CONFIGURED(oip, priority)
567 && !OSPF_IF_PARAM_CONFIGURED(oip, type)
568 && !OSPF_IF_PARAM_CONFIGURED(oip, auth_simple)
569 && !OSPF_IF_PARAM_CONFIGURED(oip, auth_type)
570 && listcount(oip->auth_crypt) == 0
571 && ntohl(oip->network_lsa_seqnum) != OSPF_INITIAL_SEQUENCE_NUMBER) {
572 ospf_del_if_params(oip);
573 rn->info = NULL;
574 route_unlock_node(rn);
575 }
576 }
577
578 struct ospf_if_params *ospf_lookup_if_params(struct interface *ifp,
579 struct in_addr addr)
580 {
581 struct prefix_ipv4 p;
582 struct route_node *rn;
583
584 p.family = AF_INET;
585 p.prefixlen = IPV4_MAX_PREFIXLEN;
586 p.prefix = addr;
587
588 rn = route_node_lookup(IF_OIFS_PARAMS(ifp), (struct prefix *)&p);
589
590 if (rn) {
591 route_unlock_node(rn);
592 return rn->info;
593 }
594
595 return NULL;
596 }
597
598 struct ospf_if_params *ospf_get_if_params(struct interface *ifp,
599 struct in_addr addr)
600 {
601 struct prefix_ipv4 p;
602 struct route_node *rn;
603
604 p.family = AF_INET;
605 p.prefixlen = IPV4_MAX_PREFIXLEN;
606 p.prefix = addr;
607 apply_mask_ipv4(&p);
608
609 rn = route_node_get(IF_OIFS_PARAMS(ifp), (struct prefix *)&p);
610
611 if (rn->info == NULL)
612 rn->info = ospf_new_if_params();
613 else
614 route_unlock_node(rn);
615
616 return rn->info;
617 }
618
619 void ospf_if_update_params(struct interface *ifp, struct in_addr addr)
620 {
621 struct route_node *rn;
622 struct ospf_interface *oi;
623
624 for (rn = route_top(IF_OIFS(ifp)); rn; rn = route_next(rn)) {
625 if ((oi = rn->info) == NULL)
626 continue;
627
628 if (IPV4_ADDR_SAME(&oi->address->u.prefix4, &addr))
629 oi->params = ospf_lookup_if_params(
630 ifp, oi->address->u.prefix4);
631 }
632 }
633
634 int ospf_if_new_hook(struct interface *ifp)
635 {
636 int rc = 0;
637
638 ifp->info = XCALLOC(MTYPE_OSPF_IF_INFO, sizeof(struct ospf_if_info));
639
640 IF_OIFS(ifp) = route_table_init();
641 IF_OIFS_PARAMS(ifp) = route_table_init();
642
643 IF_DEF_PARAMS(ifp) = ospf_new_if_params();
644
645 SET_IF_PARAM(IF_DEF_PARAMS(ifp), transmit_delay);
646 IF_DEF_PARAMS(ifp)->transmit_delay = OSPF_TRANSMIT_DELAY_DEFAULT;
647
648 SET_IF_PARAM(IF_DEF_PARAMS(ifp), retransmit_interval);
649 IF_DEF_PARAMS(ifp)->retransmit_interval =
650 OSPF_RETRANSMIT_INTERVAL_DEFAULT;
651
652 SET_IF_PARAM(IF_DEF_PARAMS(ifp), priority);
653 IF_DEF_PARAMS(ifp)->priority = OSPF_ROUTER_PRIORITY_DEFAULT;
654
655 IF_DEF_PARAMS(ifp)->mtu_ignore = OSPF_MTU_IGNORE_DEFAULT;
656
657 SET_IF_PARAM(IF_DEF_PARAMS(ifp), v_hello);
658 IF_DEF_PARAMS(ifp)->v_hello = OSPF_HELLO_INTERVAL_DEFAULT;
659
660 SET_IF_PARAM(IF_DEF_PARAMS(ifp), fast_hello);
661 IF_DEF_PARAMS(ifp)->fast_hello = OSPF_FAST_HELLO_DEFAULT;
662
663 SET_IF_PARAM(IF_DEF_PARAMS(ifp), v_wait);
664 IF_DEF_PARAMS(ifp)->v_wait = OSPF_ROUTER_DEAD_INTERVAL_DEFAULT;
665
666 SET_IF_PARAM(IF_DEF_PARAMS(ifp), auth_simple);
667 memset(IF_DEF_PARAMS(ifp)->auth_simple, 0, OSPF_AUTH_SIMPLE_SIZE);
668
669 SET_IF_PARAM(IF_DEF_PARAMS(ifp), auth_type);
670 IF_DEF_PARAMS(ifp)->auth_type = OSPF_AUTH_NOTSET;
671
672 rc = ospf_opaque_new_if(ifp);
673 return rc;
674 }
675
676 static int ospf_if_delete_hook(struct interface *ifp)
677 {
678 int rc = 0;
679 struct route_node *rn;
680 rc = ospf_opaque_del_if(ifp);
681
682 route_table_finish(IF_OIFS(ifp));
683
684 for (rn = route_top(IF_OIFS_PARAMS(ifp)); rn; rn = route_next(rn))
685 if (rn->info)
686 ospf_del_if_params(rn->info);
687 route_table_finish(IF_OIFS_PARAMS(ifp));
688
689 ospf_del_if_params((struct ospf_if_params *)IF_DEF_PARAMS(ifp));
690 XFREE(MTYPE_OSPF_IF_INFO, ifp->info);
691 ifp->info = NULL;
692
693 return rc;
694 }
695
696 int ospf_if_is_enable(struct ospf_interface *oi)
697 {
698 if (!(if_is_loopback(oi->ifp) || if_is_vrf(oi->ifp)))
699 if (if_is_up(oi->ifp))
700 return 1;
701
702 return 0;
703 }
704
705 void ospf_if_set_multicast(struct ospf_interface *oi)
706 {
707 if ((oi->state > ISM_Loopback) && (oi->type != OSPF_IFTYPE_LOOPBACK)
708 && (oi->type != OSPF_IFTYPE_VIRTUALLINK)
709 && (OSPF_IF_PASSIVE_STATUS(oi) == OSPF_IF_ACTIVE)) {
710 /* The interface should belong to the OSPF-all-routers group. */
711 if (!OI_MEMBER_CHECK(oi, MEMBER_ALLROUTERS)
712 && (ospf_if_add_allspfrouters(oi->ospf, oi->address,
713 oi->ifp->ifindex)
714 >= 0))
715 /* Set the flag only if the system call to join
716 * succeeded. */
717 OI_MEMBER_JOINED(oi, MEMBER_ALLROUTERS);
718 } else {
719 /* The interface should NOT belong to the OSPF-all-routers
720 * group. */
721 if (OI_MEMBER_CHECK(oi, MEMBER_ALLROUTERS)) {
722 /* Only actually drop if this is the last reference */
723 if (OI_MEMBER_COUNT(oi, MEMBER_ALLROUTERS) == 1)
724 ospf_if_drop_allspfrouters(oi->ospf,
725 oi->address,
726 oi->ifp->ifindex);
727 /* Unset the flag regardless of whether the system call
728 to leave
729 the group succeeded, since it's much safer to assume
730 that
731 we are not a member. */
732 OI_MEMBER_LEFT(oi, MEMBER_ALLROUTERS);
733 }
734 }
735
736 if (((oi->type == OSPF_IFTYPE_BROADCAST)
737 || (oi->type == OSPF_IFTYPE_POINTOPOINT))
738 && ((oi->state == ISM_DR) || (oi->state == ISM_Backup))
739 && (OSPF_IF_PASSIVE_STATUS(oi) == OSPF_IF_ACTIVE)) {
740 /* The interface should belong to the OSPF-designated-routers
741 * group. */
742 if (!OI_MEMBER_CHECK(oi, MEMBER_DROUTERS)
743 && (ospf_if_add_alldrouters(oi->ospf, oi->address,
744 oi->ifp->ifindex)
745 >= 0))
746 /* Set the flag only if the system call to join
747 * succeeded. */
748 OI_MEMBER_JOINED(oi, MEMBER_DROUTERS);
749 } else {
750 /* The interface should NOT belong to the
751 * OSPF-designated-routers group */
752 if (OI_MEMBER_CHECK(oi, MEMBER_DROUTERS)) {
753 /* drop only if last reference */
754 if (OI_MEMBER_COUNT(oi, MEMBER_DROUTERS) == 1)
755 ospf_if_drop_alldrouters(oi->ospf, oi->address,
756 oi->ifp->ifindex);
757
758 /* Unset the flag regardless of whether the system call
759 to leave
760 the group succeeded, since it's much safer to assume
761 that
762 we are not a member. */
763 OI_MEMBER_LEFT(oi, MEMBER_DROUTERS);
764 }
765 }
766 }
767
768 int ospf_if_up(struct ospf_interface *oi)
769 {
770 if (oi == NULL)
771 return 0;
772
773 if (oi->type == OSPF_IFTYPE_LOOPBACK)
774 OSPF_ISM_EVENT_SCHEDULE(oi, ISM_LoopInd);
775 else {
776 OSPF_ISM_EVENT_SCHEDULE(oi, ISM_InterfaceUp);
777 }
778
779 return 1;
780 }
781
782 int ospf_if_down(struct ospf_interface *oi)
783 {
784 if (oi == NULL)
785 return 0;
786
787 OSPF_ISM_EVENT_EXECUTE(oi, ISM_InterfaceDown);
788 /* delete position in router LSA */
789 oi->lsa_pos_beg = 0;
790 oi->lsa_pos_end = 0;
791 /* Shutdown packet reception and sending */
792 ospf_if_stream_unset(oi);
793
794 return 1;
795 }
796
797
798 /* Virtual Link related functions. */
799
800 struct ospf_vl_data *ospf_vl_data_new(struct ospf_area *area,
801 struct in_addr vl_peer)
802 {
803 struct ospf_vl_data *vl_data;
804
805 vl_data = XCALLOC(MTYPE_OSPF_VL_DATA, sizeof(struct ospf_vl_data));
806
807 vl_data->vl_peer.s_addr = vl_peer.s_addr;
808 vl_data->vl_area_id = area->area_id;
809 vl_data->vl_area_id_fmt = area->area_id_fmt;
810
811 return vl_data;
812 }
813
814 void ospf_vl_data_free(struct ospf_vl_data *vl_data)
815 {
816 XFREE(MTYPE_OSPF_VL_DATA, vl_data);
817 }
818
819 unsigned int vlink_count = 0;
820
821 struct ospf_interface *ospf_vl_new(struct ospf *ospf,
822 struct ospf_vl_data *vl_data)
823 {
824 struct ospf_interface *voi;
825 struct interface *vi;
826 char ifname[INTERFACE_NAMSIZ];
827 struct ospf_area *area;
828 struct in_addr area_id;
829 struct connected *co;
830 struct prefix_ipv4 *p;
831
832 if (IS_DEBUG_OSPF_EVENT)
833 zlog_debug("ospf_vl_new(): Start");
834 if (vlink_count == OSPF_VL_MAX_COUNT) {
835 if (IS_DEBUG_OSPF_EVENT)
836 zlog_debug(
837 "ospf_vl_new(): Alarm: "
838 "cannot create more than OSPF_MAX_VL_COUNT virtual links");
839 return NULL;
840 }
841
842 if (IS_DEBUG_OSPF_EVENT)
843 zlog_debug(
844 "ospf_vl_new(): creating pseudo zebra interface vrf id %u",
845 ospf->vrf_id);
846
847 snprintf(ifname, sizeof(ifname), "VLINK%u", vlink_count);
848 vi = if_create(ifname, ospf->vrf_id);
849 /*
850 * if_create sets ZEBRA_INTERFACE_LINKDETECTION
851 * virtual links don't need this.
852 */
853 UNSET_FLAG(vi->status, ZEBRA_INTERFACE_LINKDETECTION);
854 co = connected_new();
855 co->ifp = vi;
856 listnode_add(vi->connected, co);
857
858 p = prefix_ipv4_new();
859 p->family = AF_INET;
860 p->prefix.s_addr = 0;
861 p->prefixlen = 0;
862
863 co->address = (struct prefix *)p;
864
865 voi = ospf_if_new(ospf, vi, co->address);
866 if (voi == NULL) {
867 if (IS_DEBUG_OSPF_EVENT)
868 zlog_debug(
869 "ospf_vl_new(): Alarm: OSPF int structure is not created");
870 return NULL;
871 }
872 voi->connected = co;
873 voi->vl_data = vl_data;
874 voi->ifp->mtu = OSPF_VL_MTU;
875 voi->type = OSPF_IFTYPE_VIRTUALLINK;
876
877 vlink_count++;
878 if (IS_DEBUG_OSPF_EVENT)
879 zlog_debug("ospf_vl_new(): Created name: %s", ifname);
880 if (IS_DEBUG_OSPF_EVENT)
881 zlog_debug("ospf_vl_new(): set if->name to %s", vi->name);
882
883 area_id.s_addr = 0;
884 area = ospf_area_get(ospf, area_id);
885 voi->area = area;
886
887 if (IS_DEBUG_OSPF_EVENT)
888 zlog_debug(
889 "ospf_vl_new(): set associated area to the backbone");
890
891 /* Add pseudo neighbor. */
892 ospf_nbr_self_reset(voi, voi->ospf->router_id);
893
894 ospf_area_add_if(voi->area, voi);
895
896 if (IS_DEBUG_OSPF_EVENT)
897 zlog_debug("ospf_vl_new(): Stop");
898 return voi;
899 }
900
901 static void ospf_vl_if_delete(struct ospf_vl_data *vl_data)
902 {
903 struct interface *ifp = vl_data->vl_oi->ifp;
904 vl_data->vl_oi->address->u.prefix4.s_addr = 0;
905 vl_data->vl_oi->address->prefixlen = 0;
906 ospf_if_free(vl_data->vl_oi);
907 if_delete(ifp);
908 vlink_count--;
909 }
910
911 /* for a defined area, count the number of configured vl
912 */
913 int ospf_vl_count(struct ospf *ospf, struct ospf_area *area)
914 {
915 int count = 0;
916 struct ospf_vl_data *vl_data;
917 struct listnode *node;
918
919 for (ALL_LIST_ELEMENTS_RO(ospf->vlinks, node, vl_data)) {
920 if (area
921 && !IPV4_ADDR_SAME(&vl_data->vl_area_id, &area->area_id))
922 continue;
923 count++;
924 }
925 return count;
926 }
927
928 /* Look up vl_data for given peer, optionally qualified to be in the
929 * specified area. NULL area returns first found..
930 */
931 struct ospf_vl_data *ospf_vl_lookup(struct ospf *ospf, struct ospf_area *area,
932 struct in_addr vl_peer)
933 {
934 struct ospf_vl_data *vl_data;
935 struct listnode *node;
936
937 if (IS_DEBUG_OSPF_EVENT) {
938 zlog_debug("%s: Looking for %s", __func__, inet_ntoa(vl_peer));
939 if (area)
940 zlog_debug("%s: in area %s", __func__,
941 inet_ntoa(area->area_id));
942 }
943
944 for (ALL_LIST_ELEMENTS_RO(ospf->vlinks, node, vl_data)) {
945 if (IS_DEBUG_OSPF_EVENT)
946 zlog_debug("%s: VL %s, peer %s", __func__,
947 vl_data->vl_oi->ifp->name,
948 inet_ntoa(vl_data->vl_peer));
949
950 if (area
951 && !IPV4_ADDR_SAME(&vl_data->vl_area_id, &area->area_id))
952 continue;
953
954 if (IPV4_ADDR_SAME(&vl_data->vl_peer, &vl_peer))
955 return vl_data;
956 }
957
958 return NULL;
959 }
960
961 static void ospf_vl_shutdown(struct ospf_vl_data *vl_data)
962 {
963 struct ospf_interface *oi;
964
965 if ((oi = vl_data->vl_oi) == NULL)
966 return;
967
968 oi->address->u.prefix4.s_addr = 0;
969 oi->address->prefixlen = 0;
970
971 UNSET_FLAG(oi->ifp->flags, IFF_UP);
972 /* OSPF_ISM_EVENT_SCHEDULE (oi, ISM_InterfaceDown); */
973 OSPF_ISM_EVENT_EXECUTE(oi, ISM_InterfaceDown);
974 }
975
976 void ospf_vl_add(struct ospf *ospf, struct ospf_vl_data *vl_data)
977 {
978 listnode_add(ospf->vlinks, vl_data);
979 hook_call(ospf_vl_add, vl_data);
980 }
981
982 void ospf_vl_delete(struct ospf *ospf, struct ospf_vl_data *vl_data)
983 {
984 ospf_vl_shutdown(vl_data);
985 ospf_vl_if_delete(vl_data);
986
987 hook_call(ospf_vl_delete, vl_data);
988 listnode_delete(ospf->vlinks, vl_data);
989
990 ospf_vl_data_free(vl_data);
991 }
992
993 static int ospf_vl_set_params(struct ospf_vl_data *vl_data, struct vertex *v)
994 {
995 int changed = 0;
996 struct ospf_interface *voi;
997 struct listnode *node;
998 struct vertex_parent *vp = NULL;
999 unsigned int i;
1000 struct router_lsa *rl;
1001
1002 voi = vl_data->vl_oi;
1003
1004 if (voi->output_cost != v->distance) {
1005
1006 voi->output_cost = v->distance;
1007 changed = 1;
1008 }
1009
1010 for (ALL_LIST_ELEMENTS_RO(v->parents, node, vp)) {
1011 vl_data->nexthop.oi = vp->nexthop->oi;
1012 vl_data->nexthop.router = vp->nexthop->router;
1013
1014 if (!IPV4_ADDR_SAME(&voi->address->u.prefix4,
1015 &vl_data->nexthop.oi->address->u.prefix4))
1016 changed = 1;
1017
1018 voi->address->u.prefix4 =
1019 vl_data->nexthop.oi->address->u.prefix4;
1020 voi->address->prefixlen =
1021 vl_data->nexthop.oi->address->prefixlen;
1022
1023 break; /* We take the first interface. */
1024 }
1025
1026 rl = (struct router_lsa *)v->lsa;
1027
1028 /* use SPF determined backlink index in struct vertex
1029 * for virtual link destination address
1030 */
1031 if (vp && vp->backlink >= 0) {
1032 if (!IPV4_ADDR_SAME(&vl_data->peer_addr,
1033 &rl->link[vp->backlink].link_data))
1034 changed = 1;
1035 vl_data->peer_addr = rl->link[vp->backlink].link_data;
1036 } else {
1037 /* This is highly odd, there is no backlink index
1038 * there should be due to the ospf_spf_has_link() check
1039 * in SPF. Lets warn and try pick a link anyway.
1040 */
1041 zlog_info("ospf_vl_set_params: No backlink for %s!",
1042 vl_data->vl_oi->ifp->name);
1043 for (i = 0; i < ntohs(rl->links); i++) {
1044 switch (rl->link[i].type) {
1045 case LSA_LINK_TYPE_VIRTUALLINK:
1046 if (IS_DEBUG_OSPF_EVENT)
1047 zlog_debug(
1048 "found back link through VL");
1049 /* fallthru */
1050 case LSA_LINK_TYPE_TRANSIT:
1051 case LSA_LINK_TYPE_POINTOPOINT:
1052 if (!IPV4_ADDR_SAME(&vl_data->peer_addr,
1053 &rl->link[i].link_data))
1054 changed = 1;
1055 vl_data->peer_addr = rl->link[i].link_data;
1056 }
1057 }
1058 }
1059
1060 if (IS_DEBUG_OSPF_EVENT)
1061 zlog_debug("%s: %s peer address: %s, cost: %d,%schanged",
1062 __func__, vl_data->vl_oi->ifp->name,
1063 inet_ntoa(vl_data->peer_addr), voi->output_cost,
1064 (changed ? " " : " un"));
1065
1066 return changed;
1067 }
1068
1069
1070 void ospf_vl_up_check(struct ospf_area *area, struct in_addr rid,
1071 struct vertex *v)
1072 {
1073 struct ospf *ospf = area->ospf;
1074 struct listnode *node;
1075 struct ospf_vl_data *vl_data;
1076 struct ospf_interface *oi;
1077
1078 if (IS_DEBUG_OSPF_EVENT) {
1079 zlog_debug("ospf_vl_up_check(): Start");
1080 zlog_debug("ospf_vl_up_check(): Router ID is %s",
1081 inet_ntoa(rid));
1082 zlog_debug("ospf_vl_up_check(): Area is %s",
1083 inet_ntoa(area->area_id));
1084 }
1085
1086 for (ALL_LIST_ELEMENTS_RO(ospf->vlinks, node, vl_data)) {
1087 if (IS_DEBUG_OSPF_EVENT) {
1088 zlog_debug("%s: considering VL, %s in area %s",
1089 __func__, vl_data->vl_oi->ifp->name,
1090 inet_ntoa(vl_data->vl_area_id));
1091 zlog_debug("%s: peer ID: %s", __func__,
1092 inet_ntoa(vl_data->vl_peer));
1093 }
1094
1095 if (IPV4_ADDR_SAME(&vl_data->vl_peer, &rid)
1096 && IPV4_ADDR_SAME(&vl_data->vl_area_id, &area->area_id)) {
1097 oi = vl_data->vl_oi;
1098 SET_FLAG(vl_data->flags, OSPF_VL_FLAG_APPROVED);
1099
1100 if (IS_DEBUG_OSPF_EVENT)
1101 zlog_debug(
1102 "ospf_vl_up_check(): this VL matched");
1103
1104 if (oi->state == ISM_Down) {
1105 if (IS_DEBUG_OSPF_EVENT)
1106 zlog_debug(
1107 "ospf_vl_up_check(): VL is down, waking it up");
1108 SET_FLAG(oi->ifp->flags, IFF_UP);
1109 OSPF_ISM_EVENT_EXECUTE(oi, ISM_InterfaceUp);
1110 }
1111
1112 if (ospf_vl_set_params(vl_data, v)) {
1113 if (IS_DEBUG_OSPF(ism, ISM_EVENTS))
1114 zlog_debug(
1115 "ospf_vl_up_check: VL cost change,"
1116 " scheduling router lsa refresh");
1117 if (ospf->backbone)
1118 ospf_router_lsa_update_area(
1119 ospf->backbone);
1120 else if (IS_DEBUG_OSPF(ism, ISM_EVENTS))
1121 zlog_debug(
1122 "ospf_vl_up_check: VL cost change, no backbone!");
1123 }
1124 }
1125 }
1126 }
1127
1128 void ospf_vl_unapprove(struct ospf *ospf)
1129 {
1130 struct listnode *node;
1131 struct ospf_vl_data *vl_data;
1132
1133 for (ALL_LIST_ELEMENTS_RO(ospf->vlinks, node, vl_data))
1134 UNSET_FLAG(vl_data->flags, OSPF_VL_FLAG_APPROVED);
1135 }
1136
1137 void ospf_vl_shut_unapproved(struct ospf *ospf)
1138 {
1139 struct listnode *node, *nnode;
1140 struct ospf_vl_data *vl_data;
1141
1142 for (ALL_LIST_ELEMENTS(ospf->vlinks, node, nnode, vl_data))
1143 if (!CHECK_FLAG(vl_data->flags, OSPF_VL_FLAG_APPROVED))
1144 ospf_vl_shutdown(vl_data);
1145 }
1146
1147 int ospf_full_virtual_nbrs(struct ospf_area *area)
1148 {
1149 if (IS_DEBUG_OSPF_EVENT) {
1150 zlog_debug(
1151 "counting fully adjacent virtual neighbors in area %s",
1152 inet_ntoa(area->area_id));
1153 zlog_debug("there are %d of them", area->full_vls);
1154 }
1155
1156 return area->full_vls;
1157 }
1158
1159 int ospf_vls_in_area(struct ospf_area *area)
1160 {
1161 struct listnode *node;
1162 struct ospf_vl_data *vl_data;
1163 int c = 0;
1164
1165 for (ALL_LIST_ELEMENTS_RO(area->ospf->vlinks, node, vl_data))
1166 if (IPV4_ADDR_SAME(&vl_data->vl_area_id, &area->area_id))
1167 c++;
1168
1169 return c;
1170 }
1171
1172
1173 struct crypt_key *ospf_crypt_key_new(void)
1174 {
1175 return XCALLOC(MTYPE_OSPF_CRYPT_KEY, sizeof(struct crypt_key));
1176 }
1177
1178 void ospf_crypt_key_add(struct list *crypt, struct crypt_key *ck)
1179 {
1180 listnode_add(crypt, ck);
1181 }
1182
1183 struct crypt_key *ospf_crypt_key_lookup(struct list *auth_crypt, uint8_t key_id)
1184 {
1185 struct listnode *node;
1186 struct crypt_key *ck;
1187
1188 for (ALL_LIST_ELEMENTS_RO(auth_crypt, node, ck))
1189 if (ck->key_id == key_id)
1190 return ck;
1191
1192 return NULL;
1193 }
1194
1195 int ospf_crypt_key_delete(struct list *auth_crypt, uint8_t key_id)
1196 {
1197 struct listnode *node, *nnode;
1198 struct crypt_key *ck;
1199
1200 for (ALL_LIST_ELEMENTS(auth_crypt, node, nnode, ck)) {
1201 if (ck->key_id == key_id) {
1202 listnode_delete(auth_crypt, ck);
1203 XFREE(MTYPE_OSPF_CRYPT_KEY, ck);
1204 return 1;
1205 }
1206 }
1207
1208 return 0;
1209 }
1210
1211 uint8_t ospf_default_iftype(struct interface *ifp)
1212 {
1213 if (if_is_pointopoint(ifp))
1214 return OSPF_IFTYPE_POINTOPOINT;
1215 else if (if_is_loopback(ifp) || if_is_vrf(ifp))
1216 return OSPF_IFTYPE_LOOPBACK;
1217 else
1218 return OSPF_IFTYPE_BROADCAST;
1219 }
1220
1221 void ospf_if_init(void)
1222 {
1223 /* Initialize Zebra interface data structure. */
1224 hook_register_prio(if_add, 0, ospf_if_new_hook);
1225 hook_register_prio(if_del, 0, ospf_if_delete_hook);
1226 }