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