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