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