]> git.proxmox.com Git - mirror_frr.git/blame - ospfd/ospf_lsa.c
nhrpd: Fix multicast crash due to dangling references to nhrp peers
[mirror_frr.git] / ospfd / ospf_lsa.c
CommitLineData
718e3744 1/*
2 * OSPF Link State Advertisement
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 it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
10 * 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
cbf3e3eb 24#include "monotime.h"
718e3744 25#include "linklist.h"
26#include "prefix.h"
27#include "if.h"
28#include "table.h"
29#include "memory.h"
30#include "stream.h"
31#include "log.h"
32#include "thread.h"
33#include "hash.h"
d62a17ae 34#include "sockunion.h" /* for inet_aton() */
6a270cd9 35#include "checksum.h"
5920b3eb 36#include "network.h"
718e3744 37
38#include "ospfd/ospfd.h"
39#include "ospfd/ospf_interface.h"
40#include "ospfd/ospf_ism.h"
41#include "ospfd/ospf_asbr.h"
42#include "ospfd/ospf_lsa.h"
43#include "ospfd/ospf_lsdb.h"
44#include "ospfd/ospf_neighbor.h"
45#include "ospfd/ospf_nsm.h"
46#include "ospfd/ospf_flood.h"
47#include "ospfd/ospf_packet.h"
48#include "ospfd/ospf_spf.h"
49#include "ospfd/ospf_dump.h"
50#include "ospfd/ospf_route.h"
51#include "ospfd/ospf_ase.h"
52#include "ospfd/ospf_zebra.h"
493472ba 53#include "ospfd/ospf_abr.h"
542a208f 54#include "ospfd/ospf_errors.h"
6b0655a2 55
d7c0a89a 56uint32_t get_metric(uint8_t *metric)
718e3744 57{
d7c0a89a 58 uint32_t m;
d62a17ae 59 m = metric[0];
60 m = (m << 8) + metric[1];
61 m = (m << 8) + metric[2];
62 return m;
718e3744 63}
64
6b0655a2 65
d62a17ae 66struct timeval int2tv(int a)
b6927875 67{
d62a17ae 68 struct timeval ret;
b6927875 69
d62a17ae 70 ret.tv_sec = a;
71 ret.tv_usec = 0;
b6927875 72
d62a17ae 73 return ret;
b6927875
DS
74}
75
d62a17ae 76struct timeval msec2tv(int a)
718e3744 77{
d62a17ae 78 struct timeval ret;
718e3744 79
d62a17ae 80 ret.tv_sec = a / 1000;
81 ret.tv_usec = (a % 1000) * 1000;
718e3744 82
d62a17ae 83 return ret;
718e3744 84}
85
d62a17ae 86int ospf_lsa_refresh_delay(struct ospf_lsa *lsa)
718e3744 87{
d62a17ae 88 struct timeval delta;
89 int delay = 0;
718e3744 90
d62a17ae 91 if (monotime_since(&lsa->tv_orig, &delta)
92 < OSPF_MIN_LS_INTERVAL * 1000LL) {
93 struct timeval minv = msec2tv(OSPF_MIN_LS_INTERVAL);
94 timersub(&minv, &delta, &minv);
cbf3e3eb 95
d62a17ae 96 /* TBD: remove padding to full sec, return timeval instead */
97 delay = minv.tv_sec + !!minv.tv_usec;
718e3744 98
d62a17ae 99 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
100 zlog_debug(
96b663a3
MS
101 "LSA[Type%d:%pI4]: Refresh timer delay %d seconds",
102 lsa->data->type, &lsa->data->id,
d62a17ae 103 delay);
718e3744 104
d62a17ae 105 assert(delay > 0);
106 }
718e3744 107
d62a17ae 108 return delay;
718e3744 109}
110
6b0655a2 111
d62a17ae 112int get_age(struct ospf_lsa *lsa)
718e3744 113{
d62a17ae 114 struct timeval rel;
718e3744 115
d62a17ae 116 monotime_since(&lsa->tv_recv, &rel);
117 return ntohs(lsa->data->ls_age) + rel.tv_sec;
718e3744 118}
119
6b0655a2 120
718e3744 121/* Fletcher Checksum -- Refer to RFC1008. */
718e3744 122
d62a17ae 123/* All the offsets are zero-based. The offsets in the RFC1008 are
6a270cd9 124 one-based. */
d7c0a89a 125uint16_t ospf_lsa_checksum(struct lsa_header *lsa)
718e3744 126{
c4efd0f4 127 uint8_t *buffer = &lsa->options;
d7c0a89a 128 int options_offset = buffer - (uint8_t *)&lsa->ls_age; /* should be 2 */
718e3744 129
d62a17ae 130 /* Skip the AGE field */
d7c0a89a 131 uint16_t len = ntohs(lsa->length) - options_offset;
718e3744 132
d62a17ae 133 /* Checksum offset starts from "options" field, not the beginning of the
134 lsa_header struct. The offset is 14, rather than 16. */
d7c0a89a 135 int checksum_offset = (uint8_t *)&lsa->checksum - buffer;
718e3744 136
d62a17ae 137 return fletcher_checksum(buffer, len, checksum_offset);
718e3744 138}
139
d62a17ae 140int ospf_lsa_checksum_valid(struct lsa_header *lsa)
d8a4e42b 141{
c4efd0f4 142 uint8_t *buffer = &lsa->options;
d7c0a89a 143 int options_offset = buffer - (uint8_t *)&lsa->ls_age; /* should be 2 */
d8a4e42b 144
d62a17ae 145 /* Skip the AGE field */
d7c0a89a 146 uint16_t len = ntohs(lsa->length) - options_offset;
d8a4e42b 147
d62a17ae 148 return (fletcher_checksum(buffer, len, FLETCHER_CHECKSUM_VALIDATE)
149 == 0);
d8a4e42b
JR
150}
151
718e3744 152
718e3744 153/* Create OSPF LSA. */
4d762f26 154struct ospf_lsa *ospf_lsa_new(void)
718e3744 155{
d62a17ae 156 struct ospf_lsa *new;
718e3744 157
d62a17ae 158 new = XCALLOC(MTYPE_OSPF_LSA, sizeof(struct ospf_lsa));
718e3744 159
d62a17ae 160 new->flags = 0;
161 new->lock = 1;
162 new->retransmit_counter = 0;
163 monotime(&new->tv_recv);
164 new->tv_orig = new->tv_recv;
165 new->refresh_list = -1;
b5a8894d 166 new->vrf_id = VRF_DEFAULT;
df074ec3 167 new->to_be_acknowledged = 0;
d62a17ae 168
169 return new;
718e3744 170}
171
5b3d4186
DS
172struct ospf_lsa *ospf_lsa_new_and_data(size_t size)
173{
174 struct ospf_lsa *new;
175
176 new = ospf_lsa_new();
177 new->data = ospf_lsa_data_new(size);
178
179 return new;
180}
181
718e3744 182/* Duplicate OSPF LSA. */
d62a17ae 183struct ospf_lsa *ospf_lsa_dup(struct ospf_lsa *lsa)
718e3744 184{
d62a17ae 185 struct ospf_lsa *new;
718e3744 186
d62a17ae 187 if (lsa == NULL)
188 return NULL;
718e3744 189
d62a17ae 190 new = XCALLOC(MTYPE_OSPF_LSA, sizeof(struct ospf_lsa));
718e3744 191
d62a17ae 192 memcpy(new, lsa, sizeof(struct ospf_lsa));
193 UNSET_FLAG(new->flags, OSPF_LSA_DISCARD);
194 new->lock = 1;
195 new->retransmit_counter = 0;
196 new->data = ospf_lsa_data_dup(lsa->data);
718e3744 197
d62a17ae 198 /* kevinm: Clear the refresh_list, otherwise there are going
199 to be problems when we try to remove the LSA from the
200 queue (which it's not a member of.)
201 XXX: Should we add the LSA to the refresh_list queue? */
202 new->refresh_list = -1;
f2c80652 203
d62a17ae 204 if (IS_DEBUG_OSPF(lsa, LSA))
205 zlog_debug("LSA: duplicated %p (new: %p)", (void *)lsa,
206 (void *)new);
f2c80652 207
d62a17ae 208 return new;
718e3744 209}
210
211/* Free OSPF LSA. */
d62a17ae 212void ospf_lsa_free(struct ospf_lsa *lsa)
718e3744 213{
d62a17ae 214 assert(lsa->lock == 0);
215
216 if (IS_DEBUG_OSPF(lsa, LSA))
217 zlog_debug("LSA: freed %p", (void *)lsa);
718e3744 218
d62a17ae 219 /* Delete LSA data. */
220 if (lsa->data != NULL)
221 ospf_lsa_data_free(lsa->data);
718e3744 222
d62a17ae 223 assert(lsa->refresh_list < 0);
718e3744 224
d62a17ae 225 memset(lsa, 0, sizeof(struct ospf_lsa));
226 XFREE(MTYPE_OSPF_LSA, lsa);
718e3744 227}
228
229/* Lock LSA. */
d62a17ae 230struct ospf_lsa *ospf_lsa_lock(struct ospf_lsa *lsa)
718e3744 231{
d62a17ae 232 lsa->lock++;
233 return lsa;
718e3744 234}
235
236/* Unlock LSA. */
d62a17ae 237void ospf_lsa_unlock(struct ospf_lsa **lsa)
718e3744 238{
d62a17ae 239 /* This is sanity check. */
240 if (!lsa || !*lsa)
241 return;
718e3744 242
d62a17ae 243 (*lsa)->lock--;
718e3744 244
d62a17ae 245 assert((*lsa)->lock >= 0);
246
247 if ((*lsa)->lock == 0) {
248 assert(CHECK_FLAG((*lsa)->flags, OSPF_LSA_DISCARD));
249 ospf_lsa_free(*lsa);
250 *lsa = NULL;
251 }
718e3744 252}
253
254/* Check discard flag. */
d62a17ae 255void ospf_lsa_discard(struct ospf_lsa *lsa)
718e3744 256{
d62a17ae 257 if (!CHECK_FLAG(lsa->flags, OSPF_LSA_DISCARD)) {
258 SET_FLAG(lsa->flags, OSPF_LSA_DISCARD);
259 ospf_lsa_unlock(&lsa);
260 }
718e3744 261}
262
263/* Create LSA data. */
d62a17ae 264struct lsa_header *ospf_lsa_data_new(size_t size)
718e3744 265{
d62a17ae 266 return XCALLOC(MTYPE_OSPF_LSA_DATA, size);
718e3744 267}
268
269/* Duplicate LSA data. */
d62a17ae 270struct lsa_header *ospf_lsa_data_dup(struct lsa_header *lsah)
718e3744 271{
d62a17ae 272 struct lsa_header *new;
718e3744 273
d62a17ae 274 new = ospf_lsa_data_new(ntohs(lsah->length));
275 memcpy(new, lsah, ntohs(lsah->length));
718e3744 276
d62a17ae 277 return new;
718e3744 278}
279
280/* Free LSA data. */
d62a17ae 281void ospf_lsa_data_free(struct lsa_header *lsah)
718e3744 282{
d62a17ae 283 if (IS_DEBUG_OSPF(lsa, LSA))
96b663a3
MS
284 zlog_debug("LSA[Type%d:%pI4]: data freed %p", lsah->type,
285 &lsah->id, (void *)lsah);
718e3744 286
d62a17ae 287 XFREE(MTYPE_OSPF_LSA_DATA, lsah);
718e3744 288}
289
6b0655a2 290
718e3744 291/* LSA general functions. */
292
d62a17ae 293const char *dump_lsa_key(struct ospf_lsa *lsa)
718e3744 294{
2b64873d 295 static char buf[sizeof("Type255,id(255.255.255.255),ar(255.255.255.255)")+1];
d62a17ae 296 struct lsa_header *lsah;
718e3744 297
d62a17ae 298 if (lsa != NULL && (lsah = lsa->data) != NULL) {
299 char id[INET_ADDRSTRLEN], ar[INET_ADDRSTRLEN];
96b663a3
MS
300 inet_ntop(AF_INET, &lsah->id, id, sizeof(id));
301 inet_ntop(AF_INET, &lsah->adv_router, ar, sizeof(ar));
718e3744 302
772270f3
QY
303 snprintf(buf, sizeof(buf), "Type%d,id(%s),ar(%s)", lsah->type,
304 id, ar);
d62a17ae 305 } else
a1d6bbb1 306 strlcpy(buf, "NULL", sizeof(buf));
718e3744 307
d62a17ae 308 return buf;
718e3744 309}
310
d7c0a89a 311uint32_t lsa_seqnum_increment(struct ospf_lsa *lsa)
718e3744 312{
d7c0a89a 313 uint32_t seqnum;
718e3744 314
d62a17ae 315 seqnum = ntohl(lsa->data->ls_seqnum) + 1;
718e3744 316
d62a17ae 317 return htonl(seqnum);
718e3744 318}
319
d7c0a89a 320void lsa_header_set(struct stream *s, uint8_t options, uint8_t type,
d62a17ae 321 struct in_addr id, struct in_addr router_id)
718e3744 322{
d62a17ae 323 struct lsa_header *lsah;
718e3744 324
d62a17ae 325 lsah = (struct lsa_header *)STREAM_DATA(s);
718e3744 326
d62a17ae 327 lsah->ls_age = htons(OSPF_LSA_INITIAL_AGE);
328 lsah->options = options;
329 lsah->type = type;
330 lsah->id = id;
331 lsah->adv_router = router_id;
332 lsah->ls_seqnum = htonl(OSPF_INITIAL_SEQUENCE_NUMBER);
718e3744 333
d62a17ae 334 stream_forward_endp(s, OSPF_LSA_HEADER_SIZE);
718e3744 335}
6b0655a2 336
68980084 337
718e3744 338/* router-LSA related functions. */
339/* Get router-LSA flags. */
7fd0729f 340uint8_t router_lsa_flags(struct ospf_area *area)
d62a17ae 341{
d7c0a89a 342 uint8_t flags;
d62a17ae 343
344 flags = area->ospf->flags;
345
346 /* Set virtual link flag. */
347 if (ospf_full_virtual_nbrs(area))
348 SET_FLAG(flags, ROUTER_LSA_VIRTUAL);
349 else
350 /* Just sanity check */
351 UNSET_FLAG(flags, ROUTER_LSA_VIRTUAL);
352
353 /* Set Shortcut ABR behabiour flag. */
354 UNSET_FLAG(flags, ROUTER_LSA_SHORTCUT);
355 if (area->ospf->abr_type == OSPF_ABR_SHORTCUT)
356 if (!OSPF_IS_AREA_BACKBONE(area))
357 if ((area->shortcut_configured == OSPF_SHORTCUT_DEFAULT
358 && area->ospf->backbone == NULL)
359 || area->shortcut_configured
360 == OSPF_SHORTCUT_ENABLE)
361 SET_FLAG(flags, ROUTER_LSA_SHORTCUT);
362
363 /* ASBR can't exit in stub area. */
364 if (area->external_routing == OSPF_AREA_STUB)
365 UNSET_FLAG(flags, ROUTER_LSA_EXTERNAL);
366 /* If ASBR set External flag */
367 else if (IS_OSPF_ASBR(area->ospf))
368 SET_FLAG(flags, ROUTER_LSA_EXTERNAL);
369
370 /* Set ABR dependent flags */
371 if (IS_OSPF_ABR(area->ospf)) {
372 SET_FLAG(flags, ROUTER_LSA_BORDER);
373 /* If Area is NSSA and we are both ABR and unconditional
374 * translator,
375 * set Nt bit to inform other routers.
376 */
377 if ((area->external_routing == OSPF_AREA_NSSA)
378 && (area->NSSATranslatorRole == OSPF_NSSA_ROLE_ALWAYS))
379 SET_FLAG(flags, ROUTER_LSA_NT);
380 }
381 return flags;
718e3744 382}
383
384/* Lookup neighbor other than myself.
385 And check neighbor count,
386 Point-to-Point link must have only 1 neighbor. */
d62a17ae 387struct ospf_neighbor *ospf_nbr_lookup_ptop(struct ospf_interface *oi)
718e3744 388{
d62a17ae 389 struct ospf_neighbor *nbr = NULL;
390 struct route_node *rn;
718e3744 391
d62a17ae 392 /* Search neighbor, there must be one of two nbrs. */
393 for (rn = route_top(oi->nbrs); rn; rn = route_next(rn))
394 if ((nbr = rn->info))
395 if (!IPV4_ADDR_SAME(&nbr->router_id,
396 &oi->ospf->router_id))
397 if (nbr->state == NSM_Full) {
398 route_unlock_node(rn);
399 break;
400 }
718e3744 401
d62a17ae 402 /* PtoP link must have only 1 neighbor. */
403 if (ospf_nbr_count(oi, 0) > 1)
cf444bcf 404 flog_warn(EC_OSPF_PTP_NEIGHBOR,
13ab4921 405 "Point-to-Point link has more than 1 neighobrs.");
718e3744 406
d62a17ae 407 return nbr;
718e3744 408}
409
88d6cf37 410/* Determine cost of link, taking RFC3137 stub-router support into
411 * consideration
412 */
d7c0a89a 413static uint16_t ospf_link_cost(struct ospf_interface *oi)
88d6cf37 414{
d62a17ae 415 /* RFC3137 stub router support */
416 if (!CHECK_FLAG(oi->area->stub_router_state, OSPF_AREA_IS_STUB_ROUTED))
417 return oi->output_cost;
418 else
419 return OSPF_OUTPUT_COST_INFINITE;
88d6cf37 420}
421
718e3744 422/* Set a link information. */
7fd0729f
G
423char link_info_set(struct stream **s, struct in_addr id, struct in_addr data,
424 uint8_t type, uint8_t tos, uint16_t cost)
d62a17ae 425{
426 /* LSA stream is initially allocated to OSPF_MAX_LSA_SIZE, suits
427 * vast majority of cases. Some rare routers with lots of links need
428 * more.
429 * we try accomodate those here.
430 */
db3c830a 431 if (STREAM_WRITEABLE(*s) < OSPF_ROUTER_LSA_LINK_SIZE) {
d62a17ae 432 size_t ret = OSPF_MAX_LSA_SIZE;
433
434 /* Can we enlarge the stream still? */
db3c830a 435 if (STREAM_SIZE(*s) == OSPF_MAX_LSA_SIZE) {
d62a17ae 436 /* we futz the size here for simplicity, really we need
437 * to account
438 * for just:
0d6f7fd6 439 * IP Header - (sizeof(struct ip))
d62a17ae 440 * OSPF Header - OSPF_HEADER_SIZE
441 * LSA Header - OSPF_LSA_HEADER_SIZE
442 * MD5 auth data, if MD5 is configured -
443 * OSPF_AUTH_MD5_SIZE.
444 *
445 * Simpler just to subtract OSPF_MAX_LSA_SIZE though.
446 */
db3c830a 447 ret = stream_resize_inplace(
9d303b37 448 s, OSPF_MAX_PACKET_SIZE - OSPF_MAX_LSA_SIZE);
d62a17ae 449 }
450
451 if (ret == OSPF_MAX_LSA_SIZE) {
13ab4921 452 flog_warn(
cf444bcf 453 EC_OSPF_LSA_SIZE,
d62a17ae 454 "%s: Out of space in LSA stream, left %zd, size %zd",
db3c830a
DS
455 __func__, STREAM_WRITEABLE(*s),
456 STREAM_SIZE(*s));
d62a17ae 457 return 0;
458 }
459 }
460
461 /* TOS based routing is not supported. */
db3c830a
DS
462 stream_put_ipv4(*s, id.s_addr); /* Link ID. */
463 stream_put_ipv4(*s, data.s_addr); /* Link Data. */
464 stream_putc(*s, type); /* Link Type. */
465 stream_putc(*s, tos); /* TOS = 0. */
466 stream_putw(*s, cost); /* Link Cost. */
d62a17ae 467
468 return 1;
718e3744 469}
470
e4529636 471/* Describe Point-to-Point link (Section 12.4.1.1). */
db3c830a 472static int lsa_link_ptop_set(struct stream **s, struct ospf_interface *oi)
d62a17ae 473{
474 int links = 0;
475 struct ospf_neighbor *nbr;
476 struct in_addr id, mask, data;
d7c0a89a 477 uint16_t cost = ospf_link_cost(oi);
d62a17ae 478
479 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
480 zlog_debug("LSA[Type1]: Set link Point-to-Point");
481
482 if ((nbr = ospf_nbr_lookup_ptop(oi)))
483 if (nbr->state == NSM_Full) {
484 if (CHECK_FLAG(oi->connected->flags,
485 ZEBRA_IFA_UNNUMBERED)) {
486 /* For unnumbered point-to-point networks, the
487 Link Data field
488 should specify the interface's MIB-II ifIndex
489 value. */
490 data.s_addr = htonl(oi->ifp->ifindex);
491 links += link_info_set(
492 s, nbr->router_id, data,
493 LSA_LINK_TYPE_POINTOPOINT, 0, cost);
494 } else {
495 links += link_info_set(
496 s, nbr->router_id,
497 oi->address->u.prefix4,
498 LSA_LINK_TYPE_POINTOPOINT, 0, cost);
499 }
500 }
501
502 /* no need for a stub link for unnumbered interfaces */
503 if (!CHECK_FLAG(oi->connected->flags, ZEBRA_IFA_UNNUMBERED)) {
504 /* Regardless of the state of the neighboring router, we must
505 add a Type 3 link (stub network).
506 N.B. Options 1 & 2 share basically the same logic. */
507 masklen2ip(oi->address->prefixlen, &mask);
508 id.s_addr = CONNECTED_PREFIX(oi->connected)->u.prefix4.s_addr
509 & mask.s_addr;
510 links += link_info_set(s, id, mask, LSA_LINK_TYPE_STUB, 0,
511 oi->output_cost);
512 }
513
514 return links;
718e3744 515}
516
517/* Describe Broadcast Link. */
db3c830a 518static int lsa_link_broadcast_set(struct stream **s, struct ospf_interface *oi)
d62a17ae 519{
520 struct ospf_neighbor *dr;
521 struct in_addr id, mask;
d7c0a89a 522 uint16_t cost = ospf_link_cost(oi);
d62a17ae 523
524 /* Describe Type 3 Link. */
525 if (oi->state == ISM_Waiting) {
526 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
527 zlog_debug(
3efd0893 528 "LSA[Type1]: Interface %s is in state Waiting. Adding stub interface",
d62a17ae 529 oi->ifp->name);
530 masklen2ip(oi->address->prefixlen, &mask);
531 id.s_addr = oi->address->u.prefix4.s_addr & mask.s_addr;
532 return link_info_set(s, id, mask, LSA_LINK_TYPE_STUB, 0,
533 oi->output_cost);
534 }
535
536 dr = ospf_nbr_lookup_by_addr(oi->nbrs, &DR(oi));
537 /* Describe Type 2 link. */
9d303b37
DL
538 if (dr && (dr->state == NSM_Full
539 || IPV4_ADDR_SAME(&oi->address->u.prefix4, &DR(oi)))
d62a17ae 540 && ospf_nbr_count(oi, NSM_Full) > 0) {
541 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
542 zlog_debug(
3efd0893 543 "LSA[Type1]: Interface %s has a DR. Adding transit interface",
d62a17ae 544 oi->ifp->name);
545 return link_info_set(s, DR(oi), oi->address->u.prefix4,
546 LSA_LINK_TYPE_TRANSIT, 0, cost);
547 }
548 /* Describe type 3 link. */
549 else {
550 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
551 zlog_debug(
3efd0893 552 "LSA[Type1]: Interface %s has no DR. Adding stub interface",
d62a17ae 553 oi->ifp->name);
554 masklen2ip(oi->address->prefixlen, &mask);
555 id.s_addr = oi->address->u.prefix4.s_addr & mask.s_addr;
556 return link_info_set(s, id, mask, LSA_LINK_TYPE_STUB, 0,
557 oi->output_cost);
558 }
559}
560
db3c830a 561static int lsa_link_loopback_set(struct stream **s, struct ospf_interface *oi)
d62a17ae 562{
563 struct in_addr id, mask;
564
565 /* Describe Type 3 Link. */
566 if (oi->state != ISM_Loopback)
567 return 0;
568
569 mask.s_addr = 0xffffffff;
570 id.s_addr = oi->address->u.prefix4.s_addr;
571 return link_info_set(s, id, mask, LSA_LINK_TYPE_STUB, 0, 0);
718e3744 572}
573
574/* Describe Virtual Link. */
db3c830a
DS
575static int lsa_link_virtuallink_set(struct stream **s,
576 struct ospf_interface *oi)
718e3744 577{
d62a17ae 578 struct ospf_neighbor *nbr;
d7c0a89a 579 uint16_t cost = ospf_link_cost(oi);
718e3744 580
d62a17ae 581 if (oi->state == ISM_PointToPoint)
582 if ((nbr = ospf_nbr_lookup_ptop(oi)))
583 if (nbr->state == NSM_Full) {
584 return link_info_set(s, nbr->router_id,
585 oi->address->u.prefix4,
586 LSA_LINK_TYPE_VIRTUALLINK,
587 0, cost);
588 }
718e3744 589
d62a17ae 590 return 0;
718e3744 591}
592
593#define lsa_link_nbma_set(S,O) lsa_link_broadcast_set (S, O)
594
d62a17ae 595/* this function add for support point-to-multipoint ,see rfc2328
7afa08da 59612.4.1.4.*/
597/* from "edward rrr" <edward_rrr@hotmail.com>
598 http://marc.theaimsgroup.com/?l=zebra&m=100739222210507&w=2 */
db3c830a 599static int lsa_link_ptomp_set(struct stream **s, struct ospf_interface *oi)
d62a17ae 600{
601 int links = 0;
602 struct route_node *rn;
603 struct ospf_neighbor *nbr = NULL;
604 struct in_addr id, mask;
d7c0a89a 605 uint16_t cost = ospf_link_cost(oi);
d62a17ae 606
607 mask.s_addr = 0xffffffff;
608 id.s_addr = oi->address->u.prefix4.s_addr;
609 links += link_info_set(s, id, mask, LSA_LINK_TYPE_STUB, 0, 0);
610
611 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
612 zlog_debug("PointToMultipoint: running ptomultip_set");
613
614 /* Search neighbor, */
615 for (rn = route_top(oi->nbrs); rn; rn = route_next(rn))
616 if ((nbr = rn->info) != NULL)
617 /* Ignore myself. */
618 if (!IPV4_ADDR_SAME(&nbr->router_id,
619 &oi->ospf->router_id))
620 if (nbr->state == NSM_Full)
621
622 {
623 links += link_info_set(
624 s, nbr->router_id,
625 oi->address->u.prefix4,
626 LSA_LINK_TYPE_POINTOPOINT, 0,
627 cost);
628 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
629 zlog_debug(
96b663a3
MS
630 "PointToMultipoint: set link to %pI4",
631 &oi->address->u.prefix4);
d62a17ae 632 }
633
634 return links;
7afa08da 635}
636
718e3744 637/* Set router-LSA link information. */
db3c830a 638static int router_lsa_link_set(struct stream **s, struct ospf_area *area)
d62a17ae 639{
640 struct listnode *node;
641 struct ospf_interface *oi;
642 int links = 0;
643
644 for (ALL_LIST_ELEMENTS_RO(area->oiflist, node, oi)) {
645 struct interface *ifp = oi->ifp;
646
647 /* Check interface is up, OSPF is enable. */
648 if (if_is_operative(ifp)) {
649 if (oi->state != ISM_Down) {
650 oi->lsa_pos_beg = links;
651 /* Describe each link. */
652 switch (oi->type) {
653 case OSPF_IFTYPE_POINTOPOINT:
654 links += lsa_link_ptop_set(s, oi);
655 break;
656 case OSPF_IFTYPE_BROADCAST:
657 links += lsa_link_broadcast_set(s, oi);
658 break;
659 case OSPF_IFTYPE_NBMA:
660 links += lsa_link_nbma_set(s, oi);
661 break;
662 case OSPF_IFTYPE_POINTOMULTIPOINT:
663 links += lsa_link_ptomp_set(s, oi);
664 break;
665 case OSPF_IFTYPE_VIRTUALLINK:
666 links +=
667 lsa_link_virtuallink_set(s, oi);
668 break;
669 case OSPF_IFTYPE_LOOPBACK:
670 links += lsa_link_loopback_set(s, oi);
671 }
672 oi->lsa_pos_end = links;
673 }
718e3744 674 }
718e3744 675 }
718e3744 676
d62a17ae 677 return links;
718e3744 678}
679
680/* Set router-LSA body. */
7fd0729f 681void ospf_router_lsa_body_set(struct stream **s, struct ospf_area *area)
d62a17ae 682{
683 unsigned long putp;
d7c0a89a 684 uint16_t cnt;
d62a17ae 685
686 /* Set flags. */
db3c830a 687 stream_putc(*s, router_lsa_flags(area));
d62a17ae 688
689 /* Set Zero fields. */
db3c830a 690 stream_putc(*s, 0);
d62a17ae 691
692 /* Keep pointer to # links. */
db3c830a 693 putp = stream_get_endp(*s);
d62a17ae 694
695 /* Forward word */
db3c830a 696 stream_putw(*s, 0);
d62a17ae 697
698 /* Set all link information. */
699 cnt = router_lsa_link_set(s, area);
700
701 /* Set # of links here. */
db3c830a 702 stream_putw_at(*s, putp, cnt);
d62a17ae 703}
704
705static int ospf_stub_router_timer(struct thread *t)
706{
707 struct ospf_area *area = THREAD_ARG(t);
708
709 area->t_stub_router = NULL;
710
711 SET_FLAG(area->stub_router_state, OSPF_AREA_WAS_START_STUB_ROUTED);
712
713 /* clear stub route state and generate router-lsa refresh, don't
714 * clobber an administratively set stub-router state though.
715 */
716 if (CHECK_FLAG(area->stub_router_state, OSPF_AREA_ADMIN_STUB_ROUTED))
717 return 0;
718
719 UNSET_FLAG(area->stub_router_state, OSPF_AREA_IS_STUB_ROUTED);
720
721 ospf_router_lsa_update_area(area);
722
723 return 0;
724}
725
726static void ospf_stub_router_check(struct ospf_area *area)
727{
728 /* area must either be administratively configured to be stub
729 * or startup-time stub-router must be configured and we must in a
730 * pre-stub
731 * state.
732 */
733 if (CHECK_FLAG(area->stub_router_state, OSPF_AREA_ADMIN_STUB_ROUTED)) {
734 SET_FLAG(area->stub_router_state, OSPF_AREA_IS_STUB_ROUTED);
735 return;
736 }
737
738 /* not admin-stubbed, check whether startup stubbing is configured and
739 * whether it's not been done yet
740 */
741 if (CHECK_FLAG(area->stub_router_state,
742 OSPF_AREA_WAS_START_STUB_ROUTED))
743 return;
744
745 if (area->ospf->stub_router_startup_time
746 == OSPF_STUB_ROUTER_UNCONFIGURED) {
747 /* stub-router is hence done forever for this area, even if
748 * someone
749 * tries configure it (take effect next restart).
750 */
751 SET_FLAG(area->stub_router_state,
752 OSPF_AREA_WAS_START_STUB_ROUTED);
753 return;
754 }
755
756 /* startup stub-router configured and not yet done */
757 SET_FLAG(area->stub_router_state, OSPF_AREA_IS_STUB_ROUTED);
758
759 OSPF_AREA_TIMER_ON(area->t_stub_router, ospf_stub_router_timer,
760 area->ospf->stub_router_startup_time);
761}
762
718e3744 763/* Create new router-LSA. */
d62a17ae 764static struct ospf_lsa *ospf_router_lsa_new(struct ospf_area *area)
765{
766 struct ospf *ospf = area->ospf;
767 struct stream *s;
768 struct lsa_header *lsah;
769 struct ospf_lsa *new;
770 int length;
771
772 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
773 zlog_debug("LSA[Type1]: Create router-LSA instance");
774
775 /* check whether stub-router is desired, and if this is the first
776 * router LSA.
777 */
778 ospf_stub_router_check(area);
779
780 /* Create a stream for LSA. */
781 s = stream_new(OSPF_MAX_LSA_SIZE);
782 /* Set LSA common header fields. */
783 lsa_header_set(s, LSA_OPTIONS_GET(area) | LSA_OPTIONS_NSSA_GET(area),
784 OSPF_ROUTER_LSA, ospf->router_id, ospf->router_id);
785
786 /* Set router-LSA body fields. */
db3c830a 787 ospf_router_lsa_body_set(&s, area);
d62a17ae 788
789 /* Set length. */
790 length = stream_get_endp(s);
791 lsah = (struct lsa_header *)STREAM_DATA(s);
792 lsah->length = htons(length);
793
794 /* Now, create OSPF LSA instance. */
5b3d4186 795 new = ospf_lsa_new_and_data(length);
d62a17ae 796
797 new->area = area;
798 SET_FLAG(new->flags, OSPF_LSA_SELF | OSPF_LSA_SELF_CHECKED);
b5a8894d 799 new->vrf_id = area->ospf->vrf_id;
d62a17ae 800
801 /* Copy LSA data to store, discard stream. */
d62a17ae 802 memcpy(new->data, lsah, length);
803 stream_free(s);
804
805 return new;
718e3744 806}
807
808/* Originate Router-LSA. */
d62a17ae 809static struct ospf_lsa *ospf_router_lsa_originate(struct ospf_area *area)
718e3744 810{
d62a17ae 811 struct ospf_lsa *new;
718e3744 812
d62a17ae 813 /* Create new router-LSA instance. */
814 if ((new = ospf_router_lsa_new(area)) == NULL) {
815 zlog_err("%s: ospf_router_lsa_new returned NULL", __func__);
816 return NULL;
817 }
818
819 /* Sanity check. */
975a328e 820 if (new->data->adv_router.s_addr == INADDR_ANY) {
d62a17ae 821 if (IS_DEBUG_OSPF_EVENT)
822 zlog_debug("LSA[Type1]: AdvRouter is 0, discard");
823 ospf_lsa_discard(new);
824 return NULL;
825 }
718e3744 826
d62a17ae 827 /* Install LSA to LSDB. */
828 new = ospf_lsa_install(area->ospf, NULL, new);
718e3744 829
d62a17ae 830 /* Update LSA origination count. */
831 area->ospf->lsa_originate_count++;
718e3744 832
d62a17ae 833 /* Flooding new LSA through area. */
834 ospf_flood_through_area(area, NULL, new);
718e3744 835
d62a17ae 836 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
96b663a3
MS
837 zlog_debug("LSA[Type%d:%pI4]: Originate router-LSA %p",
838 new->data->type, &new->data->id,
d62a17ae 839 (void *)new);
840 ospf_lsa_header_dump(new->data);
841 }
718e3744 842
d62a17ae 843 return new;
718e3744 844}
845
846/* Refresh router-LSA. */
d62a17ae 847static struct ospf_lsa *ospf_router_lsa_refresh(struct ospf_lsa *lsa)
848{
849 struct ospf_area *area = lsa->area;
850 struct ospf_lsa *new;
851
852 /* Sanity check. */
853 assert(lsa->data);
718e3744 854
d62a17ae 855 /* Delete LSA from neighbor retransmit-list. */
856 ospf_ls_retransmit_delete_nbr_area(area, lsa);
718e3744 857
d62a17ae 858 /* Unregister LSA from refresh-list */
859 ospf_refresher_unregister_lsa(area->ospf, lsa);
718e3744 860
d62a17ae 861 /* Create new router-LSA instance. */
862 if ((new = ospf_router_lsa_new(area)) == NULL) {
863 zlog_err("%s: ospf_router_lsa_new returned NULL", __func__);
864 return NULL;
865 }
866
867 new->data->ls_seqnum = lsa_seqnum_increment(lsa);
718e3744 868
d62a17ae 869 ospf_lsa_install(area->ospf, NULL, new);
718e3744 870
d62a17ae 871 /* Flood LSA through area. */
872 ospf_flood_through_area(area, NULL, new);
873
874 /* Debug logging. */
875 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
96b663a3
MS
876 zlog_debug("LSA[Type%d:%pI4]: router-LSA refresh",
877 new->data->type, &new->data->id);
d62a17ae 878 ospf_lsa_header_dump(new->data);
879 }
718e3744 880
d62a17ae 881 return NULL;
718e3744 882}
883
d62a17ae 884int ospf_router_lsa_update_area(struct ospf_area *area)
718e3744 885{
d62a17ae 886 if (IS_DEBUG_OSPF_EVENT)
887 zlog_debug("[router-LSA]: (router-LSA area update)");
718e3744 888
d62a17ae 889 /* Now refresh router-LSA. */
890 if (area->router_lsa_self)
891 ospf_lsa_refresh(area->ospf, area->router_lsa_self);
892 /* Newly originate router-LSA. */
893 else
894 ospf_router_lsa_originate(area);
718e3744 895
d62a17ae 896 return 0;
718e3744 897}
898
d62a17ae 899int ospf_router_lsa_update(struct ospf *ospf)
718e3744 900{
d62a17ae 901 struct listnode *node, *nnode;
902 struct ospf_area *area;
718e3744 903
d62a17ae 904 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
905 zlog_debug("Timer[router-LSA Update]: (timer expire)");
718e3744 906
d62a17ae 907 for (ALL_LIST_ELEMENTS(ospf->areas, node, nnode, area)) {
908 struct ospf_lsa *lsa = area->router_lsa_self;
909 struct router_lsa *rl;
910 const char *area_str;
718e3744 911
d62a17ae 912 /* Keep Area ID string. */
913 area_str = AREA_NAME(area);
718e3744 914
d62a17ae 915 /* If LSA not exist in this Area, originate new. */
916 if (lsa == NULL) {
917 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
918 zlog_debug(
919 "LSA[Type1]: Create router-LSA for Area %s",
920 area_str);
718e3744 921
d62a17ae 922 ospf_router_lsa_originate(area);
923 }
924 /* If router-ID is changed, Link ID must change.
925 First flush old LSA, then originate new. */
926 else if (!IPV4_ADDR_SAME(&lsa->data->id, &ospf->router_id)) {
927 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
928 zlog_debug(
96b663a3 929 "LSA[Type%d:%pI4]: Refresh router-LSA for Area %s",
d62a17ae 930 lsa->data->type,
96b663a3 931 &lsa->data->id, area_str);
d62a17ae 932 ospf_refresher_unregister_lsa(ospf, lsa);
933 ospf_lsa_flush_area(lsa, area);
934 ospf_lsa_unlock(&area->router_lsa_self);
935 area->router_lsa_self = NULL;
936
937 /* Refresh router-LSA, (not install) and flood through
938 * area. */
939 ospf_router_lsa_update_area(area);
940 } else {
941 rl = (struct router_lsa *)lsa->data;
942 /* Refresh router-LSA, (not install) and flood through
943 * area. */
944 if (rl->flags != ospf->flags)
945 ospf_router_lsa_update_area(area);
946 }
718e3744 947 }
718e3744 948
d62a17ae 949 return 0;
718e3744 950}
951
6b0655a2 952
718e3744 953/* network-LSA related functions. */
954/* Originate Network-LSA. */
d62a17ae 955static void ospf_network_lsa_body_set(struct stream *s,
956 struct ospf_interface *oi)
718e3744 957{
d62a17ae 958 struct in_addr mask;
959 struct route_node *rn;
960 struct ospf_neighbor *nbr;
718e3744 961
d62a17ae 962 masklen2ip(oi->address->prefixlen, &mask);
963 stream_put_ipv4(s, mask.s_addr);
718e3744 964
d62a17ae 965 /* The network-LSA lists those routers that are fully adjacent to
966 the Designated Router; each fully adjacent router is identified by
967 its OSPF Router ID. The Designated Router includes itself in this
968 list. RFC2328, Section 12.4.2 */
718e3744 969
d62a17ae 970 for (rn = route_top(oi->nbrs); rn; rn = route_next(rn))
971 if ((nbr = rn->info) != NULL)
972 if (nbr->state == NSM_Full || nbr == oi->nbr_self)
973 stream_put_ipv4(s, nbr->router_id.s_addr);
718e3744 974}
975
d62a17ae 976static struct ospf_lsa *ospf_network_lsa_new(struct ospf_interface *oi)
977{
978 struct stream *s;
979 struct ospf_lsa *new;
980 struct lsa_header *lsah;
981 struct ospf_if_params *oip;
982 int length;
983
984 /* If there are no neighbours on this network (the net is stub),
985 the router does not originate network-LSA (see RFC 12.4.2) */
986 if (oi->full_nbrs == 0)
987 return NULL;
988
989 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
990 zlog_debug("LSA[Type2]: Create network-LSA instance");
991
992 /* Create new stream for LSA. */
993 s = stream_new(OSPF_MAX_LSA_SIZE);
994 lsah = (struct lsa_header *)STREAM_DATA(s);
995
996 lsa_header_set(s, (OPTIONS(oi) | LSA_OPTIONS_GET(oi->area)),
997 OSPF_NETWORK_LSA, DR(oi), oi->ospf->router_id);
998
999 /* Set network-LSA body fields. */
1000 ospf_network_lsa_body_set(s, oi);
1001
1002 /* Set length. */
1003 length = stream_get_endp(s);
1004 lsah->length = htons(length);
1005
1006 /* Create OSPF LSA instance. */
5b3d4186 1007 new = ospf_lsa_new_and_data(length);
d62a17ae 1008
1009 new->area = oi->area;
1010 SET_FLAG(new->flags, OSPF_LSA_SELF | OSPF_LSA_SELF_CHECKED);
b5a8894d 1011 new->vrf_id = oi->ospf->vrf_id;
d62a17ae 1012
1013 /* Copy LSA to store. */
d62a17ae 1014 memcpy(new->data, lsah, length);
1015 stream_free(s);
1016
1017 /* Remember prior network LSA sequence numbers, even if we stop
1018 * originating one for this oi, to try avoid re-originating LSAs with a
1019 * prior sequence number, and thus speed up adjency forming &
1020 * convergence.
1021 */
1022 if ((oip = ospf_lookup_if_params(oi->ifp, oi->address->u.prefix4))) {
1023 new->data->ls_seqnum = oip->network_lsa_seqnum;
1024 new->data->ls_seqnum = lsa_seqnum_increment(new);
1025 } else {
1026 oip = ospf_get_if_params(oi->ifp, oi->address->u.prefix4);
1027 ospf_if_update_params(oi->ifp, oi->address->u.prefix4);
1028 }
1029 oip->network_lsa_seqnum = new->data->ls_seqnum;
1030
1031 return new;
718e3744 1032}
1033
1034/* Originate network-LSA. */
d62a17ae 1035void ospf_network_lsa_update(struct ospf_interface *oi)
718e3744 1036{
d62a17ae 1037 struct ospf_lsa *new;
718e3744 1038
d62a17ae 1039 if (oi->network_lsa_self != NULL) {
1040 ospf_lsa_refresh(oi->ospf, oi->network_lsa_self);
1041 return;
1042 }
1043
1044 /* Create new network-LSA instance. */
1045 new = ospf_network_lsa_new(oi);
1046 if (new == NULL)
1047 return;
718e3744 1048
d62a17ae 1049 /* Install LSA to LSDB. */
1050 new = ospf_lsa_install(oi->ospf, oi, new);
718e3744 1051
d62a17ae 1052 /* Update LSA origination count. */
1053 oi->ospf->lsa_originate_count++;
718e3744 1054
d62a17ae 1055 /* Flooding new LSA through area. */
1056 ospf_flood_through_area(oi->area, NULL, new);
1057
1058 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
96b663a3
MS
1059 zlog_debug("LSA[Type%d:%pI4]: Originate network-LSA %p",
1060 new->data->type, &new->data->id,
d62a17ae 1061 (void *)new);
1062 ospf_lsa_header_dump(new->data);
1063 }
718e3744 1064
d62a17ae 1065 return;
718e3744 1066}
1067
d62a17ae 1068static struct ospf_lsa *ospf_network_lsa_refresh(struct ospf_lsa *lsa)
1069{
1070 struct ospf_area *area = lsa->area;
1071 struct ospf_lsa *new, *new2;
1072 struct ospf_if_params *oip;
1073 struct ospf_interface *oi;
1074
1075 assert(lsa->data);
1076
1077 /* Retrieve the oi for the network LSA */
1078 oi = ospf_if_lookup_by_local_addr(area->ospf, NULL, lsa->data->id);
1079 if (oi == NULL) {
1080 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
1081 zlog_debug(
96b663a3
MS
1082 "LSA[Type%d:%pI4]: network-LSA refresh: no oi found, ick, ignoring.",
1083 lsa->data->type, &lsa->data->id);
d62a17ae 1084 ospf_lsa_header_dump(lsa->data);
1085 }
1086 return NULL;
1087 }
1088 /* Delete LSA from neighbor retransmit-list. */
1089 ospf_ls_retransmit_delete_nbr_area(area, lsa);
1090
1091 /* Unregister LSA from refresh-list */
1092 ospf_refresher_unregister_lsa(area->ospf, lsa);
1093
1094 /* Create new network-LSA instance. */
1095 new = ospf_network_lsa_new(oi);
1096 if (new == NULL)
1097 return NULL;
1098
1099 oip = ospf_lookup_if_params(oi->ifp, oi->address->u.prefix4);
1100 assert(oip != NULL);
1101 oip->network_lsa_seqnum = new->data->ls_seqnum =
1102 lsa_seqnum_increment(lsa);
1103
1104 new2 = ospf_lsa_install(area->ospf, oi, new);
1105
1106 assert(new2 == new);
1107
1108 /* Flood LSA through aera. */
1109 ospf_flood_through_area(area, NULL, new);
1110
1111 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
96b663a3
MS
1112 zlog_debug("LSA[Type%d:%pI4]: network-LSA refresh",
1113 new->data->type, &new->data->id);
d62a17ae 1114 ospf_lsa_header_dump(new->data);
1115 }
1116
1117 return new;
1118}
1119
d7c0a89a 1120static void stream_put_ospf_metric(struct stream *s, uint32_t metric_value)
d62a17ae 1121{
d7c0a89a 1122 uint32_t metric;
d62a17ae 1123 char *mp;
1124
1125 /* Put 0 metric. TOS metric is not supported. */
1126 metric = htonl(metric_value);
1127 mp = (char *)&metric;
1128 mp++;
1129 stream_put(s, mp, 3);
718e3744 1130}
1131
1132/* summary-LSA related functions. */
d62a17ae 1133static void ospf_summary_lsa_body_set(struct stream *s, struct prefix *p,
d7c0a89a 1134 uint32_t metric)
718e3744 1135{
d62a17ae 1136 struct in_addr mask;
718e3744 1137
d62a17ae 1138 masklen2ip(p->prefixlen, &mask);
718e3744 1139
d62a17ae 1140 /* Put Network Mask. */
1141 stream_put_ipv4(s, mask.s_addr);
718e3744 1142
d62a17ae 1143 /* Set # TOS. */
d7c0a89a 1144 stream_putc(s, (uint8_t)0);
718e3744 1145
d62a17ae 1146 /* Set metric. */
1147 stream_put_ospf_metric(s, metric);
718e3744 1148}
1149
d62a17ae 1150static struct ospf_lsa *ospf_summary_lsa_new(struct ospf_area *area,
d7c0a89a 1151 struct prefix *p, uint32_t metric,
d62a17ae 1152 struct in_addr id)
718e3744 1153{
d62a17ae 1154 struct stream *s;
1155 struct ospf_lsa *new;
1156 struct lsa_header *lsah;
1157 int length;
718e3744 1158
d62a17ae 1159 if (id.s_addr == 0xffffffff) {
1160 /* Maybe Link State ID not available. */
1161 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
1162 zlog_debug(
1163 "LSA[Type%d]: Link ID not available, can't originate",
1164 OSPF_SUMMARY_LSA);
1165 return NULL;
1166 }
c24d602e 1167
d62a17ae 1168 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
1169 zlog_debug("LSA[Type3]: Create summary-LSA instance");
718e3744 1170
d62a17ae 1171 /* Create new stream for LSA. */
1172 s = stream_new(OSPF_MAX_LSA_SIZE);
1173 lsah = (struct lsa_header *)STREAM_DATA(s);
718e3744 1174
d62a17ae 1175 lsa_header_set(s, LSA_OPTIONS_GET(area), OSPF_SUMMARY_LSA, id,
1176 area->ospf->router_id);
718e3744 1177
d62a17ae 1178 /* Set summary-LSA body fields. */
1179 ospf_summary_lsa_body_set(s, p, metric);
718e3744 1180
d62a17ae 1181 /* Set length. */
1182 length = stream_get_endp(s);
1183 lsah->length = htons(length);
718e3744 1184
d62a17ae 1185 /* Create OSPF LSA instance. */
5b3d4186 1186 new = ospf_lsa_new_and_data(length);
d62a17ae 1187 new->area = area;
1188 SET_FLAG(new->flags, OSPF_LSA_SELF | OSPF_LSA_SELF_CHECKED);
b5a8894d 1189 new->vrf_id = area->ospf->vrf_id;
718e3744 1190
d62a17ae 1191 /* Copy LSA to store. */
d62a17ae 1192 memcpy(new->data, lsah, length);
1193 stream_free(s);
718e3744 1194
d62a17ae 1195 return new;
718e3744 1196}
1197
1198/* Originate Summary-LSA. */
d62a17ae 1199struct ospf_lsa *ospf_summary_lsa_originate(struct prefix_ipv4 *p,
d7c0a89a 1200 uint32_t metric,
d62a17ae 1201 struct ospf_area *area)
1202{
1203 struct ospf_lsa *new;
1204 struct in_addr id;
1205
1206 id = ospf_lsa_unique_id(area->ospf, area->lsdb, OSPF_SUMMARY_LSA, p);
1207
1208 if (id.s_addr == 0xffffffff) {
1209 /* Maybe Link State ID not available. */
1210 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
1211 zlog_debug(
1212 "LSA[Type%d]: Link ID not available, can't originate",
1213 OSPF_SUMMARY_LSA);
1214 return NULL;
1215 }
1216
1217 /* Create new summary-LSA instance. */
1218 if (!(new = ospf_summary_lsa_new(area, (struct prefix *)p, metric, id)))
1219 return NULL;
1220
1221 /* Instlal LSA to LSDB. */
1222 new = ospf_lsa_install(area->ospf, NULL, new);
1223
1224 /* Update LSA origination count. */
1225 area->ospf->lsa_originate_count++;
1226
1227 /* Flooding new LSA through area. */
1228 ospf_flood_through_area(area, NULL, new);
1229
1230 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
96b663a3
MS
1231 zlog_debug("LSA[Type%d:%pI4]: Originate summary-LSA %p",
1232 new->data->type, &new->data->id,
d62a17ae 1233 (void *)new);
1234 ospf_lsa_header_dump(new->data);
1235 }
1236
1237 return new;
1238}
1239
1240static struct ospf_lsa *ospf_summary_lsa_refresh(struct ospf *ospf,
1241 struct ospf_lsa *lsa)
1242{
1243 struct ospf_lsa *new;
1244 struct summary_lsa *sl;
1245 struct prefix p;
1246
1247 /* Sanity check. */
1248 assert(lsa->data);
1249
1250 sl = (struct summary_lsa *)lsa->data;
1251 p.prefixlen = ip_masklen(sl->mask);
1252 new = ospf_summary_lsa_new(lsa->area, &p, GET_METRIC(sl->metric),
1253 sl->header.id);
1254
1255 if (!new)
1256 return NULL;
1257
1258 new->data->ls_seqnum = lsa_seqnum_increment(lsa);
1259
1260 ospf_lsa_install(ospf, NULL, new);
1261
1262 /* Flood LSA through AS. */
1263 ospf_flood_through_area(new->area, NULL, new);
1264
1265 /* Debug logging. */
1266 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
96b663a3
MS
1267 zlog_debug("LSA[Type%d:%pI4]: summary-LSA refresh",
1268 new->data->type, &new->data->id);
d62a17ae 1269 ospf_lsa_header_dump(new->data);
1270 }
1271
1272 return new;
718e3744 1273}
1274
6b0655a2 1275
718e3744 1276/* summary-ASBR-LSA related functions. */
d62a17ae 1277static void ospf_summary_asbr_lsa_body_set(struct stream *s, struct prefix *p,
d7c0a89a 1278 uint32_t metric)
718e3744 1279{
d62a17ae 1280 /* Put Network Mask. */
d7c0a89a 1281 stream_put_ipv4(s, (uint32_t)0);
718e3744 1282
d62a17ae 1283 /* Set # TOS. */
d7c0a89a 1284 stream_putc(s, (uint8_t)0);
718e3744 1285
d62a17ae 1286 /* Set metric. */
1287 stream_put_ospf_metric(s, metric);
718e3744 1288}
1289
d62a17ae 1290static struct ospf_lsa *ospf_summary_asbr_lsa_new(struct ospf_area *area,
1291 struct prefix *p,
d7c0a89a 1292 uint32_t metric,
d62a17ae 1293 struct in_addr id)
718e3744 1294{
d62a17ae 1295 struct stream *s;
1296 struct ospf_lsa *new;
1297 struct lsa_header *lsah;
1298 int length;
718e3744 1299
d62a17ae 1300 if (id.s_addr == 0xffffffff) {
1301 /* Maybe Link State ID not available. */
1302 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
1303 zlog_debug(
1304 "LSA[Type%d]: Link ID not available, can't originate",
1305 OSPF_ASBR_SUMMARY_LSA);
1306 return NULL;
1307 }
c24d602e 1308
d62a17ae 1309 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
1310 zlog_debug("LSA[Type3]: Create summary-LSA instance");
718e3744 1311
d62a17ae 1312 /* Create new stream for LSA. */
1313 s = stream_new(OSPF_MAX_LSA_SIZE);
1314 lsah = (struct lsa_header *)STREAM_DATA(s);
718e3744 1315
d62a17ae 1316 lsa_header_set(s, LSA_OPTIONS_GET(area), OSPF_ASBR_SUMMARY_LSA, id,
1317 area->ospf->router_id);
718e3744 1318
d62a17ae 1319 /* Set summary-LSA body fields. */
1320 ospf_summary_asbr_lsa_body_set(s, p, metric);
718e3744 1321
d62a17ae 1322 /* Set length. */
1323 length = stream_get_endp(s);
1324 lsah->length = htons(length);
718e3744 1325
d62a17ae 1326 /* Create OSPF LSA instance. */
5b3d4186 1327 new = ospf_lsa_new_and_data(length);
d62a17ae 1328 new->area = area;
1329 SET_FLAG(new->flags, OSPF_LSA_SELF | OSPF_LSA_SELF_CHECKED);
b5a8894d 1330 new->vrf_id = area->ospf->vrf_id;
718e3744 1331
d62a17ae 1332 /* Copy LSA to store. */
d62a17ae 1333 memcpy(new->data, lsah, length);
1334 stream_free(s);
718e3744 1335
d62a17ae 1336 return new;
718e3744 1337}
1338
1339/* Originate summary-ASBR-LSA. */
d62a17ae 1340struct ospf_lsa *ospf_summary_asbr_lsa_originate(struct prefix_ipv4 *p,
d7c0a89a 1341 uint32_t metric,
d62a17ae 1342 struct ospf_area *area)
1343{
1344 struct ospf_lsa *new;
1345 struct in_addr id;
1346
1347 id = ospf_lsa_unique_id(area->ospf, area->lsdb, OSPF_ASBR_SUMMARY_LSA,
1348 p);
1349
1350 if (id.s_addr == 0xffffffff) {
1351 /* Maybe Link State ID not available. */
1352 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
1353 zlog_debug(
1354 "LSA[Type%d]: Link ID not available, can't originate",
1355 OSPF_ASBR_SUMMARY_LSA);
1356 return NULL;
1357 }
1358
1359 /* Create new summary-LSA instance. */
1360 new = ospf_summary_asbr_lsa_new(area, (struct prefix *)p, metric, id);
1361 if (!new)
1362 return NULL;
1363
1364 /* Install LSA to LSDB. */
1365 new = ospf_lsa_install(area->ospf, NULL, new);
718e3744 1366
d62a17ae 1367 /* Update LSA origination count. */
1368 area->ospf->lsa_originate_count++;
718e3744 1369
d62a17ae 1370 /* Flooding new LSA through area. */
1371 ospf_flood_through_area(area, NULL, new);
718e3744 1372
d62a17ae 1373 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
96b663a3
MS
1374 zlog_debug("LSA[Type%d:%pI4]: Originate summary-ASBR-LSA %p",
1375 new->data->type, &new->data->id,
d62a17ae 1376 (void *)new);
1377 ospf_lsa_header_dump(new->data);
1378 }
1379
1380 return new;
1381}
1382
1383static struct ospf_lsa *ospf_summary_asbr_lsa_refresh(struct ospf *ospf,
1384 struct ospf_lsa *lsa)
1385{
1386 struct ospf_lsa *new;
1387 struct summary_lsa *sl;
1388 struct prefix p;
1389
1390 /* Sanity check. */
1391 assert(lsa->data);
1392
1393 sl = (struct summary_lsa *)lsa->data;
1394 p.prefixlen = ip_masklen(sl->mask);
1395 new = ospf_summary_asbr_lsa_new(lsa->area, &p, GET_METRIC(sl->metric),
1396 sl->header.id);
1397 if (!new)
1398 return NULL;
1399
1400 new->data->ls_seqnum = lsa_seqnum_increment(lsa);
1401
1402 ospf_lsa_install(ospf, NULL, new);
1403
1404 /* Flood LSA through area. */
1405 ospf_flood_through_area(new->area, NULL, new);
1406
1407 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
96b663a3
MS
1408 zlog_debug("LSA[Type%d:%pI4]: summary-ASBR-LSA refresh",
1409 new->data->type, &new->data->id);
d62a17ae 1410 ospf_lsa_header_dump(new->data);
1411 }
1412
1413 return new;
718e3744 1414}
1415
1416/* AS-external-LSA related functions. */
1417
1418/* Get nexthop for AS-external-LSAs. Return nexthop if its interface
1419 is connected, else 0*/
d62a17ae 1420static struct in_addr ospf_external_lsa_nexthop_get(struct ospf *ospf,
1421 struct in_addr nexthop)
718e3744 1422{
d62a17ae 1423 struct in_addr fwd;
1424 struct prefix nh;
1425 struct listnode *node;
1426 struct ospf_interface *oi;
1427
1428 fwd.s_addr = 0;
718e3744 1429
d62a17ae 1430 if (!nexthop.s_addr)
1431 return fwd;
718e3744 1432
d62a17ae 1433 /* Check whether nexthop is covered by OSPF network. */
1434 nh.family = AF_INET;
1435 nh.u.prefix4 = nexthop;
1436 nh.prefixlen = IPV4_MAX_BITLEN;
718e3744 1437
d62a17ae 1438 /* XXX/SCALE: If there were a lot of oi's on an ifp, then it'd be
1439 * better to make use of the per-ifp table of ois.
1440 */
1441 for (ALL_LIST_ELEMENTS_RO(ospf->oiflist, node, oi))
1442 if (if_is_operative(oi->ifp))
1443 if (oi->address->family == AF_INET)
1444 if (prefix_match(oi->address, &nh))
1445 return nexthop;
718e3744 1446
d62a17ae 1447 return fwd;
718e3744 1448}
1449
718e3744 1450/* NSSA-external-LSA related functions. */
1451
1452/* Get 1st IP connection for Forward Addr */
4dadc291 1453
d62a17ae 1454struct in_addr ospf_get_ip_from_ifp(struct ospf_interface *oi)
718e3744 1455{
d62a17ae 1456 struct in_addr fwd;
718e3744 1457
975a328e 1458 fwd.s_addr = INADDR_ANY;
718e3744 1459
d62a17ae 1460 if (if_is_operative(oi->ifp))
1461 return oi->address->u.prefix4;
1462
1463 return fwd;
718e3744 1464}
1465
1466/* Get 1st IP connection for Forward Addr */
d62a17ae 1467struct in_addr ospf_get_nssa_ip(struct ospf_area *area)
1468{
1469 struct in_addr fwd;
1470 struct in_addr best_default;
1471 struct listnode *node;
1472 struct ospf_interface *oi;
1473
1474 fwd.s_addr = 0;
1475 best_default.s_addr = 0;
1476
1477 for (ALL_LIST_ELEMENTS_RO(area->ospf->oiflist, node, oi)) {
1478 if (if_is_operative(oi->ifp))
1479 if (oi->area->external_routing == OSPF_AREA_NSSA)
1480 if (oi->address
1481 && oi->address->family == AF_INET) {
3a6290bd 1482 if (best_default.s_addr == INADDR_ANY)
d62a17ae 1483 best_default =
1484 oi->address->u.prefix4;
1485 if (oi->area == area)
1486 return oi->address->u.prefix4;
1487 }
1488 }
3a6290bd 1489 if (best_default.s_addr != INADDR_ANY)
d62a17ae 1490 return best_default;
718e3744 1491
3a6290bd 1492 if (best_default.s_addr != INADDR_ANY)
d62a17ae 1493 return best_default;
68980084 1494
d62a17ae 1495 return fwd;
718e3744 1496}
beebba75 1497
d7c0a89a 1498int metric_type(struct ospf *ospf, uint8_t src, unsigned short instance)
718e3744 1499{
d62a17ae 1500 struct ospf_redist *red;
7c8ff89e 1501
d62a17ae 1502 red = ospf_redist_lookup(ospf, src, instance);
7c8ff89e 1503
d62a17ae 1504 return ((!red || red->dmetric.type < 0) ? DEFAULT_METRIC_TYPE
1505 : red->dmetric.type);
718e3744 1506}
1507
d7c0a89a 1508int metric_value(struct ospf *ospf, uint8_t src, unsigned short instance)
718e3744 1509{
d62a17ae 1510 struct ospf_redist *red;
7c8ff89e 1511
d62a17ae 1512 red = ospf_redist_lookup(ospf, src, instance);
1513 if (!red || red->dmetric.value < 0) {
1514 if (src == DEFAULT_ROUTE) {
1515 if (ospf->default_originate == DEFAULT_ORIGINATE_ZEBRA)
1516 return DEFAULT_DEFAULT_ORIGINATE_METRIC;
1517 else
1518 return DEFAULT_DEFAULT_ALWAYS_METRIC;
1519 } else if (ospf->default_metric < 0)
1520 return DEFAULT_DEFAULT_METRIC;
1521 else
1522 return ospf->default_metric;
718e3744 1523 }
718e3744 1524
d62a17ae 1525 return red->dmetric.value;
718e3744 1526}
1527
1528/* Set AS-external-LSA body. */
d62a17ae 1529static void ospf_external_lsa_body_set(struct stream *s,
1530 struct external_info *ei,
1531 struct ospf *ospf)
1532{
1533 struct prefix_ipv4 *p = &ei->p;
1534 struct in_addr mask, fwd_addr;
d7c0a89a 1535 uint32_t mvalue;
d62a17ae 1536 int mtype;
1537 int type;
d7c0a89a 1538 unsigned short instance;
d62a17ae 1539
1540 /* Put Network Mask. */
1541 masklen2ip(p->prefixlen, &mask);
1542 stream_put_ipv4(s, mask.s_addr);
1543
1544 /* If prefix is default, specify DEFAULT_ROUTE. */
1545 type = is_prefix_default(&ei->p) ? DEFAULT_ROUTE : ei->type;
1546 instance = is_prefix_default(&ei->p) ? 0 : ei->instance;
1547
1548 mtype = (ROUTEMAP_METRIC_TYPE(ei) != -1)
1549 ? ROUTEMAP_METRIC_TYPE(ei)
1550 : metric_type(ospf, type, instance);
1551
1552 mvalue = (ROUTEMAP_METRIC(ei) != -1)
1553 ? ROUTEMAP_METRIC(ei)
1554 : metric_value(ospf, type, instance);
1555
1556 /* Put type of external metric. */
1557 stream_putc(s, (mtype == EXTERNAL_METRIC_TYPE_2 ? 0x80 : 0));
1558
1559 /* Put 0 metric. TOS metric is not supported. */
1560 stream_put_ospf_metric(s, mvalue);
1561
1562 /* Get forwarding address to nexthop if on the Connection List, else 0.
1563 */
1564 fwd_addr = ospf_external_lsa_nexthop_get(ospf, ei->nexthop);
1565
1566 /* Put forwarding address. */
1567 stream_put_ipv4(s, fwd_addr.s_addr);
1568
1569 /* Put route tag */
1570 stream_putl(s, ei->tag);
718e3744 1571}
1572
1573/* Create new external-LSA. */
d62a17ae 1574static struct ospf_lsa *ospf_external_lsa_new(struct ospf *ospf,
1575 struct external_info *ei,
1576 struct in_addr *old_id)
1577{
1578 struct stream *s;
1579 struct lsa_header *lsah;
1580 struct ospf_lsa *new;
1581 struct in_addr id;
1582 int length;
1583
1584 if (ei == NULL) {
1585 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
1586 zlog_debug(
1587 "LSA[Type5]: External info is NULL, can't originate");
1588 return NULL;
1589 }
1590
1591 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
1592 zlog_debug("LSA[Type5]: Originate AS-external-LSA instance");
1593
1594 /* If old Link State ID is specified, refresh LSA with same ID. */
1595 if (old_id)
1596 id = *old_id;
1597 /* Get Link State with unique ID. */
1598 else {
1599 id = ospf_lsa_unique_id(ospf, ospf->lsdb, OSPF_AS_EXTERNAL_LSA,
1600 &ei->p);
1601 if (id.s_addr == 0xffffffff) {
1602 /* Maybe Link State ID not available. */
1603 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
1604 zlog_debug(
1605 "LSA[Type5]: Link ID not available, can't originate");
1606 return NULL;
1607 }
1608 }
1609
1610 /* Create new stream for LSA. */
1611 s = stream_new(OSPF_MAX_LSA_SIZE);
1612 lsah = (struct lsa_header *)STREAM_DATA(s);
1613
1614 /* Set LSA common header fields. */
1615 lsa_header_set(s, OSPF_OPTION_E, OSPF_AS_EXTERNAL_LSA, id,
1616 ospf->router_id);
1617
1618 /* Set AS-external-LSA body fields. */
1619 ospf_external_lsa_body_set(s, ei, ospf);
1620
1621 /* Set length. */
1622 length = stream_get_endp(s);
1623 lsah->length = htons(length);
1624
1625 /* Now, create OSPF LSA instance. */
5b3d4186 1626 new = ospf_lsa_new_and_data(length);
d62a17ae 1627 new->area = NULL;
1628 SET_FLAG(new->flags,
1629 OSPF_LSA_SELF | OSPF_LSA_APPROVED | OSPF_LSA_SELF_CHECKED);
b5a8894d 1630 new->vrf_id = ospf->vrf_id;
d62a17ae 1631
1632 /* Copy LSA data to store, discard stream. */
d62a17ae 1633 memcpy(new->data, lsah, length);
1634 stream_free(s);
1635
1636 return new;
718e3744 1637}
1638
718e3744 1639/* As Type-7 */
d62a17ae 1640static void ospf_install_flood_nssa(struct ospf *ospf, struct ospf_lsa *lsa,
1641 struct external_info *ei)
1642{
1643 struct ospf_lsa *new;
1644 struct as_external_lsa *extlsa;
1645 struct ospf_area *area;
1646 struct listnode *node, *nnode;
1647
1648 /* LSA may be a Type-5 originated via translation of a Type-7 LSA
1649 * which originated from an NSSA area. In which case it should not be
1650 * flooded back to NSSA areas.
1651 */
1652 if (CHECK_FLAG(lsa->flags, OSPF_LSA_LOCAL_XLT))
1653 return;
1654
1655 /* NSSA Originate or Refresh (If anyNSSA)
1656
1657 LSA is self-originated. And just installed as Type-5.
1658 Additionally, install as Type-7 LSDB for every attached NSSA.
1659
1660 P-Bit controls which ABR performs translation to outside world; If
1661 we are an ABR....do not set the P-bit, because we send the Type-5,
1662 not as the ABR Translator, but as the ASBR owner within the AS!
1663
1664 If we are NOT ABR, Flood through NSSA as Type-7 w/P-bit set. The
1665 elected ABR Translator will see the P-bit, Translate, and re-flood.
1666
1667 Later, ABR_TASK and P-bit will scan Type-7 LSDB and translate to
1668 Type-5's to non-NSSA Areas. (it will also attempt a re-install) */
1669
1670 for (ALL_LIST_ELEMENTS(ospf->areas, node, nnode, area)) {
1671 /* Don't install Type-7 LSA's into nonNSSA area */
1672 if (area->external_routing != OSPF_AREA_NSSA)
1673 continue;
1674
1675 /* make lsa duplicate, lock=1 */
1676 new = ospf_lsa_dup(lsa);
1677 new->area = area;
1678 new->data->type = OSPF_AS_NSSA_LSA;
1679
1680 /* set P-bit if not ABR */
1681 if (!IS_OSPF_ABR(ospf)) {
1682 SET_FLAG(new->data->options, OSPF_OPTION_NP);
1683
1684 /* set non-zero FWD ADDR
1685
1686 draft-ietf-ospf-nssa-update-09.txt
1687
1688 if the network between the NSSA AS boundary router and
1689 the
1690 adjacent AS is advertised into OSPF as an internal OSPF
1691 route,
1692 the forwarding address should be the next op address as
1693 is cu
1694 currently done with type-5 LSAs. If the intervening
1695 network is
1696 not adversited into OSPF as an internal OSPF route and
1697 the
1698 type-7 LSA's P-bit is set a forwarding address should be
87bd50e8 1699 selected from one of the router's active OSPF interface
d62a17ae 1700 addresses
1701 which belong to the NSSA. If no such addresses exist,
1702 then
1703 no type-7 LSA's with the P-bit set should originate from
1704 this
1705 router. */
1706
1707 /* kevinm: not updating lsa anymore, just new */
1708 extlsa = (struct as_external_lsa *)(new->data);
1709
3a6290bd 1710 if (extlsa->e[0].fwd_addr.s_addr == INADDR_ANY)
d62a17ae 1711 extlsa->e[0].fwd_addr = ospf_get_nssa_ip(
1712 area); /* this NSSA area in ifp */
1713
3a6290bd 1714 if (extlsa->e[0].fwd_addr.s_addr == INADDR_ANY) {
d62a17ae 1715 if (IS_DEBUG_OSPF_NSSA)
1716 zlog_debug(
1717 "LSA[Type-7]: Could not build FWD-ADDR");
1718 ospf_lsa_discard(new);
1719 return;
1720 }
1721 }
1722
1723 /* install also as Type-7 */
1724 ospf_lsa_install(ospf, NULL,
1725 new); /* Remove Old, Lock New = 2 */
1726
1727 /* will send each copy, lock=2+n */
1728 ospf_flood_through_as(
1729 ospf, NULL, new); /* all attached NSSA's, no AS/STUBs */
1730 }
d4a53d58 1731}
1732
d62a17ae 1733static struct ospf_lsa *ospf_lsa_translated_nssa_new(struct ospf *ospf,
1734 struct ospf_lsa *type7)
1735{
1736
1737 struct ospf_lsa *new;
1738 struct as_external_lsa *ext, *extnew;
1739 struct external_info ei;
1740
1741 ext = (struct as_external_lsa *)(type7->data);
1742
1743 /* need external_info struct, fill in bare minimum */
1744 ei.p.family = AF_INET;
1745 ei.p.prefix = type7->data->id;
1746 ei.p.prefixlen = ip_masklen(ext->mask);
1747 ei.type = ZEBRA_ROUTE_OSPF;
1748 ei.nexthop = ext->header.adv_router;
1749 ei.route_map_set.metric = -1;
1750 ei.route_map_set.metric_type = -1;
1751 ei.tag = 0;
12a81f8e 1752 ei.instance = 0;
d62a17ae 1753
1754 if ((new = ospf_external_lsa_new(ospf, &ei, &type7->data->id))
1755 == NULL) {
1756 if (IS_DEBUG_OSPF_NSSA)
1757 zlog_debug(
dc135f9e 1758 "%s: Could not originate Translated Type-5 for %pI4",
1759 __func__, &ei.p.prefix);
d62a17ae 1760 return NULL;
1761 }
1762
1763 extnew = (struct as_external_lsa *)(new->data);
1764
1765 /* copy over Type-7 data to new */
1766 extnew->e[0].tos = ext->e[0].tos;
1767 extnew->e[0].route_tag = ext->e[0].route_tag;
c317eddb 1768 if (type7->area->suppress_fa) {
1769 extnew->e[0].fwd_addr.s_addr = 0;
1770 if (IS_DEBUG_OSPF_NSSA)
1771 zlog_debug(
1772 "ospf_lsa_translated_nssa_new(): Suppress forwarding address for %pI4",
1773 &ei.p.prefix);
1774 } else
1775 extnew->e[0].fwd_addr.s_addr = ext->e[0].fwd_addr.s_addr;
d62a17ae 1776 new->data->ls_seqnum = type7->data->ls_seqnum;
1777
1778 /* add translated flag, checksum and lock new lsa */
1779 SET_FLAG(new->flags, OSPF_LSA_LOCAL_XLT); /* Translated from 7 */
1780 new = ospf_lsa_lock(new);
1781
1782 return new;
d4a53d58 1783}
1784
d4a53d58 1785/* Originate Translated Type-5 for supplied Type-7 NSSA LSA */
d62a17ae 1786struct ospf_lsa *ospf_translated_nssa_originate(struct ospf *ospf,
c317eddb 1787 struct ospf_lsa *type7,
1788 struct ospf_lsa *type5)
d62a17ae 1789{
1790 struct ospf_lsa *new;
1791 struct as_external_lsa *extnew;
1792
1793 /* we cant use ospf_external_lsa_originate() as we need to set
1794 * the OSPF_LSA_LOCAL_XLT flag, must originate by hand
1795 */
1796
1797 if ((new = ospf_lsa_translated_nssa_new(ospf, type7)) == NULL) {
1798 if (IS_DEBUG_OSPF_NSSA)
1799 zlog_debug(
dc135f9e 1800 "%s: Could not translate Type-7, Id %pI4, to Type-5",
1801 __func__, &type7->data->id);
d62a17ae 1802 return NULL;
1803 }
1804
919714bd 1805 extnew = (struct as_external_lsa *)new->data;
1806
c317eddb 1807 /* Update LSA sequence number from translated Type-5 LSA */
1808 if (type5)
1809 new->data->ls_seqnum = lsa_seqnum_increment(type5);
1810
919714bd 1811 if ((new = ospf_lsa_install(ospf, NULL, new)) == NULL) {
dc135f9e 1812 flog_warn(EC_OSPF_LSA_INSTALL_FAILURE,
1813 "%s: Could not install LSA id %pI4", __func__,
1814 &type7->data->id);
919714bd 1815 return NULL;
1816 }
d62a17ae 1817
1818 if (IS_DEBUG_OSPF_NSSA) {
dc135f9e 1819 zlog_debug("%s: translated Type 7, installed", __func__);
d62a17ae 1820 ospf_lsa_header_dump(new->data);
1821 zlog_debug(" Network mask: %d", ip_masklen(extnew->mask));
96b663a3
MS
1822 zlog_debug(" Forward addr: %pI4",
1823 &extnew->e[0].fwd_addr);
d62a17ae 1824 }
1825
d62a17ae 1826 ospf->lsa_originate_count++;
1827 ospf_flood_through_as(ospf, NULL, new);
1828
1829 return new;
d4a53d58 1830}
1831
1832/* Refresh Translated from NSSA AS-external-LSA. */
d62a17ae 1833struct ospf_lsa *ospf_translated_nssa_refresh(struct ospf *ospf,
1834 struct ospf_lsa *type7,
1835 struct ospf_lsa *type5)
1836{
1837 struct ospf_lsa *new = NULL;
c317eddb 1838 struct as_external_lsa *extold = NULL;
1839 uint32_t ls_seqnum = 0;
d62a17ae 1840
1841 /* Sanity checks. */
1842 assert(type7 || type5);
1843 if (!(type7 || type5))
1844 return NULL;
1845 if (type7)
1846 assert(type7->data);
1847 if (type5)
1848 assert(type5->data);
1849 assert(ospf->anyNSSA);
1850
1851 /* get required data according to what has been given */
1852 if (type7 && type5 == NULL) {
1853 /* find the translated Type-5 for this Type-7 */
1854 struct as_external_lsa *ext =
1855 (struct as_external_lsa *)(type7->data);
1856 struct prefix_ipv4 p = {
1857 .prefix = type7->data->id,
1858 .prefixlen = ip_masklen(ext->mask),
1859 .family = AF_INET,
1860 };
1861
1862 type5 = ospf_external_info_find_lsa(ospf, &p);
1863 } else if (type5 && type7 == NULL) {
1864 /* find the type-7 from which supplied type-5 was translated,
1865 * ie find first type-7 with same LSA Id.
1866 */
1867 struct listnode *ln, *lnn;
1868 struct route_node *rn;
1869 struct ospf_lsa *lsa;
1870 struct ospf_area *area;
1871
1872 for (ALL_LIST_ELEMENTS(ospf->areas, ln, lnn, area)) {
1873 if (area->external_routing != OSPF_AREA_NSSA && !type7)
1874 continue;
1875
044506e7 1876 LSDB_LOOP (NSSA_LSDB(area), rn, lsa) {
d62a17ae 1877 if (lsa->data->id.s_addr
1878 == type5->data->id.s_addr) {
1879 type7 = lsa;
1880 break;
1881 }
1882 }
1883 }
1884 }
1885
1886 /* do we have type7? */
1887 if (!type7) {
1888 if (IS_DEBUG_OSPF_NSSA)
1889 zlog_debug(
96b663a3
MS
1890 "ospf_translated_nssa_refresh(): no Type-7 found for Type-5 LSA Id %pI4",
1891 &type5->data->id);
d62a17ae 1892 return NULL;
1893 }
1894
1895 /* do we have valid translated type5? */
1896 if (type5 == NULL || !CHECK_FLAG(type5->flags, OSPF_LSA_LOCAL_XLT)) {
1897 if (IS_DEBUG_OSPF_NSSA)
1898 zlog_debug(
96b663a3
MS
1899 "ospf_translated_nssa_refresh(): No translated Type-5 found for Type-7 with Id %pI4",
1900 &type7->data->id);
d62a17ae 1901 return NULL;
1902 }
1903
c317eddb 1904 extold = (struct as_external_lsa *)type5->data;
1905 if (type7->area->suppress_fa == 1) {
1906 if (extold->e[0].fwd_addr.s_addr == 0)
1907 ls_seqnum = ntohl(type5->data->ls_seqnum);
1908 }
1909
d62a17ae 1910 /* Delete LSA from neighbor retransmit-list. */
1911 ospf_ls_retransmit_delete_nbr_as(ospf, type5);
1912
1913 /* create new translated LSA */
1914 if ((new = ospf_lsa_translated_nssa_new(ospf, type7)) == NULL) {
1915 if (IS_DEBUG_OSPF_NSSA)
1916 zlog_debug(
96b663a3
MS
1917 "ospf_translated_nssa_refresh(): Could not translate Type-7 for %pI4 to Type-5",
1918 &type7->data->id);
d62a17ae 1919 return NULL;
1920 }
1921
c317eddb 1922 if (type7->area->suppress_fa == 1) {
1923 if (extold->e[0].fwd_addr.s_addr == 0)
1924 new->data->ls_seqnum = htonl(ls_seqnum + 1);
1925 }
1926
d62a17ae 1927 if (!(new = ospf_lsa_install(ospf, NULL, new))) {
542a208f 1928 flog_warn(
cf444bcf 1929 EC_OSPF_LSA_INSTALL_FAILURE,
96b663a3
MS
1930 "ospf_translated_nssa_refresh(): Could not install translated LSA, Id %pI4",
1931 &type7->data->id);
d62a17ae 1932 return NULL;
1933 }
1934
1935 /* Flood LSA through area. */
1936 ospf_flood_through_as(ospf, NULL, new);
1937
1938 return new;
1939}
1940
1941int is_prefix_default(struct prefix_ipv4 *p)
1942{
1943 struct prefix_ipv4 q;
1944
1945 q.family = AF_INET;
975a328e 1946 q.prefix.s_addr = INADDR_ANY;
d62a17ae 1947 q.prefixlen = 0;
1948
1949 return prefix_same((struct prefix *)p, (struct prefix *)&q);
718e3744 1950}
1951
1952/* Originate an AS-external-LSA, install and flood. */
d62a17ae 1953struct ospf_lsa *ospf_external_lsa_originate(struct ospf *ospf,
1954 struct external_info *ei)
1955{
1956 struct ospf_lsa *new;
1957
1958 /* Added for NSSA project....
1959
1960 External LSAs are originated in ASBRs as usual, but for NSSA
1961 systems.
1962 there is the global Type-5 LSDB and a Type-7 LSDB installed for
1963 every area. The Type-7's are flooded to every IR and every ABR; We
1964 install the Type-5 LSDB so that the normal "refresh" code operates
1965 as usual, and flag them as not used during ASE calculations. The
1966 Type-7 LSDB is used for calculations. Each Type-7 has a Forwarding
1967 Address of non-zero.
1968
1969 If an ABR is the elected NSSA translator, following SPF and during
1970 the ABR task it will translate all the scanned Type-7's, with P-bit
1971 ON and not-self generated, and translate to Type-5's throughout the
1972 non-NSSA/STUB AS.
1973
1974 A difference in operation depends whether this ASBR is an ABR
1975 or not. If not an ABR, the P-bit is ON, to indicate that any
1976 elected NSSA-ABR can perform its translation.
1977
1978 If an ABR, the P-bit is OFF; No ABR will perform translation and
1979 this ASBR will flood the Type-5 LSA as usual.
1980
1981 For the case where this ASBR is not an ABR, the ASE calculations
1982 are based on the Type-5 LSDB; The Type-7 LSDB exists just to
1983 demonstrate to the user that there are LSA's that belong to any
1984 attached NSSA.
1985
1986 Finally, it just so happens that when the ABR is translating every
1987 Type-7 into Type-5, it installs it into the Type-5 LSDB as an
1988 approved Type-5 (translated from Type-7); at the end of translation
1989 if any Translated Type-5's remain unapproved, then they must be
1990 flushed from the AS.
1991
1992 */
1993
975a328e 1994 if (ospf->router_id.s_addr == INADDR_ANY) {
f1cf5af6 1995 if (IS_DEBUG_OSPF_EVENT)
975a328e
DA
1996 zlog_debug(
1997 "LSA[Type5:%pI4]: deferring AS-external-LSA origination, router ID is zero",
1998 &ei->p.prefix);
f1cf5af6
DL
1999 return NULL;
2000 }
2001
d62a17ae 2002 /* Create new AS-external-LSA instance. */
2003 if ((new = ospf_external_lsa_new(ospf, ei, NULL)) == NULL) {
2004 if (IS_DEBUG_OSPF_EVENT)
2005 zlog_debug(
96b663a3
MS
2006 "LSA[Type5:%pI4]: Could not originate AS-external-LSA",
2007 &ei->p.prefix);
d62a17ae 2008 return NULL;
2009 }
2010
2011 /* Install newly created LSA into Type-5 LSDB, lock = 1. */
2012 ospf_lsa_install(ospf, NULL, new);
2013
2014 /* Update LSA origination count. */
2015 ospf->lsa_originate_count++;
2016
2017 /* Flooding new LSA. only to AS (non-NSSA/STUB) */
2018 ospf_flood_through_as(ospf, NULL, new);
2019
2020 /* If there is any attached NSSA, do special handling */
2021 if (ospf->anyNSSA &&
2022 /* stay away from translated LSAs! */
2023 !(CHECK_FLAG(new->flags, OSPF_LSA_LOCAL_XLT)))
2024 ospf_install_flood_nssa(
2025 ospf, new, ei); /* Install/Flood Type-7 to all NSSAs */
2026
2027 /* Debug logging. */
2028 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
96b663a3
MS
2029 zlog_debug("LSA[Type%d:%pI4]: Originate AS-external-LSA %p",
2030 new->data->type, &new->data->id,
d62a17ae 2031 (void *)new);
2032 ospf_lsa_header_dump(new->data);
2033 }
2034
2035 return new;
718e3744 2036}
2037
d62a17ae 2038static struct external_info *ospf_default_external_info(struct ospf *ospf)
2039{
2040 int type;
d62a17ae 2041 struct prefix_ipv4 p;
1febb13d
S
2042 struct external_info *default_ei;
2043 int ret = 0;
d62a17ae 2044
2045 p.family = AF_INET;
2046 p.prefix.s_addr = 0;
2047 p.prefixlen = 0;
2048
1febb13d
S
2049 default_ei = ospf_external_info_lookup(ospf, DEFAULT_ROUTE,
2050 ospf->instance, &p);
2051 if (!default_ei)
2052 return NULL;
2053
d62a17ae 2054 /* First, lookup redistributed default route. */
2055 for (type = 0; type <= ZEBRA_ROUTE_MAX; type++) {
2056 struct list *ext_list;
d62a17ae 2057
2058 if (type == ZEBRA_ROUTE_OSPF)
2059 continue;
2060
de1ac5fd 2061 ext_list = ospf->external[type];
d62a17ae 2062 if (!ext_list)
2063 continue;
2064
1febb13d
S
2065 ret = ospf_external_default_routemap_apply_walk(ospf, ext_list,
2066 default_ei);
2067 if (ret)
2068 return default_ei;
d62a17ae 2069 }
2070
2071 return NULL;
2072}
2073
fa3c7c7e 2074void ospf_external_lsa_rid_change(struct ospf *ospf)
d62a17ae 2075{
d62a17ae 2076 struct external_info *ei;
960417cf 2077 struct ospf_external_aggr_rt *aggr;
fa3c7c7e 2078 int type;
d62a17ae 2079
fa3c7c7e
DL
2080 for (type = 0; type < ZEBRA_ROUTE_MAX; type++) {
2081 struct route_node *rn;
2082 struct route_table *rt;
2083 struct list *ext_list;
2084 struct listnode *node;
2085 struct ospf_external *ext;
d62a17ae 2086
fa3c7c7e
DL
2087 ext_list = ospf->external[type];
2088 if (!ext_list)
2089 continue;
d62a17ae 2090
fa3c7c7e
DL
2091 for (ALL_LIST_ELEMENTS_RO(ext_list, node, ext)) {
2092 /* Originate As-external-LSA from all type of
2093 * distribute source.
2094 */
2095 rt = ext->external_info;
2096 if (!rt)
2097 continue;
d62a17ae 2098
fa3c7c7e
DL
2099 for (rn = route_top(rt); rn; rn = route_next(rn)) {
2100 ei = rn->info;
d62a17ae 2101
fa3c7c7e
DL
2102 if (!ei)
2103 continue;
2104
2105 if (is_prefix_default(
2106 (struct prefix_ipv4 *)&ei->p))
2107 continue;
2108
960417cf 2109 if (!ospf_redistribute_check(ospf, ei, NULL))
2110 continue;
2111
2112 aggr = ospf_external_aggr_match(ospf, &ei->p);
2113 if (aggr) {
2114 if (IS_DEBUG_OSPF(lsa, EXTNL_LSA_AGGR))
2115 zlog_debug(
2116 "Originate Summary LSA after reset/router-ID change");
2117 /* Here the LSA is originated as new */
2118 ospf_originate_summary_lsa(ospf, aggr,
2119 ei);
2120 } else if (!ospf_external_lsa_originate(ospf,
2121 ei))
fa3c7c7e 2122 flog_warn(EC_OSPF_LSA_INSTALL_FAILURE,
960417cf 2123 "LSA: AS-external-LSA was not originated.");
fa3c7c7e
DL
2124 }
2125 }
2126 }
2127
2128 ei = ospf_default_external_info(ospf);
2129 if (ei && !ospf_external_lsa_originate(ospf, ei)) {
2130 flog_warn(EC_OSPF_LSA_INSTALL_FAILURE,
2131 "LSA: AS-external-LSA for default route was not originated.");
2132 }
718e3744 2133}
2134
645878f1 2135/* Flush any NSSA LSAs for given prefix */
d62a17ae 2136void ospf_nssa_lsa_flush(struct ospf *ospf, struct prefix_ipv4 *p)
2137{
2138 struct listnode *node, *nnode;
b5a8894d 2139 struct ospf_lsa *lsa = NULL;
d62a17ae 2140 struct ospf_area *area;
2141
2142 for (ALL_LIST_ELEMENTS(ospf->areas, node, nnode, area)) {
2143 if (area->external_routing == OSPF_AREA_NSSA) {
996c9314
LB
2144 lsa = ospf_lsa_lookup(ospf, area, OSPF_AS_NSSA_LSA,
2145 p->prefix, ospf->router_id);
b5a8894d 2146 if (!lsa) {
d62a17ae 2147 if (IS_DEBUG_OSPF(lsa, LSA_FLOODING))
2148 zlog_debug(
ae32e1c2
DS
2149 "LSA: There is no such AS-NSSA-LSA %pFX in LSDB",
2150 p);
d62a17ae 2151 continue;
2152 }
2153 ospf_ls_retransmit_delete_nbr_area(area, lsa);
2154 if (!IS_LSA_MAXAGE(lsa)) {
2155 ospf_refresher_unregister_lsa(ospf, lsa);
2156 ospf_lsa_flush_area(lsa, area);
2157 }
2158 }
2159 }
645878f1 2160}
645878f1 2161
718e3744 2162/* Flush an AS-external-LSA from LSDB and routing domain. */
d7c0a89a 2163void ospf_external_lsa_flush(struct ospf *ospf, uint8_t type,
d62a17ae 2164 struct prefix_ipv4 *p,
2165 ifindex_t ifindex /*, struct in_addr nexthop */)
2166{
2167 struct ospf_lsa *lsa;
2168
2169 if (IS_DEBUG_OSPF(lsa, LSA_FLOODING))
ae32e1c2 2170 zlog_debug("LSA: Flushing AS-external-LSA %pFX", p);
d62a17ae 2171
2172 /* First lookup LSA from LSDB. */
2173 if (!(lsa = ospf_external_info_find_lsa(ospf, p))) {
2174 if (IS_DEBUG_OSPF(lsa, LSA_FLOODING))
2175 zlog_debug(
ae32e1c2
DS
2176 "LSA: There is no such AS-external-LSA %pFX in LSDB",
2177 p);
d62a17ae 2178 return;
2179 }
2180
2181 /* If LSA is selforiginated, not a translated LSA, and there is
2182 * NSSA area, flush Type-7 LSA's at first.
2183 */
2184 if (IS_LSA_SELF(lsa) && (ospf->anyNSSA)
2185 && !(CHECK_FLAG(lsa->flags, OSPF_LSA_LOCAL_XLT)))
2186 ospf_nssa_lsa_flush(ospf, p);
2187
2188 /* Sweep LSA from Link State Retransmit List. */
2189 ospf_ls_retransmit_delete_nbr_as(ospf, lsa);
2190
d62a17ae 2191 if (!IS_LSA_MAXAGE(lsa)) {
2192 /* Unregister LSA from Refresh queue. */
2193 ospf_refresher_unregister_lsa(ospf, lsa);
2194
2195 /* Flush AS-external-LSA through AS. */
2196 ospf_lsa_flush_as(ospf, lsa);
2197 }
2198
2199 if (IS_DEBUG_OSPF(lsa, LSA_FLOODING))
2200 zlog_debug("ospf_external_lsa_flush(): stop");
2201}
2202
2203void ospf_external_lsa_refresh_default(struct ospf *ospf)
2204{
2205 struct prefix_ipv4 p;
2206 struct external_info *ei;
2207 struct ospf_lsa *lsa;
2208
2209 p.family = AF_INET;
2210 p.prefixlen = 0;
975a328e 2211 p.prefix.s_addr = INADDR_ANY;
d62a17ae 2212
2213 ei = ospf_default_external_info(ospf);
2214 lsa = ospf_external_info_find_lsa(ospf, &p);
2215
d5eac1e0
DL
2216 if (ei && lsa) {
2217 if (IS_DEBUG_OSPF_EVENT)
2218 zlog_debug("LSA[Type5:0.0.0.0]: Refresh AS-external-LSA %p",
63f0e941 2219 (void *)lsa);
2220 ospf_external_lsa_refresh(ospf, lsa, ei, LSA_REFRESH_FORCE,
2221 false);
d5eac1e0
DL
2222 } else if (ei && !lsa) {
2223 if (IS_DEBUG_OSPF_EVENT)
2224 zlog_debug(
2225 "LSA[Type5:0.0.0.0]: Originate AS-external-LSA");
2226 ospf_external_lsa_originate(ospf, ei);
2227 } else if (lsa) {
2228 if (IS_DEBUG_OSPF_EVENT)
2229 zlog_debug("LSA[Type5:0.0.0.0]: Flush AS-external-LSA");
2230 ospf_external_lsa_flush(ospf, DEFAULT_ROUTE, &p, 0);
d62a17ae 2231 }
2232}
2233
d7c0a89a
QY
2234void ospf_external_lsa_refresh_type(struct ospf *ospf, uint8_t type,
2235 unsigned short instance, int force)
d62a17ae 2236{
2237 struct route_node *rn;
2238 struct external_info *ei;
2239 struct ospf_external *ext;
2240
de1ac5fd
CS
2241 if (type == DEFAULT_ROUTE)
2242 return;
2243
2244 ext = ospf_external_lookup(ospf, type, instance);
2245
2246 if (ext && EXTERNAL_INFO(ext)) {
2247 /* Refresh each redistributed AS-external-LSAs. */
2248 for (rn = route_top(EXTERNAL_INFO(ext)); rn;
2249 rn = route_next(rn)) {
2250 ei = rn->info;
2251 if (ei) {
2252 if (!is_prefix_default(&ei->p)) {
2253 struct ospf_lsa *lsa;
960417cf 2254 struct ospf_external_aggr_rt *aggr;
de1ac5fd 2255
960417cf 2256 aggr = ospf_external_aggr_match(ospf,
2257 &ei->p);
996c9314 2258 lsa = ospf_external_info_find_lsa(
960417cf 2259 ospf, &ei->p);
2260 if (aggr) {
2261 /* Check the AS-external-LSA
2262 * should be originated.
2263 */
2264 if (!ospf_redistribute_check(
2265 ospf, ei, NULL)) {
2266
2267 ospf_unlink_ei_from_aggr(
2268 ospf, aggr, ei);
2269 continue;
2270 }
2271
2272 if (IS_DEBUG_OSPF(
2273 lsa,
2274 EXTNL_LSA_AGGR))
2275 zlog_debug(
f1db813d 2276 "%s: Send Aggreate LSA (%pFX)",
960417cf 2277 __func__,
f1db813d 2278 &aggr->p);
960417cf 2279
2280 ospf_originate_summary_lsa(
2281 ospf, aggr, ei);
2282
2283 } else if (lsa) {
2284
2285 if (IS_LSA_MAXAGE(lsa))
2286 force = LSA_REFRESH_FORCE;
2287
996c9314 2288 ospf_external_lsa_refresh(
63f0e941 2289 ospf, lsa, ei, force,
2290 false);
960417cf 2291 } else {
2292 if (!ospf_redistribute_check(
2293 ospf, ei, NULL))
2294 continue;
996c9314
LB
2295 ospf_external_lsa_originate(
2296 ospf, ei);
960417cf 2297 }
de1ac5fd
CS
2298 }
2299 }
2300 }
2301 }
718e3744 2302}
2303
2304/* Refresh AS-external-LSA. */
d62a17ae 2305struct ospf_lsa *ospf_external_lsa_refresh(struct ospf *ospf,
2306 struct ospf_lsa *lsa,
63f0e941 2307 struct external_info *ei, int force,
2308 bool is_aggr)
d62a17ae 2309{
2310 struct ospf_lsa *new;
63f0e941 2311 int changed = 0;
d62a17ae 2312
2313 /* Check the AS-external-LSA should be originated. */
63f0e941 2314 if (!is_aggr)
2315 if (!ospf_redistribute_check(ospf, ei, &changed)) {
2316 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
2317 zlog_debug(
ce513ac6 2318 "LSA[Type%d:%pI4] Could not be refreshed, redist check fail",
63f0e941 2319 lsa->data->type,
ce513ac6 2320 &lsa->data->id);
63f0e941 2321
2322 ospf_external_lsa_flush(ospf, ei->type, &ei->p,
2323 ei->ifindex /*, ei->nexthop */);
2324 return NULL;
2325 }
d62a17ae 2326
2327 if (!changed && !force) {
2328 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
2329 zlog_debug(
96b663a3
MS
2330 "LSA[Type%d:%pI4]: Not refreshed, not changed/forced",
2331 lsa->data->type, &lsa->data->id);
d62a17ae 2332 return NULL;
2333 }
2334
2335 /* Delete LSA from neighbor retransmit-list. */
2336 ospf_ls_retransmit_delete_nbr_as(ospf, lsa);
2337
2338 /* Unregister AS-external-LSA from refresh-list. */
2339 ospf_refresher_unregister_lsa(ospf, lsa);
2340
2341 new = ospf_external_lsa_new(ospf, ei, &lsa->data->id);
2342
2343 if (new == NULL) {
2344 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
96b663a3
MS
2345 zlog_debug("LSA[Type%d:%pI4]: Could not be refreshed",
2346 lsa->data->type, &lsa->data->id);
d62a17ae 2347 return NULL;
2348 }
2349
2350 new->data->ls_seqnum = lsa_seqnum_increment(lsa);
2351
2352 ospf_lsa_install(ospf, NULL, new); /* As type-5. */
2353
2354 /* Flood LSA through AS. */
2355 ospf_flood_through_as(ospf, NULL, new);
2356
2357 /* If any attached NSSA, install as Type-7, flood to all NSSA Areas */
2358 if (ospf->anyNSSA && !(CHECK_FLAG(new->flags, OSPF_LSA_LOCAL_XLT)))
2359 ospf_install_flood_nssa(ospf, new,
2360 ei); /* Install/Flood per new rules */
2361
2362 /* Register self-originated LSA to refresh queue.
2363 * Translated LSAs should not be registered, but refreshed upon
2364 * refresh of the Type-7
2365 */
2366 if (!CHECK_FLAG(new->flags, OSPF_LSA_LOCAL_XLT))
2367 ospf_refresher_register_lsa(ospf, new);
2368
2369 /* Debug logging. */
2370 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
96b663a3
MS
2371 zlog_debug("LSA[Type%d:%pI4]: AS-external-LSA refresh",
2372 new->data->type, &new->data->id);
d62a17ae 2373 ospf_lsa_header_dump(new->data);
2374 }
2375
2376 return new;
718e3744 2377}
2378
6b0655a2 2379
718e3744 2380/* LSA installation functions. */
2381
2382/* Install router-LSA to an area. */
4dadc291 2383static struct ospf_lsa *
d62a17ae 2384ospf_router_lsa_install(struct ospf *ospf, struct ospf_lsa *new, int rt_recalc)
718e3744 2385{
d62a17ae 2386 struct ospf_area *area = new->area;
718e3744 2387
d62a17ae 2388 /* RFC 2328 Section 13.2 Router-LSAs and network-LSAs
2389 The entire routing table must be recalculated, starting with
2390 the shortest path calculations for each area (not just the
2391 area whose link-state database has changed).
2392 */
718e3744 2393
d62a17ae 2394 if (IS_LSA_SELF(new)) {
5996e0df 2395
d62a17ae 2396 /* Only install LSA if it is originated/refreshed by us.
2397 * If LSA was received by flooding, the RECEIVED flag is set so
2398 * do
2399 * not link the LSA */
2400 if (CHECK_FLAG(new->flags, OSPF_LSA_RECEIVED))
2401 return new; /* ignore stale LSA */
5996e0df 2402
d62a17ae 2403 /* Set self-originated router-LSA. */
2404 ospf_lsa_unlock(&area->router_lsa_self);
2405 area->router_lsa_self = ospf_lsa_lock(new);
718e3744 2406
d62a17ae 2407 ospf_refresher_register_lsa(ospf, new);
2408 }
2409 if (rt_recalc)
2410 ospf_spf_calculate_schedule(ospf, SPF_FLAG_ROUTER_LSA_INSTALL);
2411 return new;
718e3744 2412}
2413
d62a17ae 2414#define OSPF_INTERFACE_TIMER_ON(T, F, V) \
2415 if (!(T)) \
2416 (T) = thread_add_timer(master, (F), oi, (V))
718e3744 2417
2418/* Install network-LSA to an area. */
d62a17ae 2419static struct ospf_lsa *ospf_network_lsa_install(struct ospf *ospf,
2420 struct ospf_interface *oi,
2421 struct ospf_lsa *new,
2422 int rt_recalc)
2423{
2424
2425 /* RFC 2328 Section 13.2 Router-LSAs and network-LSAs
2426 The entire routing table must be recalculated, starting with
2427 the shortest path calculations for each area (not just the
2428 area whose link-state database has changed).
2429 */
2430 if (IS_LSA_SELF(new)) {
2431 /* We supposed that when LSA is originated by us, we pass the
2432 int
2433 for which it was originated. If LSA was received by flooding,
2434 the RECEIVED flag is set, so we do not link the LSA to the
2435 int. */
2436 if (CHECK_FLAG(new->flags, OSPF_LSA_RECEIVED))
2437 return new; /* ignore stale LSA */
2438
2439 ospf_lsa_unlock(&oi->network_lsa_self);
2440 oi->network_lsa_self = ospf_lsa_lock(new);
2441 ospf_refresher_register_lsa(ospf, new);
2442 }
2443 if (rt_recalc)
2444 ospf_spf_calculate_schedule(ospf, SPF_FLAG_NETWORK_LSA_INSTALL);
2445
2446 return new;
718e3744 2447}
2448
2449/* Install summary-LSA to an area. */
4dadc291 2450static struct ospf_lsa *
d62a17ae 2451ospf_summary_lsa_install(struct ospf *ospf, struct ospf_lsa *new, int rt_recalc)
2452{
2453 if (rt_recalc && !IS_LSA_SELF(new)) {
2454/* RFC 2328 Section 13.2 Summary-LSAs
2455 The best route to the destination described by the summary-
2456 LSA must be recalculated (see Section 16.5). If this
2457 destination is an AS boundary router, it may also be
2458 necessary to re-examine all the AS-external-LSAs.
2459*/
718e3744 2460
d62a17ae 2461 ospf_spf_calculate_schedule(ospf, SPF_FLAG_SUMMARY_LSA_INSTALL);
d62a17ae 2462 }
718e3744 2463
d62a17ae 2464 if (IS_LSA_SELF(new))
2465 ospf_refresher_register_lsa(ospf, new);
718e3744 2466
d62a17ae 2467 return new;
718e3744 2468}
2469
2470/* Install ASBR-summary-LSA to an area. */
d62a17ae 2471static struct ospf_lsa *ospf_summary_asbr_lsa_install(struct ospf *ospf,
2472 struct ospf_lsa *new,
2473 int rt_recalc)
2474{
2475 if (rt_recalc && !IS_LSA_SELF(new)) {
2476/* RFC 2328 Section 13.2 Summary-LSAs
2477 The best route to the destination described by the summary-
2478 LSA must be recalculated (see Section 16.5). If this
2479 destination is an AS boundary router, it may also be
2480 necessary to re-examine all the AS-external-LSAs.
2481*/
d62a17ae 2482 ospf_spf_calculate_schedule(ospf,
2483 SPF_FLAG_ASBR_SUMMARY_LSA_INSTALL);
d62a17ae 2484 }
718e3744 2485
d62a17ae 2486 /* register LSA to refresh-list. */
2487 if (IS_LSA_SELF(new))
2488 ospf_refresher_register_lsa(ospf, new);
718e3744 2489
d62a17ae 2490 return new;
718e3744 2491}
2492
2493/* Install AS-external-LSA. */
d62a17ae 2494static struct ospf_lsa *ospf_external_lsa_install(struct ospf *ospf,
2495 struct ospf_lsa *new,
2496 int rt_recalc)
2497{
2498 ospf_ase_register_external_lsa(new, ospf);
2499 /* If LSA is not self-originated, calculate an external route. */
2500 if (rt_recalc) {
2501 /* RFC 2328 Section 13.2 AS-external-LSAs
2502 The best route to the destination described by the AS-
2503 external-LSA must be recalculated (see Section 16.6).
2504 */
2505
2506 if (!IS_LSA_SELF(new))
2507 ospf_ase_incremental_update(ospf, new);
2508 }
2509
2510 if (new->data->type == OSPF_AS_NSSA_LSA) {
2511 /* There is no point to register selforiginate Type-7 LSA for
2512 * refreshing. We rely on refreshing Type-5 LSA's
2513 */
2514 if (IS_LSA_SELF(new))
2515 return new;
2516 else {
2517 /* Try refresh type-5 translated LSA for this LSA, if
2518 * one exists.
2519 * New translations will be taken care of by the
2520 * abr_task.
2521 */
2522 ospf_translated_nssa_refresh(ospf, new, NULL);
493472ba 2523 ospf_schedule_abr_task(ospf);
d62a17ae 2524 }
2525 }
2526
2527 /* Register self-originated LSA to refresh queue.
2528 * Leave Translated LSAs alone if NSSA is enabled
2529 */
2530 if (IS_LSA_SELF(new) && !CHECK_FLAG(new->flags, OSPF_LSA_LOCAL_XLT))
2531 ospf_refresher_register_lsa(ospf, new);
2532
2533 return new;
2534}
2535
2536void ospf_discard_from_db(struct ospf *ospf, struct ospf_lsdb *lsdb,
2537 struct ospf_lsa *lsa)
2538{
2539 struct ospf_lsa *old;
2540
266469eb 2541 if (!lsdb)
d62a17ae 2542 return;
d62a17ae 2543
2544 old = ospf_lsdb_lookup(lsdb, lsa);
2545
2546 if (!old)
2547 return;
2548
2549 if (old->refresh_list >= 0)
2550 ospf_refresher_unregister_lsa(ospf, old);
2551
2552 switch (old->data->type) {
2553 case OSPF_AS_EXTERNAL_LSA:
2554 ospf_ase_unregister_external_lsa(old, ospf);
2555 ospf_ls_retransmit_delete_nbr_as(ospf, old);
2556 break;
2557 case OSPF_OPAQUE_AS_LSA:
2558 ospf_ls_retransmit_delete_nbr_as(ospf, old);
2559 break;
2560 case OSPF_AS_NSSA_LSA:
2561 ospf_ls_retransmit_delete_nbr_area(old->area, old);
2562 ospf_ase_unregister_external_lsa(old, ospf);
2563 break;
2564 default:
2565 ospf_ls_retransmit_delete_nbr_area(old->area, old);
2566 break;
2567 }
2568
2569 ospf_lsa_maxage_delete(ospf, old);
2570 ospf_lsa_discard(old);
2571}
2572
2573struct ospf_lsa *ospf_lsa_install(struct ospf *ospf, struct ospf_interface *oi,
2574 struct ospf_lsa *lsa)
2575{
2576 struct ospf_lsa *new = NULL;
2577 struct ospf_lsa *old = NULL;
2578 struct ospf_lsdb *lsdb = NULL;
2579 int rt_recalc;
2580
2581 /* Set LSDB. */
2582 switch (lsa->data->type) {
2583 /* kevinm */
2584 case OSPF_AS_NSSA_LSA:
2585 if (lsa->area)
2586 lsdb = lsa->area->lsdb;
2587 else
2588 lsdb = ospf->lsdb;
2589 break;
2590 case OSPF_AS_EXTERNAL_LSA:
2591 case OSPF_OPAQUE_AS_LSA:
2592 lsdb = ospf->lsdb;
2593 break;
2594 default:
a37befa7 2595 if (lsa->area)
2596 lsdb = lsa->area->lsdb;
d62a17ae 2597 break;
2598 }
2599
2600 assert(lsdb);
2601
2602 /* RFC 2328 13.2. Installing LSAs in the database
2603
2604 Installing a new LSA in the database, either as the result of
2605 flooding or a newly self-originated LSA, may cause the OSPF
2606 routing table structure to be recalculated. The contents of the
2607 new LSA should be compared to the old instance, if present. If
2608 there is no difference, there is no need to recalculate the
2609 routing table. When comparing an LSA to its previous instance,
2610 the following are all considered to be differences in contents:
2611
2612 o The LSA's Options field has changed.
2613
2614 o One of the LSA instances has LS age set to MaxAge, and
2615 the other does not.
2616
2617 o The length field in the LSA header has changed.
2618
2619 o The body of the LSA (i.e., anything outside the 20-byte
2620 LSA header) has changed. Note that this excludes changes
2621 in LS Sequence Number and LS Checksum.
2622
2623 */
2624 /* Look up old LSA and determine if any SPF calculation or incremental
2625 update is needed */
2626 old = ospf_lsdb_lookup(lsdb, lsa);
2627
2628 /* Do comparision and record if recalc needed. */
2629 rt_recalc = 0;
df074ec3 2630 if (old == NULL || ospf_lsa_different(old, lsa)) {
2631 /* Ref rfc3623 section 3.2.3
2632 * Installing new lsa or change in the existing LSA
2633 * or flushing existing LSA leads to topo change
2634 * and trigger SPF caculation.
2635 * So, router should be aborted from HELPER role
2636 * if it is detected as TOPO change.
2637 */
2638 if (CHECK_LSA_TYPE_1_TO_5_OR_7(lsa->data->type))
2639 ospf_helper_handle_topo_chg(ospf, lsa);
2640
d62a17ae 2641 rt_recalc = 1;
df074ec3 2642 }
d62a17ae 2643
2644 /*
2645 Sequence number check (Section 14.1 of rfc 2328)
2646 "Premature aging is used when it is time for a self-originated
2647 LSA's sequence number field to wrap. At this point, the current
2648 LSA instance (having LS sequence number MaxSequenceNumber) must
2649 be prematurely aged and flushed from the routing domain before a
2650 new instance with sequence number equal to InitialSequenceNumber
2651 can be originated. "
2652 */
2653
2654 if (ntohl(lsa->data->ls_seqnum) - 1 == OSPF_MAX_SEQUENCE_NUMBER) {
2655 if (ospf_lsa_is_self_originated(ospf, lsa)) {
2656 lsa->data->ls_seqnum = htonl(OSPF_MAX_SEQUENCE_NUMBER);
2657
2658 if (!IS_LSA_MAXAGE(lsa))
2659 lsa->flags |= OSPF_LSA_PREMATURE_AGE;
2660 lsa->data->ls_age = htons(OSPF_LSA_MAXAGE);
2661
2662 if (IS_DEBUG_OSPF(lsa, LSA_REFRESH)) {
2663 zlog_debug(
3efd0893 2664 "ospf_lsa_install() Premature Aging lsa 0x%p, seqnum 0x%x",
d62a17ae 2665 (void *)lsa,
2666 ntohl(lsa->data->ls_seqnum));
2667 ospf_lsa_header_dump(lsa->data);
2668 }
2669 } else {
2670 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE)) {
2671 zlog_debug(
1d5453d6 2672 "ospf_lsa_install() got an lsa with seq 0x80000000 that was not self originated. Ignoring");
d62a17ae 2673 ospf_lsa_header_dump(lsa->data);
2674 }
2675 return old;
2676 }
2677 }
2678
2679 /* discard old LSA from LSDB */
2680 if (old != NULL)
2681 ospf_discard_from_db(ospf, lsdb, lsa);
2682
2683 /* Calculate Checksum if self-originated?. */
2684 if (IS_LSA_SELF(lsa))
2685 ospf_lsa_checksum(lsa->data);
2686
2687 /* Insert LSA to LSDB. */
2688 ospf_lsdb_add(lsdb, lsa);
2689 lsa->lsdb = lsdb;
2690
2691 /* Do LSA specific installation process. */
2692 switch (lsa->data->type) {
2693 case OSPF_ROUTER_LSA:
2694 new = ospf_router_lsa_install(ospf, lsa, rt_recalc);
2695 break;
2696 case OSPF_NETWORK_LSA:
2697 assert(oi);
2698 new = ospf_network_lsa_install(ospf, oi, lsa, rt_recalc);
2699 break;
2700 case OSPF_SUMMARY_LSA:
2701 new = ospf_summary_lsa_install(ospf, lsa, rt_recalc);
2702 break;
2703 case OSPF_ASBR_SUMMARY_LSA:
2704 new = ospf_summary_asbr_lsa_install(ospf, lsa, rt_recalc);
2705 break;
2706 case OSPF_AS_EXTERNAL_LSA:
2707 new = ospf_external_lsa_install(ospf, lsa, rt_recalc);
2708 break;
2709 case OSPF_OPAQUE_LINK_LSA:
2710 if (IS_LSA_SELF(lsa))
2711 lsa->oi = oi; /* Specify outgoing ospf-interface for
2712 this LSA. */
2713 else {
2714 /* Incoming "oi" for this LSA has set at LSUpd
2715 * reception. */
2716 }
2717 /* Fallthrough */
2718 case OSPF_OPAQUE_AREA_LSA:
2719 case OSPF_OPAQUE_AS_LSA:
2720 new = ospf_opaque_lsa_install(lsa, rt_recalc);
2721 break;
2722 case OSPF_AS_NSSA_LSA:
2723 new = ospf_external_lsa_install(ospf, lsa, rt_recalc);
2724 default: /* type-6,8,9....nothing special */
2725 break;
2726 }
2727
2728 if (new == NULL)
2729 return new; /* Installation failed, cannot proceed further --
2730 endo. */
2731
2732 /* Debug logs. */
2733 if (IS_DEBUG_OSPF(lsa, LSA_INSTALL)) {
d62a17ae 2734 switch (lsa->data->type) {
2735 case OSPF_AS_EXTERNAL_LSA:
2736 case OSPF_OPAQUE_AS_LSA:
2737 case OSPF_AS_NSSA_LSA:
2738 zlog_debug("LSA[%s]: Install %s", dump_lsa_key(new),
2739 lookup_msg(ospf_lsa_type_msg,
2740 new->data->type, NULL));
2741 break;
2742 default:
96b663a3 2743 zlog_debug("LSA[%s]: Install %s to Area %pI4",
d62a17ae 2744 dump_lsa_key(new),
2745 lookup_msg(ospf_lsa_type_msg,
2746 new->data->type, NULL),
96b663a3 2747 &new->area->area_id);
d62a17ae 2748 break;
2749 }
2750 }
2751
2752 /*
2753 If received LSA' ls_age is MaxAge, or lsa is being prematurely aged
2754 (it's getting flushed out of the area), set LSA on MaxAge LSA list.
2755 */
2756 if (IS_LSA_MAXAGE(new)) {
2757 if (IS_DEBUG_OSPF(lsa, LSA_INSTALL))
96b663a3
MS
2758 zlog_debug("LSA[Type%d:%pI4]: Install LSA 0x%p, MaxAge",
2759 new->data->type, &new->data->id,
d62a17ae 2760 (void *)lsa);
2761 ospf_lsa_maxage(ospf, lsa);
2762 }
2763
2764 return new;
2765}
2766
2767
2768int ospf_check_nbr_status(struct ospf *ospf)
2769{
2770 struct listnode *node, *nnode;
2771 struct ospf_interface *oi;
2772
2773 for (ALL_LIST_ELEMENTS(ospf->oiflist, node, nnode, oi)) {
2774 struct route_node *rn;
2775 struct ospf_neighbor *nbr;
2776
2777 if (ospf_if_is_enable(oi))
2778 for (rn = route_top(oi->nbrs); rn; rn = route_next(rn))
2779 if ((nbr = rn->info) != NULL)
2780 if (nbr->state == NSM_Exchange
2781 || nbr->state == NSM_Loading) {
2782 route_unlock_node(rn);
2783 return 0;
2784 }
2785 }
2786
2787 return 1;
2788}
2789
2790
2791static int ospf_maxage_lsa_remover(struct thread *thread)
2792{
2793 struct ospf *ospf = THREAD_ARG(thread);
f91ce319 2794 struct ospf_lsa *lsa, *old;
d62a17ae 2795 struct route_node *rn;
2796 int reschedule = 0;
2797
2798 ospf->t_maxage = NULL;
2799
2800 if (IS_DEBUG_OSPF(lsa, LSA_FLOODING))
2801 zlog_debug("LSA[MaxAge]: remover Start");
2802
2803 reschedule = !ospf_check_nbr_status(ospf);
2804
2805 if (!reschedule)
2806 for (rn = route_top(ospf->maxage_lsa); rn;
2807 rn = route_next(rn)) {
2808 if ((lsa = rn->info) == NULL) {
2809 continue;
2810 }
2811
2812 /* There is at least one neighbor from which we still
2813 * await an ack
2814 * for that LSA, so we are not allowed to remove it from
2815 * our lsdb yet
2816 * as per RFC 2328 section 14 para 4 a) */
2817 if (lsa->retransmit_counter > 0) {
2818 reschedule = 1;
2819 continue;
2820 }
2821
2822 /* TODO: maybe convert this function to a work-queue */
2823 if (thread_should_yield(thread)) {
2824 OSPF_TIMER_ON(ospf->t_maxage,
2825 ospf_maxage_lsa_remover, 0);
2826 route_unlock_node(
2827 rn); /* route_top/route_next */
2828 return 0;
2829 }
2830
2831 /* Remove LSA from the LSDB */
2832 if (IS_LSA_SELF(lsa))
2833 if (IS_DEBUG_OSPF(lsa, LSA_FLOODING))
2834 zlog_debug(
96b663a3 2835 "LSA[Type%d:%pI4]: LSA 0x%lx is self-originated: ",
d62a17ae 2836 lsa->data->type,
96b663a3 2837 &lsa->data->id,
d7c0a89a 2838 (unsigned long)lsa);
d62a17ae 2839
2840 if (IS_DEBUG_OSPF(lsa, LSA_FLOODING))
2841 zlog_debug(
96b663a3 2842 "LSA[Type%d:%pI4]: MaxAge LSA removed from list",
d62a17ae 2843 lsa->data->type,
96b663a3 2844 &lsa->data->id);
d62a17ae 2845
2846 if (CHECK_FLAG(lsa->flags, OSPF_LSA_PREMATURE_AGE)) {
2847 if (IS_DEBUG_OSPF(lsa, LSA_FLOODING))
2848 zlog_debug(
63efca0e 2849 "originating new lsa for lsa 0x%p",
d62a17ae 2850 (void *)lsa);
2851 ospf_lsa_refresh(ospf, lsa);
2852 }
2853
2854 /* Remove from lsdb. */
2855 if (lsa->lsdb) {
f91ce319
MR
2856 old = ospf_lsdb_lookup(lsa->lsdb, lsa);
2857 /* The max age LSA here must be the same
2858 * as the LSA in LSDB
2859 */
2860 if (old != lsa) {
2861 flog_err(EC_OSPF_LSA_MISSING,
acc847c9
MS
2862 "%s: LSA[Type%d:%pI4]: LSA not in LSDB",
2863 __func__, lsa->data->type,
2864 &lsa->data->id);
2865
f91ce319
MR
2866 continue;
2867 }
d62a17ae 2868 ospf_discard_from_db(ospf, lsa->lsdb, lsa);
2869 ospf_lsdb_delete(lsa->lsdb, lsa);
13ab4921
DS
2870 } else {
2871 if (IS_DEBUG_OSPF(lsa, LSA_FLOODING))
2872 zlog_debug(
96b663a3 2873 "%s: LSA[Type%d:%pI4]: No associated LSDB!",
13ab4921 2874 __func__, lsa->data->type,
96b663a3 2875 &lsa->data->id);
13ab4921 2876 }
d62a17ae 2877 }
2878
2879 /* A MaxAge LSA must be removed immediately from the router's link
2880 state database as soon as both a) it is no longer contained on any
2881 neighbor Link state retransmission lists and b) none of the
2882 router's
2883 neighbors are in states Exchange or Loading. */
2884 if (reschedule)
2885 OSPF_TIMER_ON(ospf->t_maxage, ospf_maxage_lsa_remover,
2886 ospf->maxage_delay);
2887
2888 return 0;
2889}
2890
2891void ospf_lsa_maxage_delete(struct ospf *ospf, struct ospf_lsa *lsa)
2892{
2893 struct route_node *rn;
dcc3ef87 2894 struct prefix lsa_prefix;
d62a17ae 2895
dcc3ef87 2896 memset(&lsa_prefix, 0, sizeof(struct prefix));
d62a17ae 2897 lsa_prefix.family = 0;
dcc3ef87
CS
2898 lsa_prefix.prefixlen = sizeof(lsa_prefix.u.ptr) * CHAR_BIT;
2899 lsa_prefix.u.ptr = (uintptr_t)lsa;
d62a17ae 2900
c4efd0f4 2901 if ((rn = route_node_lookup(ospf->maxage_lsa, &lsa_prefix))) {
d62a17ae 2902 if (rn->info == lsa) {
2903 UNSET_FLAG(lsa->flags, OSPF_LSA_IN_MAXAGE);
2904 ospf_lsa_unlock(&lsa); /* maxage_lsa */
2905 rn->info = NULL;
2906 route_unlock_node(
2907 rn); /* unlock node because lsa is deleted */
2908 }
2909 route_unlock_node(rn); /* route_node_lookup */
dcc3ef87
CS
2910 } else {
2911 if (IS_DEBUG_OSPF_EVENT)
2912 zlog_debug("%s: lsa %s is not found in maxage db.",
15569c58 2913 __func__, dump_lsa_key(lsa));
d62a17ae 2914 }
718e3744 2915}
2916
02d942c9
PJ
2917/* Add LSA onto the MaxAge list, and schedule for removal.
2918 * This does *not* lead to the LSA being flooded, that must be taken
2919 * care of elsewhere, see, e.g., ospf_lsa_flush* (which are callers of this
2920 * function).
2921 */
d62a17ae 2922void ospf_lsa_maxage(struct ospf *ospf, struct ospf_lsa *lsa)
2923{
dcc3ef87 2924 struct prefix lsa_prefix;
d62a17ae 2925 struct route_node *rn;
2926
2927 /* When we saw a MaxAge LSA flooded to us, we put it on the list
2928 and schedule the MaxAge LSA remover. */
2929 if (CHECK_FLAG(lsa->flags, OSPF_LSA_IN_MAXAGE)) {
2930 if (IS_DEBUG_OSPF(lsa, LSA_FLOODING))
2931 zlog_debug(
96b663a3
MS
2932 "LSA[Type%d:%pI4]: %p already exists on MaxAge LSA list",
2933 lsa->data->type, &lsa->data->id,
d62a17ae 2934 (void *)lsa);
2935 return;
2936 }
2937
dcc3ef87 2938 memset(&lsa_prefix, 0, sizeof(struct prefix));
d62a17ae 2939 lsa_prefix.family = 0;
dcc3ef87
CS
2940 lsa_prefix.prefixlen = sizeof(lsa_prefix.u.ptr) * CHAR_BIT;
2941 lsa_prefix.u.ptr = (uintptr_t)lsa;
d62a17ae 2942
c4efd0f4 2943 rn = route_node_get(ospf->maxage_lsa, &lsa_prefix);
0ce1ca80
DS
2944 if (rn->info != NULL) {
2945 if (IS_DEBUG_OSPF(lsa, LSA_FLOODING))
2946 zlog_debug(
2947 "LSA[%s]: found LSA (%p) in table for LSA %p %d",
2948 dump_lsa_key(lsa), rn->info,
2949 (void *)lsa, lsa_prefix.prefixlen);
2950 route_unlock_node(rn);
d62a17ae 2951 } else {
0ce1ca80
DS
2952 rn->info = ospf_lsa_lock(lsa);
2953 SET_FLAG(lsa->flags, OSPF_LSA_IN_MAXAGE);
d62a17ae 2954 }
2955
2956 if (IS_DEBUG_OSPF(lsa, LSA_FLOODING))
2957 zlog_debug("LSA[%s]: MaxAge LSA remover scheduled.",
2958 dump_lsa_key(lsa));
2959
2960 OSPF_TIMER_ON(ospf->t_maxage, ospf_maxage_lsa_remover,
2961 ospf->maxage_delay);
2962}
2963
2964static int ospf_lsa_maxage_walker_remover(struct ospf *ospf,
2965 struct ospf_lsa *lsa)
2966{
2967 /* Stay away from any Local Translated Type-7 LSAs */
2968 if (CHECK_FLAG(lsa->flags, OSPF_LSA_LOCAL_XLT))
2969 return 0;
2970
2971 if (IS_LSA_MAXAGE(lsa))
2972 /* Self-originated LSAs should NOT time-out instead,
2973 they're flushed and submitted to the max_age list explicitly.
2974 */
2975 if (!ospf_lsa_is_self_originated(ospf, lsa)) {
2976 if (IS_DEBUG_OSPF(lsa, LSA_FLOODING))
2977 zlog_debug("LSA[%s]: is MaxAge",
2978 dump_lsa_key(lsa));
2979
2980 switch (lsa->data->type) {
2981 case OSPF_OPAQUE_LINK_LSA:
2982 case OSPF_OPAQUE_AREA_LSA:
2983 case OSPF_OPAQUE_AS_LSA:
2984 /*
2985 * As a general rule, whenever network topology
2986 * has changed
2987 * (due to an LSA removal in this case), routing
2988 * recalculation
2989 * should be triggered. However, this is not
2990 * true for opaque
2991 * LSAs. Even if an opaque LSA instance is going
2992 * to be removed
2993 * from the routing domain, it does not mean a
2994 * change in network
2995 * topology, and thus, routing recalculation is
2996 * not needed here.
2997 */
2998 break;
2999 case OSPF_AS_EXTERNAL_LSA:
3000 case OSPF_AS_NSSA_LSA:
3001 ospf_ase_incremental_update(ospf, lsa);
3002 break;
3003 default:
3004 ospf_spf_calculate_schedule(ospf,
3005 SPF_FLAG_MAXAGE);
3006 break;
3007 }
3008 ospf_lsa_maxage(ospf, lsa);
3009 }
3010
3011 if (IS_LSA_MAXAGE(lsa) && !ospf_lsa_is_self_originated(ospf, lsa))
3012 if (LS_AGE(lsa) > OSPF_LSA_MAXAGE + 30)
3013 printf("Eek! Shouldn't happen!\n");
3014
3015 return 0;
718e3744 3016}
3017
3018/* Periodical check of MaxAge LSA. */
d62a17ae 3019int ospf_lsa_maxage_walker(struct thread *thread)
3020{
3021 struct ospf *ospf = THREAD_ARG(thread);
3022 struct route_node *rn;
3023 struct ospf_lsa *lsa;
3024 struct ospf_area *area;
3025 struct listnode *node, *nnode;
3026
3027 ospf->t_maxage_walker = NULL;
3028
3029 for (ALL_LIST_ELEMENTS(ospf->areas, node, nnode, area)) {
996c9314 3030 LSDB_LOOP (ROUTER_LSDB(area), rn, lsa)
044506e7 3031 ospf_lsa_maxage_walker_remover(ospf, lsa);
996c9314 3032 LSDB_LOOP (NETWORK_LSDB(area), rn, lsa)
044506e7 3033 ospf_lsa_maxage_walker_remover(ospf, lsa);
996c9314 3034 LSDB_LOOP (SUMMARY_LSDB(area), rn, lsa)
044506e7 3035 ospf_lsa_maxage_walker_remover(ospf, lsa);
996c9314 3036 LSDB_LOOP (ASBR_SUMMARY_LSDB(area), rn, lsa)
044506e7 3037 ospf_lsa_maxage_walker_remover(ospf, lsa);
996c9314 3038 LSDB_LOOP (OPAQUE_AREA_LSDB(area), rn, lsa)
044506e7 3039 ospf_lsa_maxage_walker_remover(ospf, lsa);
996c9314 3040 LSDB_LOOP (OPAQUE_LINK_LSDB(area), rn, lsa)
044506e7 3041 ospf_lsa_maxage_walker_remover(ospf, lsa);
996c9314 3042 LSDB_LOOP (NSSA_LSDB(area), rn, lsa)
044506e7 3043 ospf_lsa_maxage_walker_remover(ospf, lsa);
d62a17ae 3044 }
3045
3046 /* for AS-external-LSAs. */
3047 if (ospf->lsdb) {
996c9314 3048 LSDB_LOOP (EXTERNAL_LSDB(ospf), rn, lsa)
044506e7 3049 ospf_lsa_maxage_walker_remover(ospf, lsa);
996c9314 3050 LSDB_LOOP (OPAQUE_AS_LSDB(ospf), rn, lsa)
044506e7 3051 ospf_lsa_maxage_walker_remover(ospf, lsa);
d62a17ae 3052 }
3053
3054 OSPF_TIMER_ON(ospf->t_maxage_walker, ospf_lsa_maxage_walker,
3055 OSPF_LSA_MAXAGE_CHECK_INTERVAL);
3056 return 0;
3057}
3058
d7c0a89a 3059struct ospf_lsa *ospf_lsa_lookup_by_prefix(struct ospf_lsdb *lsdb, uint8_t type,
d62a17ae 3060 struct prefix_ipv4 *p,
3061 struct in_addr router_id)
3062{
3063 struct ospf_lsa *lsa;
3064 struct in_addr mask, id;
3065 struct lsa_header_mask {
3066 struct lsa_header header;
3067 struct in_addr mask;
3068 } * hmask;
3069
3070 lsa = ospf_lsdb_lookup_by_id(lsdb, type, p->prefix, router_id);
3071 if (lsa == NULL)
3072 return NULL;
3073
3074 masklen2ip(p->prefixlen, &mask);
3075
3076 hmask = (struct lsa_header_mask *)lsa->data;
3077
3078 if (mask.s_addr != hmask->mask.s_addr) {
3079 id.s_addr = p->prefix.s_addr | (~mask.s_addr);
3080 lsa = ospf_lsdb_lookup_by_id(lsdb, type, id, router_id);
3081 if (!lsa)
3082 return NULL;
3083 }
3084
3085 return lsa;
3086}
3087
b5a8894d 3088struct ospf_lsa *ospf_lsa_lookup(struct ospf *ospf, struct ospf_area *area,
d7c0a89a 3089 uint32_t type, struct in_addr id,
b5a8894d 3090 struct in_addr adv_router)
d62a17ae 3091{
b5a8894d
CS
3092 if (!ospf)
3093 return NULL;
d62a17ae 3094
3095 switch (type) {
3096 case OSPF_ROUTER_LSA:
3097 case OSPF_NETWORK_LSA:
3098 case OSPF_SUMMARY_LSA:
3099 case OSPF_ASBR_SUMMARY_LSA:
3100 case OSPF_AS_NSSA_LSA:
3101 case OSPF_OPAQUE_LINK_LSA:
3102 case OSPF_OPAQUE_AREA_LSA:
3103 return ospf_lsdb_lookup_by_id(area->lsdb, type, id, adv_router);
3104 case OSPF_AS_EXTERNAL_LSA:
3105 case OSPF_OPAQUE_AS_LSA:
3106 return ospf_lsdb_lookup_by_id(ospf->lsdb, type, id, adv_router);
3107 default:
3108 break;
3109 }
3110
3111 return NULL;
3112}
3113
d7c0a89a 3114struct ospf_lsa *ospf_lsa_lookup_by_id(struct ospf_area *area, uint32_t type,
d62a17ae 3115 struct in_addr id)
3116{
3117 struct ospf_lsa *lsa;
3118 struct route_node *rn;
3119
3120 switch (type) {
3121 case OSPF_ROUTER_LSA:
3122 return ospf_lsdb_lookup_by_id(area->lsdb, type, id, id);
3123 case OSPF_NETWORK_LSA:
3124 for (rn = route_top(NETWORK_LSDB(area)); rn;
3125 rn = route_next(rn))
3126 if ((lsa = rn->info))
3127 if (IPV4_ADDR_SAME(&lsa->data->id, &id)) {
3128 route_unlock_node(rn);
3129 return lsa;
3130 }
3131 break;
3132 case OSPF_SUMMARY_LSA:
3133 case OSPF_ASBR_SUMMARY_LSA:
3134 /* Currently not used. */
3135 assert(1);
3136 return ospf_lsdb_lookup_by_id(area->lsdb, type, id, id);
3137 case OSPF_AS_EXTERNAL_LSA:
3138 case OSPF_AS_NSSA_LSA:
3139 case OSPF_OPAQUE_LINK_LSA:
3140 case OSPF_OPAQUE_AREA_LSA:
3141 case OSPF_OPAQUE_AS_LSA:
3142 /* Currently not used. */
3143 break;
3144 default:
3145 break;
3146 }
3147
3148 return NULL;
3149}
3150
3151struct ospf_lsa *ospf_lsa_lookup_by_header(struct ospf_area *area,
3152 struct lsa_header *lsah)
3153{
3154 struct ospf_lsa *match;
3155
3156 /*
3157 * Strictly speaking, the LSA-ID field for Opaque-LSAs (type-9/10/11)
3158 * is redefined to have two subfields; opaque-type and opaque-id.
3159 * However, it is harmless to treat the two sub fields together, as if
3160 * they two were forming a unique LSA-ID.
3161 */
3162
b5a8894d
CS
3163 match = ospf_lsa_lookup(area->ospf, area, lsah->type, lsah->id,
3164 lsah->adv_router);
d62a17ae 3165
3166 if (match == NULL)
3167 if (IS_DEBUG_OSPF(lsa, LSA) == OSPF_DEBUG_LSA)
96b663a3
MS
3168 zlog_debug("LSA[Type%d:%pI4]: Lookup by header, NO MATCH",
3169 lsah->type, &lsah->id);
d62a17ae 3170
3171 return match;
718e3744 3172}
3173
3174/* return +n, l1 is more recent.
3175 return -n, l2 is more recent.
3176 return 0, l1 and l2 is identical. */
d62a17ae 3177int ospf_lsa_more_recent(struct ospf_lsa *l1, struct ospf_lsa *l2)
3178{
3179 int r;
3180 int x, y;
3181
3182 if (l1 == NULL && l2 == NULL)
3183 return 0;
3184 if (l1 == NULL)
3185 return -1;
3186 if (l2 == NULL)
3187 return 1;
3188
3189 /* compare LS sequence number. */
3190 x = (int)ntohl(l1->data->ls_seqnum);
3191 y = (int)ntohl(l2->data->ls_seqnum);
3192 if (x > y)
3193 return 1;
3194 if (x < y)
3195 return -1;
3196
3197 /* compare LS checksum. */
3198 r = ntohs(l1->data->checksum) - ntohs(l2->data->checksum);
3199 if (r)
3200 return r;
3201
3202 /* compare LS age. */
3203 if (IS_LSA_MAXAGE(l1) && !IS_LSA_MAXAGE(l2))
3204 return 1;
3205 else if (!IS_LSA_MAXAGE(l1) && IS_LSA_MAXAGE(l2))
3206 return -1;
3207
3208 /* compare LS age with MaxAgeDiff. */
3209 if (LS_AGE(l1) - LS_AGE(l2) > OSPF_LSA_MAXAGE_DIFF)
3210 return -1;
3211 else if (LS_AGE(l2) - LS_AGE(l1) > OSPF_LSA_MAXAGE_DIFF)
3212 return 1;
3213
3214 /* LSAs are identical. */
3215 return 0;
718e3744 3216}
3217
3218/* If two LSAs are different, return 1, otherwise return 0. */
d62a17ae 3219int ospf_lsa_different(struct ospf_lsa *l1, struct ospf_lsa *l2)
718e3744 3220{
d62a17ae 3221 char *p1, *p2;
3222 assert(l1);
3223 assert(l2);
3224 assert(l1->data);
3225 assert(l2->data);
718e3744 3226
d62a17ae 3227 if (l1->data->options != l2->data->options)
3228 return 1;
718e3744 3229
d62a17ae 3230 if (IS_LSA_MAXAGE(l1) && !IS_LSA_MAXAGE(l2))
3231 return 1;
718e3744 3232
d62a17ae 3233 if (IS_LSA_MAXAGE(l2) && !IS_LSA_MAXAGE(l1))
3234 return 1;
718e3744 3235
d62a17ae 3236 if (l1->data->length != l2->data->length)
3237 return 1;
718e3744 3238
d62a17ae 3239 if (l1->data->length == 0)
3240 return 1;
718e3744 3241
d62a17ae 3242 if (CHECK_FLAG((l1->flags ^ l2->flags), OSPF_LSA_RECEIVED))
3243 return 1; /* May be a stale LSA in the LSBD */
5996e0df 3244
d62a17ae 3245 assert(ntohs(l1->data->length) > OSPF_LSA_HEADER_SIZE);
718e3744 3246
d62a17ae 3247 p1 = (char *)l1->data;
3248 p2 = (char *)l2->data;
718e3744 3249
d62a17ae 3250 if (memcmp(p1 + OSPF_LSA_HEADER_SIZE, p2 + OSPF_LSA_HEADER_SIZE,
3251 ntohs(l1->data->length) - OSPF_LSA_HEADER_SIZE)
3252 != 0)
3253 return 1;
718e3744 3254
d62a17ae 3255 return 0;
718e3744 3256}
3257
d62a17ae 3258int ospf_lsa_flush_schedule(struct ospf *ospf, struct ospf_lsa *lsa)
3259{
3260 if (lsa == NULL || !IS_LSA_SELF(lsa))
3261 return 0;
3262
3263 if (IS_DEBUG_OSPF_EVENT)
3264 zlog_debug(
96b663a3
MS
3265 "LSA[Type%d:%pI4]: Schedule self-originated LSA to FLUSH",
3266 lsa->data->type, &lsa->data->id);
d62a17ae 3267
3268 /* Force given lsa's age to MaxAge. */
3269 lsa->data->ls_age = htons(OSPF_LSA_MAXAGE);
3270
3271 switch (lsa->data->type) {
3272 /* Opaque wants to be notified of flushes */
3273 case OSPF_OPAQUE_LINK_LSA:
3274 case OSPF_OPAQUE_AREA_LSA:
3275 case OSPF_OPAQUE_AS_LSA:
3276 ospf_opaque_lsa_refresh(lsa);
3277 break;
3278 default:
3279 ospf_refresher_unregister_lsa(ospf, lsa);
3280 ospf_lsa_flush(ospf, lsa);
3281 break;
3282 }
3283
3284 return 0;
3285}
3286
3287void ospf_flush_self_originated_lsas_now(struct ospf *ospf)
3288{
3289 struct listnode *node, *nnode;
3290 struct listnode *node2, *nnode2;
3291 struct ospf_area *area;
3292 struct ospf_interface *oi;
3293 struct ospf_lsa *lsa;
3294 struct route_node *rn;
3295 int need_to_flush_ase = 0;
3296
046460a1
CS
3297 ospf->inst_shutdown = 1;
3298
d62a17ae 3299 for (ALL_LIST_ELEMENTS(ospf->areas, node, nnode, area)) {
3300 if ((lsa = area->router_lsa_self) != NULL) {
3301 if (IS_DEBUG_OSPF_EVENT)
3302 zlog_debug(
96b663a3 3303 "LSA[Type%d:%pI4]: Schedule self-originated LSA to FLUSH",
d62a17ae 3304 lsa->data->type,
96b663a3 3305 &lsa->data->id);
d62a17ae 3306
3307 ospf_refresher_unregister_lsa(ospf, lsa);
3308 ospf_lsa_flush_area(lsa, area);
3309 ospf_lsa_unlock(&area->router_lsa_self);
3310 area->router_lsa_self = NULL;
3311 }
3312
3313 for (ALL_LIST_ELEMENTS(area->oiflist, node2, nnode2, oi)) {
3314 if ((lsa = oi->network_lsa_self) != NULL
3315 && oi->state == ISM_DR && oi->full_nbrs > 0) {
3316 if (IS_DEBUG_OSPF_EVENT)
3317 zlog_debug(
96b663a3 3318 "LSA[Type%d:%pI4]: Schedule self-originated LSA to FLUSH",
d62a17ae 3319 lsa->data->type,
96b663a3 3320 &lsa->data->id);
d62a17ae 3321
3322 ospf_refresher_unregister_lsa(
3323 ospf, oi->network_lsa_self);
3324 ospf_lsa_flush_area(oi->network_lsa_self, area);
3325 ospf_lsa_unlock(&oi->network_lsa_self);
3326 oi->network_lsa_self = NULL;
3327 }
3328
3329 if (oi->type != OSPF_IFTYPE_VIRTUALLINK
3330 && area->external_routing == OSPF_AREA_DEFAULT)
3331 need_to_flush_ase = 1;
3332 }
3333
996c9314 3334 LSDB_LOOP (SUMMARY_LSDB(area), rn, lsa)
044506e7 3335 ospf_lsa_flush_schedule(ospf, lsa);
996c9314 3336 LSDB_LOOP (ASBR_SUMMARY_LSDB(area), rn, lsa)
044506e7 3337 ospf_lsa_flush_schedule(ospf, lsa);
996c9314 3338 LSDB_LOOP (OPAQUE_LINK_LSDB(area), rn, lsa)
044506e7 3339 ospf_lsa_flush_schedule(ospf, lsa);
996c9314 3340 LSDB_LOOP (OPAQUE_AREA_LSDB(area), rn, lsa)
044506e7 3341 ospf_lsa_flush_schedule(ospf, lsa);
d62a17ae 3342 }
3343
3344 if (need_to_flush_ase) {
996c9314 3345 LSDB_LOOP (EXTERNAL_LSDB(ospf), rn, lsa)
044506e7 3346 ospf_lsa_flush_schedule(ospf, lsa);
996c9314 3347 LSDB_LOOP (OPAQUE_AS_LSDB(ospf), rn, lsa)
044506e7 3348 ospf_lsa_flush_schedule(ospf, lsa);
d62a17ae 3349 }
3350
3351 /*
3352 * Make sure that the MaxAge LSA remover is executed immediately,
3353 * without conflicting to other threads.
3354 */
3355 if (ospf->t_maxage != NULL) {
3356 OSPF_TIMER_OFF(ospf->t_maxage);
3357 thread_execute(master, ospf_maxage_lsa_remover, ospf, 0);
3358 }
3359
3360 return;
718e3744 3361}
718e3744 3362
3363/* If there is self-originated LSA, then return 1, otherwise return 0. */
3364/* An interface-independent version of ospf_lsa_is_self_originated */
d62a17ae 3365int ospf_lsa_is_self_originated(struct ospf *ospf, struct ospf_lsa *lsa)
3366{
3367 struct listnode *node;
3368 struct ospf_interface *oi;
3369
3370 /* This LSA is already checked. */
3371 if (CHECK_FLAG(lsa->flags, OSPF_LSA_SELF_CHECKED))
3372 return IS_LSA_SELF(lsa);
3373
3374 /* Make sure LSA is self-checked. */
3375 SET_FLAG(lsa->flags, OSPF_LSA_SELF_CHECKED);
3376
3377 /* AdvRouter and Router ID is the same. */
3378 if (IPV4_ADDR_SAME(&lsa->data->adv_router, &ospf->router_id))
3379 SET_FLAG(lsa->flags, OSPF_LSA_SELF);
3380
3381 /* LSA is router-LSA. */
3382 else if (lsa->data->type == OSPF_ROUTER_LSA
3383 && IPV4_ADDR_SAME(&lsa->data->id, &ospf->router_id))
3384 SET_FLAG(lsa->flags, OSPF_LSA_SELF);
3385
3386 /* LSA is network-LSA. Compare Link ID with all interfaces. */
3387 else if (lsa->data->type == OSPF_NETWORK_LSA)
3388 for (ALL_LIST_ELEMENTS_RO(ospf->oiflist, node, oi)) {
3389 /* Ignore virtual link. */
3390 if (oi->type != OSPF_IFTYPE_VIRTUALLINK)
3391 if (oi->address->family == AF_INET)
3392 if (IPV4_ADDR_SAME(
3393 &lsa->data->id,
3394 &oi->address->u.prefix4)) {
3395 /* to make it easier later */
3396 SET_FLAG(lsa->flags,
3397 OSPF_LSA_SELF);
3398 return IS_LSA_SELF(lsa);
3399 }
3400 }
3401
3402 return IS_LSA_SELF(lsa);
718e3744 3403}
3404
3405/* Get unique Link State ID. */
d62a17ae 3406struct in_addr ospf_lsa_unique_id(struct ospf *ospf, struct ospf_lsdb *lsdb,
d7c0a89a 3407 uint8_t type, struct prefix_ipv4 *p)
d62a17ae 3408{
3409 struct ospf_lsa *lsa;
3410 struct in_addr mask, id;
3411
3412 id = p->prefix;
3413
3414 /* Check existence of LSA instance. */
3415 lsa = ospf_lsdb_lookup_by_id(lsdb, type, id, ospf->router_id);
3416 if (lsa) {
3417 struct as_external_lsa *al =
3418 (struct as_external_lsa *)lsa->data;
3419 if (ip_masklen(al->mask) == p->prefixlen) {
3420 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
3421 zlog_debug(
ae32e1c2
DS
3422 "ospf_lsa_unique_id(): Can't get Link State ID for %pFX",
3423 p);
d62a17ae 3424 /* id.s_addr = 0; */
3425 id.s_addr = 0xffffffff;
3426 return id;
3427 }
3428 /* Masklen differs, then apply wildcard mask to Link State ID.
9d303b37 3429 */
d62a17ae 3430 else {
3431 masklen2ip(p->prefixlen, &mask);
3432
3433 id.s_addr = p->prefix.s_addr | (~mask.s_addr);
3434 lsa = ospf_lsdb_lookup_by_id(ospf->lsdb, type, id,
3435 ospf->router_id);
3436 if (lsa) {
3437 if (IS_DEBUG_OSPF(lsa, LSA_GENERATE))
3438 zlog_debug(
ae32e1c2
DS
3439 "ospf_lsa_unique_id(): Can't get Link State ID for %pFX",
3440 p);
d62a17ae 3441 /* id.s_addr = 0; */
3442 id.s_addr = 0xffffffff;
3443 return id;
3444 }
3445 }
3446 }
3447
3448 return id;
718e3744 3449}
3450
6b0655a2 3451
70461d79
PJ
3452#define LSA_ACTION_FLOOD_AREA 1
3453#define LSA_ACTION_FLUSH_AREA 2
718e3744 3454
d62a17ae 3455struct lsa_action {
d7c0a89a 3456 uint8_t action;
d62a17ae 3457 struct ospf_area *area;
3458 struct ospf_lsa *lsa;
718e3744 3459};
3460
d62a17ae 3461static int ospf_lsa_action(struct thread *t)
718e3744 3462{
d62a17ae 3463 struct lsa_action *data;
718e3744 3464
d62a17ae 3465 data = THREAD_ARG(t);
718e3744 3466
d62a17ae 3467 if (IS_DEBUG_OSPF(lsa, LSA) == OSPF_DEBUG_LSA)
3468 zlog_debug("LSA[Action]: Performing scheduled LSA action: %d",
3469 data->action);
718e3744 3470
d62a17ae 3471 switch (data->action) {
3472 case LSA_ACTION_FLOOD_AREA:
3473 ospf_flood_through_area(data->area, NULL, data->lsa);
3474 break;
3475 case LSA_ACTION_FLUSH_AREA:
3476 ospf_lsa_flush_area(data->lsa, data->area);
3477 break;
3478 }
718e3744 3479
d62a17ae 3480 ospf_lsa_unlock(&data->lsa); /* Message */
3481 XFREE(MTYPE_OSPF_MESSAGE, data);
3482 return 0;
718e3744 3483}
3484
d62a17ae 3485void ospf_schedule_lsa_flood_area(struct ospf_area *area, struct ospf_lsa *lsa)
718e3744 3486{
d62a17ae 3487 struct lsa_action *data;
718e3744 3488
d62a17ae 3489 data = XCALLOC(MTYPE_OSPF_MESSAGE, sizeof(struct lsa_action));
3490 data->action = LSA_ACTION_FLOOD_AREA;
3491 data->area = area;
3492 data->lsa = ospf_lsa_lock(lsa); /* Message / Flood area */
718e3744 3493
d62a17ae 3494 thread_add_event(master, ospf_lsa_action, data, 0, NULL);
718e3744 3495}
3496
d62a17ae 3497void ospf_schedule_lsa_flush_area(struct ospf_area *area, struct ospf_lsa *lsa)
718e3744 3498{
d62a17ae 3499 struct lsa_action *data;
718e3744 3500
d62a17ae 3501 data = XCALLOC(MTYPE_OSPF_MESSAGE, sizeof(struct lsa_action));
3502 data->action = LSA_ACTION_FLUSH_AREA;
3503 data->area = area;
3504 data->lsa = ospf_lsa_lock(lsa); /* Message / Flush area */
718e3744 3505
d62a17ae 3506 thread_add_event(master, ospf_lsa_action, data, 0, NULL);
718e3744 3507}
3508
6b0655a2 3509
718e3744 3510/* LSA Refreshment functions. */
d62a17ae 3511struct ospf_lsa *ospf_lsa_refresh(struct ospf *ospf, struct ospf_lsa *lsa)
3512{
3513 struct external_info *ei;
960417cf 3514 struct ospf_external_aggr_rt *aggr;
d62a17ae 3515 struct ospf_lsa *new = NULL;
960417cf 3516 struct as_external_lsa *al;
3517 struct prefix_ipv4 p;
3518
d62a17ae 3519 assert(CHECK_FLAG(lsa->flags, OSPF_LSA_SELF));
3520 assert(IS_LSA_SELF(lsa));
3521 assert(lsa->lock > 0);
3522
3523 switch (lsa->data->type) {
3524 /* Router and Network LSAs are processed differently. */
3525 case OSPF_ROUTER_LSA:
3526 new = ospf_router_lsa_refresh(lsa);
3527 break;
3528 case OSPF_NETWORK_LSA:
3529 new = ospf_network_lsa_refresh(lsa);
3530 break;
3531 case OSPF_SUMMARY_LSA:
3532 new = ospf_summary_lsa_refresh(ospf, lsa);
3533 break;
3534 case OSPF_ASBR_SUMMARY_LSA:
3535 new = ospf_summary_asbr_lsa_refresh(ospf, lsa);
3536 break;
3537 case OSPF_AS_EXTERNAL_LSA:
3538 /* Translated from NSSA Type-5s are refreshed when
3539 * from refresh of Type-7 - do not refresh these directly.
3540 */
960417cf 3541
3542 al = (struct as_external_lsa *)lsa->data;
3543 p.family = AF_INET;
3544 p.prefixlen = ip_masklen(al->mask);
3545 p.prefix = lsa->data->id;
3546
d62a17ae 3547 if (CHECK_FLAG(lsa->flags, OSPF_LSA_LOCAL_XLT))
3548 break;
b5a8894d 3549 ei = ospf_external_info_check(ospf, lsa);
d62a17ae 3550 if (ei)
63f0e941 3551 new = ospf_external_lsa_refresh(
3552 ospf, lsa, ei, LSA_REFRESH_FORCE, false);
960417cf 3553 else {
3554 aggr = (struct ospf_external_aggr_rt *)
3555 ospf_extrenal_aggregator_lookup(ospf, &p);
3556 if (aggr) {
3557 struct external_info ei_aggr;
3558
3559 memset(&ei_aggr, 0,
3560 sizeof(struct external_info));
3561 ei_aggr.p = aggr->p;
3562 ei_aggr.tag = aggr->tag;
3563 ei_aggr.instance = ospf->instance;
3564 ei_aggr.route_map_set.metric = -1;
3565 ei_aggr.route_map_set.metric_type = -1;
3566
3567 ospf_external_lsa_refresh(ospf, lsa, &ei_aggr,
3568 LSA_REFRESH_FORCE, true);
3569 } else
3570 ospf_lsa_flush_as(ospf, lsa);
3571 }
d62a17ae 3572 break;
3573 case OSPF_OPAQUE_LINK_LSA:
3574 case OSPF_OPAQUE_AREA_LSA:
3575 case OSPF_OPAQUE_AS_LSA:
3576 new = ospf_opaque_lsa_refresh(lsa);
3577 break;
3578 default:
3579 break;
3580 }
3581 return new;
718e3744 3582}
3583
d62a17ae 3584void ospf_refresher_register_lsa(struct ospf *ospf, struct ospf_lsa *lsa)
3585{
d7c0a89a 3586 uint16_t index, current_index;
d62a17ae 3587
3588 assert(lsa->lock > 0);
3589 assert(IS_LSA_SELF(lsa));
3590
3591 if (lsa->refresh_list < 0) {
3592 int delay;
3593 int min_delay =
3594 OSPF_LS_REFRESH_TIME - (2 * OSPF_LS_REFRESH_JITTER);
3595 int max_delay = OSPF_LS_REFRESH_TIME - OSPF_LS_REFRESH_JITTER;
3596
3597 /* We want to refresh the LSA within OSPF_LS_REFRESH_TIME which
3598 * is
3599 * 1800s. Use jitter so that we send the LSA sometime between
3600 * 1680s
3601 * and 1740s.
3602 */
5920b3eb
RZ
3603 delay = (frr_weak_random() % (max_delay - min_delay))
3604 + min_delay;
d62a17ae 3605
3606 current_index = ospf->lsa_refresh_queue.index
3607 + (monotime(NULL) - ospf->lsa_refresher_started)
3608 / OSPF_LSA_REFRESHER_GRANULARITY;
3609
3610 index = (current_index + delay / OSPF_LSA_REFRESHER_GRANULARITY)
3611 % (OSPF_LSA_REFRESHER_SLOTS);
3612
3613 if (IS_DEBUG_OSPF(lsa, LSA_REFRESH))
3614 zlog_debug(
96b663a3
MS
3615 "LSA[Refresh:Type%d:%pI4]: age %d, added to index %d",
3616 lsa->data->type, &lsa->data->id,
d62a17ae 3617 LS_AGE(lsa), index);
3618
3619 if (!ospf->lsa_refresh_queue.qs[index])
3620 ospf->lsa_refresh_queue.qs[index] = list_new();
3621
3622 listnode_add(ospf->lsa_refresh_queue.qs[index],
3623 ospf_lsa_lock(lsa)); /* lsa_refresh_queue */
3624 lsa->refresh_list = index;
3625
3626 if (IS_DEBUG_OSPF(lsa, LSA_REFRESH))
3627 zlog_debug(
96b663a3
MS
3628 "LSA[Refresh:Type%d:%pI4]: ospf_refresher_register_lsa(): setting refresh_list on lsa %p (slod %d)",
3629 lsa->data->type, &lsa->data->id,
d62a17ae 3630 (void *)lsa, index);
3631 }
3632}
3633
3634void ospf_refresher_unregister_lsa(struct ospf *ospf, struct ospf_lsa *lsa)
3635{
3636 assert(lsa->lock > 0);
3637 assert(IS_LSA_SELF(lsa));
3638 if (lsa->refresh_list >= 0) {
3639 struct list *refresh_list =
3640 ospf->lsa_refresh_queue.qs[lsa->refresh_list];
3641 listnode_delete(refresh_list, lsa);
3642 if (!listcount(refresh_list)) {
6a154c88 3643 list_delete(&refresh_list);
d62a17ae 3644 ospf->lsa_refresh_queue.qs[lsa->refresh_list] = NULL;
3645 }
d62a17ae 3646 lsa->refresh_list = -1;
a61b32f0 3647 ospf_lsa_unlock(&lsa); /* lsa_refresh_queue */
d62a17ae 3648 }
3649}
3650
3651int ospf_lsa_refresh_walker(struct thread *t)
3652{
3653 struct list *refresh_list;
3654 struct listnode *node, *nnode;
3655 struct ospf *ospf = THREAD_ARG(t);
3656 struct ospf_lsa *lsa;
3657 int i;
3658 struct list *lsa_to_refresh = list_new();
3659
3660 if (IS_DEBUG_OSPF(lsa, LSA_REFRESH))
3661 zlog_debug("LSA[Refresh]: ospf_lsa_refresh_walker(): start");
3662
3663
3664 i = ospf->lsa_refresh_queue.index;
3665
3666 /* Note: if clock has jumped backwards, then time change could be
3667 negative,
3668 so we are careful to cast the expression to unsigned before taking
3669 modulus. */
3670 ospf->lsa_refresh_queue.index =
3671 ((unsigned long)(ospf->lsa_refresh_queue.index
3672 + (monotime(NULL)
3673 - ospf->lsa_refresher_started)
3674 / OSPF_LSA_REFRESHER_GRANULARITY))
3675 % OSPF_LSA_REFRESHER_SLOTS;
3676
3677 if (IS_DEBUG_OSPF(lsa, LSA_REFRESH))
3678 zlog_debug(
3679 "LSA[Refresh]: ospf_lsa_refresh_walker(): next index %d",
3680 ospf->lsa_refresh_queue.index);
3681
3682 for (; i != ospf->lsa_refresh_queue.index;
3683 i = (i + 1) % OSPF_LSA_REFRESHER_SLOTS) {
3684 if (IS_DEBUG_OSPF(lsa, LSA_REFRESH))
3685 zlog_debug(
3efd0893 3686 "LSA[Refresh]: ospf_lsa_refresh_walker(): refresh index %d",
d62a17ae 3687 i);
3688
3689 refresh_list = ospf->lsa_refresh_queue.qs[i];
3690
3691 assert(i >= 0);
3692
3693 ospf->lsa_refresh_queue.qs[i] = NULL;
3694
3695 if (refresh_list) {
3696 for (ALL_LIST_ELEMENTS(refresh_list, node, nnode,
3697 lsa)) {
3698 if (IS_DEBUG_OSPF(lsa, LSA_REFRESH))
3699 zlog_debug(
96b663a3 3700 "LSA[Refresh:Type%d:%pI4]: ospf_lsa_refresh_walker(): refresh lsa %p (slot %d)",
d62a17ae 3701 lsa->data->type,
96b663a3 3702 &lsa->data->id,
d62a17ae 3703 (void *)lsa, i);
3704
3705 assert(lsa->lock > 0);
3706 list_delete_node(refresh_list, node);
3707 lsa->refresh_list = -1;
3708 listnode_add(lsa_to_refresh, lsa);
3709 }
6a154c88 3710 list_delete(&refresh_list);
d62a17ae 3711 }
3712 }
3713
3714 ospf->t_lsa_refresher = NULL;
3715 thread_add_timer(master, ospf_lsa_refresh_walker, ospf,
3716 ospf->lsa_refresh_interval, &ospf->t_lsa_refresher);
3717 ospf->lsa_refresher_started = monotime(NULL);
3718
3719 for (ALL_LIST_ELEMENTS(lsa_to_refresh, node, nnode, lsa)) {
3720 ospf_lsa_refresh(ospf, lsa);
3721 assert(lsa->lock > 0);
3722 ospf_lsa_unlock(
3723 &lsa); /* lsa_refresh_queue & temp for lsa_to_refresh*/
3724 }
3725
6a154c88 3726 list_delete(&lsa_to_refresh);
d62a17ae 3727
3728 if (IS_DEBUG_OSPF(lsa, LSA_REFRESH))
3729 zlog_debug("LSA[Refresh]: ospf_lsa_refresh_walker(): end");
3730
3731 return 0;
3732}
44445dee
S
3733
3734/* Flush the LSAs for the specific area */
3735void ospf_flush_lsa_from_area(struct ospf *ospf, struct in_addr area_id,
3736 int type)
3737{
3738 struct ospf_area *area;
3739 struct route_node *rn;
3740 struct ospf_lsa *lsa;
3741
3742 area = ospf_area_get(ospf, area_id);
3743
3744 switch (type) {
3745 case OSPF_AS_EXTERNAL_LSA:
3746 if ((area->external_routing == OSPF_AREA_NSSA) ||
3747 (area->external_routing == OSPF_AREA_STUB)) {
3748 LSDB_LOOP (EXTERNAL_LSDB(ospf), rn, lsa)
3749 if (IS_LSA_SELF(lsa) &&
3750 !(CHECK_FLAG(lsa->flags,
3751 OSPF_LSA_LOCAL_XLT)))
3752 ospf_lsa_flush_area(lsa, area);
3753 }
3754 break;
3755 case OSPF_AS_NSSA_LSA:
3756 LSDB_LOOP (NSSA_LSDB(area), rn, lsa)
3757 if (IS_LSA_SELF(lsa))
3758 ospf_lsa_flush_area(lsa, area);
3759 break;
3760 default:
3761 break;
3762 }
3763}