Coverage Report

Created: 2026-11-24 07:13

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