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