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