topology.c 53.1 KB
Newer Older
1 2 3
/*
 *	BIRD -- OSPF Topological Database
 *
Ondřej Filip's avatar
Ondřej Filip committed
4 5
 *	(c) 1999       Martin Mares <mj@ucw.cz>
 *	(c) 1999--2004 Ondrej Filip <feela@network.cz>
6 7
 *	(c) 2009--2014 Ondrej Zajicek <santiago@crfreenet.org>
 *	(c) 2009--2014 CZ.NIC z.s.p.o.
8 9 10 11 12
 *
 *	Can be freely distributed and used under the terms of the GNU GPL.
 */

#include "nest/bird.h"
13
#include "lib/string.h"
14 15 16

#include "ospf.h"

17

18
#define HASH_DEF_ORDER 6
19 20 21 22 23 24 25
#define HASH_HI_MARK *4
#define HASH_HI_STEP 2
#define HASH_HI_MAX 16
#define HASH_LO_MARK /5
#define HASH_LO_STEP 2
#define HASH_LO_MIN 8

26 27
static inline void * lsab_flush(struct ospf_proto *p);
static inline void lsab_reset(struct ospf_proto *p);
28 29


30 31 32 33
/**
 * ospf_install_lsa - install new LSA into database
 * @p: OSPF protocol instance
 * @lsa: LSA header
34
 * @type: type of LSA
35 36 37 38 39 40 41 42
 * @domain: domain of LSA
 * @body: pointer to LSA body
 *
 * This function ensures installing new LSA received in LS update into LSA
 * database. Old instance is replaced. Several actions are taken to detect if
 * new routing table calculation is necessary. This is described in 13.2 of RFC
 * 2328. This function is for received LSA only, locally originated LSAs are
 * installed by ospf_originate_lsa().
43 44 45
 *
 * The LSA body in @body is expected to be mb_allocated by the caller and its
 * ownership is transferred to the LSA entry structure.
46 47 48 49 50 51
 */
struct top_hash_entry *
ospf_install_lsa(struct ospf_proto *p, struct ospf_lsa_header *lsa, u32 type, u32 domain, void *body)
{
  struct top_hash_entry *en;
  int change = 0;
52

53
  en = ospf_hash_get(p->gr, domain, lsa->id, lsa->rt, type);
54

55 56 57 58 59 60 61 62 63 64 65
  if (!SNODE_VALID(en))
    s_add_tail(&p->lsal, SNODE en);

  if ((en->lsa_body == NULL) ||			/* No old LSA */
      (en->lsa.length != lsa->length) ||
      (en->lsa.type_raw != lsa->type_raw) ||	/* Check for OSPFv2 options */
      (en->lsa.age == LSA_MAXAGE) ||
      (lsa->age == LSA_MAXAGE) ||
      memcmp(en->lsa_body, body, lsa->length - sizeof(struct ospf_lsa_header)))
    change = 1;

66 67
  if ((en->lsa.age == LSA_MAXAGE) && (lsa->age == LSA_MAXAGE))
    change = 0;
68 69 70 71 72

  mb_free(en->lsa_body);
  en->lsa_body = body;
  en->lsa = *lsa;
  en->init_age = en->lsa.age;
73
  en->inst_time = current_time();
74

75 76 77 78 79 80 81 82 83 84
  /*
   * We do not set en->mode. It is either default LSA_M_BASIC, or in a special
   * case when en is local but flushed, there is postponed LSA, self-originated
   * LSA is received and ospf_install_lsa() is called from ospf_advance_lse(),
   * then we have en->mode from the postponed LSA origination.
   */

  OSPF_TRACE(D_EVENTS, "Installing LSA: Type: %04x, Id: %R, Rt: %R, Seq: %08x, Age: %u",
	     en->lsa_type, en->lsa.id, en->lsa.rt, en->lsa.sn, en->lsa.age);

85
  if (change)
86
    ospf_schedule_rtcalc(p);
87 88 89 90

  return en;
}

91 92 93 94 95
/**
 * ospf_advance_lsa - handle received unexpected self-originated LSA
 * @p: OSPF protocol instance
 * @en: current LSA entry or NULL
 * @lsa: new LSA header
96
 * @type: type of LSA
97 98 99 100 101 102 103 104 105 106 107
 * @domain: domain of LSA
 * @body: pointer to LSA body
 *
 * This function handles received unexpected self-originated LSA (@lsa, @body)
 * by either advancing sequence number of the local LSA instance (@en) and
 * propagating it, or installing the received LSA and immediately flushing it
 * (if there is no local LSA; i.e., @en is NULL or MaxAge).
 *
 * The LSA body in @body is expected to be mb_allocated by the caller and its
 * ownership is transferred to the LSA entry structure or it is freed.
 */
108 109 110
void
ospf_advance_lsa(struct ospf_proto *p, struct top_hash_entry *en, struct ospf_lsa_header *lsa, u32 type, u32 domain, void *body)
{
111
  /* RFC 2328 13.4 */
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130

  if (en && (en->lsa.age < LSA_MAXAGE))
  {
    if (lsa->sn != LSA_MAXSEQNO)
    {
      /*
       * We simply advance current LSA to have higher seqnum than received LSA.
       * The received LSA is ignored and the advanced LSA is propagated instead.
       *
       * Although this is an origination of distinct LSA instance and therefore
       * should be limited by MinLSInterval, we do not enforce it here. Fast
       * reaction is needed and we are already limited by MinLSArrival.
       */

      mb_free(body);

      en->lsa.sn = lsa->sn + 1;
      en->lsa.age = 0;
      en->init_age = 0;
131
      en->inst_time = current_time();
132
      lsa_generate_checksum(&en->lsa, en->lsa_body);
133 134 135

      OSPF_TRACE(D_EVENTS, "Advancing LSA: Type: %04x, Id: %R, Rt: %R, Seq: %08x",
		 en->lsa_type, en->lsa.id, en->lsa.rt, en->lsa.sn);
136 137 138
    }
    else
    {
139
      /*
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
       * Received LSA has maximal sequence number, so we cannot simply override
       * it. We have to install it to the database, immediately flush it to
       * implement sequence number wrapping, and schedule our current LSA to be
       * originated after the received instance is flushed.
       */

      if (en->next_lsa_body == NULL)
      {
	/* Schedule current LSA */
	en->next_lsa_blen = en->lsa.length - sizeof(struct ospf_lsa_header);
	en->next_lsa_body = en->lsa_body;
	en->next_lsa_opts = ospf_is_v2(p) ? lsa_get_options(&en->lsa) : 0;
      }
      else
      {
	/* There is already scheduled LSA, so we just free current one */
	mb_free(en->lsa_body);
      }

      en->lsa_body = body;
      en->lsa = *lsa;
      en->lsa.age = LSA_MAXAGE;
      en->init_age = lsa->age;
163
      en->inst_time = current_time();
164 165 166 167 168

      OSPF_TRACE(D_EVENTS, "Resetting LSA:  Type: %04x, Id: %R, Rt: %R, Seq: %08x",
		 en->lsa_type, en->lsa.id, en->lsa.rt, en->lsa.sn);
      OSPF_TRACE(D_EVENTS, "Postponing LSA: Type: %04x, Id: %R, Rt: %R",
		 en->lsa_type, en->lsa.id, en->lsa.rt);
169 170
    }
  }
171
  else
172 173 174 175 176
  {
    /*
     * We do not have received LSA in the database. We have to flush the
     * received LSA. It has to be installed in the database to secure
     * retransmissions. Note that the received LSA may already be MaxAge.
177
     * Also note that en->next_lsa_* may be defined.
178 179 180 181 182 183
     */

    lsa->age = LSA_MAXAGE;
    en = ospf_install_lsa(p, lsa, type, domain, body);
  }

184
  /*
185 186
   * We flood the updated LSA. Although in some cases the to-be-flooded LSA is
   * the same as the received LSA, and therefore we should propagate it as
187
   * regular received LSA (send the acknowledgement instead of the update to
188 189 190
   * the neighbor we received it from), we cheat a bit here.
   */

191
  ospf_flood_lsa(p, en, NULL);
192 193 194
}


195 196
static int
ospf_do_originate_lsa(struct ospf_proto *p, struct top_hash_entry *en, void *lsa_body, u16 lsa_blen, u16 lsa_opts)
197
{
198
  /* Enforce MinLSInterval */
199
  if (!en->init_age && en->inst_time && (lsa_inst_age(en) < MINLSINTERVAL))
200 201 202 203 204 205 206 207
    return 0;

  /* Handle wrapping sequence number */
  if (en->lsa.sn == LSA_MAXSEQNO)
  {
    /* Prepare to flush old LSA */
    if (en->lsa.age != LSA_MAXAGE)
    {
208 209
      OSPF_TRACE(D_EVENTS, "Resetting LSA:  Type: %04x, Id: %R, Rt: %R, Seq: %08x",
		 en->lsa_type, en->lsa.id, en->lsa.rt, en->lsa.sn);
210 211

      en->lsa.age = LSA_MAXAGE;
212
      ospf_flood_lsa(p, en, NULL);
213 214 215 216 217 218 219 220 221 222 223
      return 0;
    }

    /* Already flushing */
    if ((p->padj != 0) || (en->ret_count != 0))
      return 0;

    /* Flush done, just clean up seqnum, lsa_body is freed below */
    en->lsa.sn = LSA_ZEROSEQNO;
  }

224
  /*
225 226
   * lsa.type_raw is initialized by ospf_hash_get() to OSPFv3 LSA type.
   * lsa_set_options() implicitly converts it to OSPFv2 LSA type, assuming that
227 228
   * old type is just new type masked by 0xff. That holds for most OSPFv2 types,
   * but we have to fix it for opaque LSAs.
229
   */
230 231

  if (ospf_is_v2(p))
232 233 234 235
  {
    if (lsa_is_opaque(en->lsa_type))
      en->lsa.type_raw = LSA_T_V2_OPAQUE_ + LSA_SCOPE_ORDER(en->lsa_type);

236
    lsa_set_options(&en->lsa, lsa_opts);
237
  }
238 239 240 241 242 243 244

  mb_free(en->lsa_body);
  en->lsa_body = lsa_body;
  en->lsa.length = sizeof(struct ospf_lsa_header) + lsa_blen;
  en->lsa.sn++;
  en->lsa.age = 0;
  en->init_age = 0;
245
  en->inst_time = current_time();
246
  lsa_generate_checksum(&en->lsa, en->lsa_body);
247

248 249 250 251 252 253 254
  OSPF_TRACE(D_EVENTS, "Originating LSA: Type: %04x, Id: %R, Rt: %R, Seq: %08x",
	     en->lsa_type, en->lsa.id, en->lsa.rt, en->lsa.sn);

  ospf_flood_lsa(p, en, NULL);

  if (en->mode == LSA_M_BASIC)
    ospf_schedule_rtcalc(p);
255 256

  return 1;
257 258
}

259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
/**
 * ospf_originate_lsa - originate new LSA
 * @p: OSPF protocol instance
 * @lsa: New LSA specification
 *
 * This function prepares a new LSA, installs it into the LSA database and
 * floods it. If the new LSA cannot be originated now (because the old instance
 * was originated within MinLSInterval, or because the LSA seqnum is currently
 * wrapping), the origination is instead scheduled for later. If the new LSA is
 * equivalent to the current LSA, the origination is skipped. In all cases, the
 * corresponding LSA entry is returned. The new LSA is based on the LSA
 * specification (@lsa) and the LSA body from lsab buffer of @p, which is
 * emptied after the call. The opposite of this function is ospf_flush_lsa().
 */
struct top_hash_entry *
ospf_originate_lsa(struct ospf_proto *p, struct ospf_new_lsa *lsa)
{
  struct top_hash_entry *en;
  void *lsa_body = p->lsab;
  u16 lsa_blen = p->lsab_used;
  u16 lsa_length = sizeof(struct ospf_lsa_header) + lsa_blen;
280

281 282 283 284
  /* For OSPFv2 Opaque LSAs, LS ID consists of Opaque Type and Opaque ID */
  if (ospf_is_v2(p) && lsa_is_opaque(lsa->type))
    lsa->id |= (u32) lsa_get_opaque_type(lsa->type) << 24;

285
  en = ospf_hash_get(p->gr, lsa->dom, lsa->id, p->router_id, lsa->type);
286

287 288 289
  if (!SNODE_VALID(en))
    s_add_tail(&p->lsal, SNODE en);

290
  if (!en->nf || !en->lsa_body)
291 292 293 294
    en->nf = lsa->nf;

  if (en->nf != lsa->nf)
  {
295 296
    log(L_ERR "%s: LSA ID collision for %N",
	p->p.name, lsa->nf->fn.addr);
297 298

    en = NULL;
299 300 301
    goto drop;
  }

302 303
  if (en->mode != lsa->mode)
    en->mode = lsa->mode;
304 305 306 307

  if (en->next_lsa_body)
  {
    /* Ignore the new LSA if it is the same as the scheduled one */
308 309 310
    if ((lsa_blen == en->next_lsa_blen) &&
	!memcmp(lsa_body, en->next_lsa_body, lsa_blen) &&
	(!ospf_is_v2(p) || (lsa->opts == en->next_lsa_opts)))
311 312 313 314 315 316 317 318 319 320
      goto drop;

    /* Free scheduled LSA */
    mb_free(en->next_lsa_body);
    en->next_lsa_body = NULL;
    en->next_lsa_blen = 0;
    en->next_lsa_opts = 0;
  }

  /* Ignore the the new LSA if is the same as the current one */
321 322 323 324
  if ((en->lsa.age < LSA_MAXAGE) &&
      (lsa_length == en->lsa.length) &&
      !memcmp(lsa_body, en->lsa_body, lsa_blen) &&
      (!ospf_is_v2(p) || (lsa->opts == lsa_get_options(&en->lsa))))
325 326 327 328 329 330
    goto drop;

  lsa_body = lsab_flush(p);

  if (! ospf_do_originate_lsa(p, en, lsa_body, lsa_blen, lsa->opts))
  {
331 332 333
    OSPF_TRACE(D_EVENTS, "Postponing LSA: Type: %04x, Id: %R, Rt: %R",
	       en->lsa_type, en->lsa.id, en->lsa.rt);

334 335 336 337 338 339 340 341 342 343 344 345 346 347
    en->next_lsa_body = lsa_body;
    en->next_lsa_blen = lsa_blen;
    en->next_lsa_opts = lsa->opts;
  }

  return en;

 drop:
  lsab_reset(p);
  return en;
}

static void
ospf_originate_next_lsa(struct ospf_proto *p, struct top_hash_entry *en)
348
{
349 350 351 352
  /* Called by ospf_update_lsadb() to handle scheduled origination */

  if (! ospf_do_originate_lsa(p, en, en->next_lsa_body, en->next_lsa_blen, en->next_lsa_opts))
    return;
353

354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
  en->next_lsa_body = NULL;
  en->next_lsa_blen = 0;
  en->next_lsa_opts = 0;
}

static void
ospf_refresh_lsa(struct ospf_proto *p, struct top_hash_entry *en)
{
  /*
   * Called by ospf_update_lsadb() for periodic LSA refresh.
   *
   * We know that lsa.age < LSA_MAXAGE and lsa.rt is our router ID. We can also
   * assume that there is no scheduled LSA, because inst_time is deep in past,
   * therefore ospf_originate_next_lsa() called before would either succeed or
   * switched lsa.age to LSA_MAXAGE.
   */

371 372
  OSPF_TRACE(D_EVENTS, "Refreshing LSA: Type: %04x, Id: %R, Rt: %R, Seq: %08x",
	     en->lsa_type, en->lsa.id, en->lsa.rt, en->lsa.sn);
373 374 375 376 377 378 379 380 381 382 383 384 385

  ASSERT(en->next_lsa_body == NULL);

  /* Handle wrapping sequence number */
  if (en->lsa.sn == LSA_MAXSEQNO)
  {
    /* Copy LSA body as next LSA to get automatic origination after flush is finished */
    en->next_lsa_blen = en->lsa.length - sizeof(struct ospf_lsa_header);
    en->next_lsa_body = mb_alloc(p->p.pool, en->next_lsa_blen);
    memcpy(en->next_lsa_body, en->lsa_body, en->next_lsa_blen);
    en->next_lsa_opts = ospf_is_v2(p) ? lsa_get_options(&en->lsa) : 0;

    en->lsa.age = LSA_MAXAGE;
386
    ospf_flood_lsa(p, en, NULL);
387 388 389 390 391 392
    return;
  }

  en->lsa.sn++;
  en->lsa.age = 0;
  en->init_age = 0;
393
  en->inst_time = current_time();
394
  lsa_generate_checksum(&en->lsa, en->lsa_body);
395
  ospf_flood_lsa(p, en, NULL);
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
}

/**
 * ospf_flush_lsa - flush LSA from OSPF domain
 * @p: OSPF protocol instance
 * @en: LSA entry to flush
 *
 * This function flushes @en from the OSPF domain by setting its age to
 * %LSA_MAXAGE and flooding it. That also triggers subsequent events in LSA
 * lifecycle leading to removal of the LSA from the LSA database (e.g. the LSA
 * content is freed when flushing is acknowledged by neighbors). The function
 * does nothing if the LSA is already being flushed. LSA entries are not
 * immediately removed when being flushed, the caller may assume that @en still
 * exists after the call. The function is the opposite of ospf_originate_lsa()
 * and is supposed to do the right thing even in cases of postponed
411
 * origination.
412 413 414 415
 */
void
ospf_flush_lsa(struct ospf_proto *p, struct top_hash_entry *en)
{
416 417
  en->nf = NULL;

418 419 420 421 422 423 424 425 426 427 428
  if (en->next_lsa_body)
  {
    mb_free(en->next_lsa_body);
    en->next_lsa_body = NULL;
    en->next_lsa_blen = 0;
    en->next_lsa_opts = 0;
  }

  if (en->lsa.age == LSA_MAXAGE)
    return;

429 430 431
  OSPF_TRACE(D_EVENTS, "Flushing LSA: Type: %04x, Id: %R, Rt: %R, Seq: %08x",
	     en->lsa_type, en->lsa.id, en->lsa.rt, en->lsa.sn);

432
  en->lsa.age = LSA_MAXAGE;
433 434 435 436 437 438
  ospf_flood_lsa(p, en, NULL);

  if (en->mode == LSA_M_BASIC)
    ospf_schedule_rtcalc(p);

  en->mode = LSA_M_BASIC;
439 440 441 442 443 444 445 446
}

static void
ospf_clear_lsa(struct ospf_proto *p, struct top_hash_entry *en)
{
  /*
   * Called by ospf_update_lsadb() as part of LSA flushing process.
   * Flushed LSA was acknowledged by neighbors and we can free its content.
447
   * The log message is for 'remove' - we hide empty LSAs from users.
448 449
   */

450 451 452
  OSPF_TRACE(D_EVENTS, "Removing LSA: Type: %04x, Id: %R, Rt: %R, Seq: %08x",
	     en->lsa_type, en->lsa.id, en->lsa.rt, en->lsa.sn);

453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
  if (en->lsa.sn == LSA_MAXSEQNO)
    en->lsa.sn = LSA_ZEROSEQNO;

  mb_free(en->lsa_body);
  en->lsa_body = NULL;
}

static void
ospf_remove_lsa(struct ospf_proto *p, struct top_hash_entry *en)
{
  /*
   * Called by ospf_update_lsadb() as part of LSA flushing process.
   * Both lsa_body and next_lsa_body are NULL.
   */

  s_rem_node(SNODE en);
  ospf_hash_delete(p->gr, en);
}

/**
 * ospf_update_lsadb - update LSA database
 * @p: OSPF protocol instance
 *
 * This function is periodicaly invoked from ospf_disp(). It does some periodic
 * or postponed processing related to LSA entries. It originates postponed LSAs
 * scheduled by ospf_originate_lsa(), It continues in flushing processes started
 * by ospf_flush_lsa(). It also periodically refreshs locally originated LSAs --
 * when the current instance is older %LSREFRESHTIME, a new instance is originated.
 * Finally, it also ages stored LSAs and flushes ones that reached %LSA_MAXAGE.
 *
 * The RFC 2328 says that a router should periodically check checksums of all
 * stored LSAs to detect hardware problems. This is not implemented.
 */
void
ospf_update_lsadb(struct ospf_proto *p)
{
  struct top_hash_entry *en, *nxt;
490 491
  btime now_ = current_time();
  int real_age;
492 493 494 495 496 497

  WALK_SLIST_DELSAFE(en, nxt, p->lsal)
  {
    if (en->next_lsa_body)
      ospf_originate_next_lsa(p, en);

498
    real_age = en->init_age + (now_ - en->inst_time) TO_S;
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518

    if (en->lsa.age == LSA_MAXAGE)
    {
      if (en->lsa_body && (p->padj == 0) && (en->ret_count == 0))
	ospf_clear_lsa(p, en);

      if ((en->lsa_body == NULL) && (en->next_lsa_body == NULL) &&
	  ((en->lsa.rt != p->router_id) || (real_age >= LSA_MAXAGE)))
	ospf_remove_lsa(p, en);

      continue;
    }

    if ((en->lsa.rt == p->router_id) && (real_age >= LSREFRESHTIME))
    {
      ospf_refresh_lsa(p, en);
      continue;
    }

    if (real_age >= LSA_MAXAGE)
519
    {
520 521
      ospf_flush_lsa(p, en);
      continue;
522
    }
523 524 525 526 527 528

    en->lsa.age = real_age;
  }
}


529
static u32
530 531 532 533 534
ort_to_lsaid(struct ospf_proto *p, ort *nf)
{
  /*
   * In OSPFv2, We have to map IP prefixes to u32 in such manner that resulting
   * u32 interpreted as IP address is a member of given prefix. Therefore, /32
535 536
   * prefix has to be mapped on itself.  All received prefixes have to be mapped
   * on different u32s.
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
   *
   * We have an assumption that if there is nontrivial (non-/32) network prefix,
   * then there is not /32 prefix for the first and the last IP address of the
   * network (these are usually reserved, therefore it is not an important
   * restriction).  The network prefix is mapped to the first or the last IP
   * address in the manner that disallow collisions - we use the IP address that
   * cannot be used by the parent prefix.
   *
   * For example:
   * 192.168.0.0/24 maps to 192.168.0.255
   * 192.168.1.0/24 maps to 192.168.1.0
   * because 192.168.0.0 and 192.168.1.255 might be used by 192.168.0.0/23 .
   *
   * Appendig E of RFC 2328 suggests different algorithm, that tries to maximize
   * both compatibility and subnetting. But as it is not possible to have both
   * reliably and the suggested algorithm was unnecessary complicated and it
   * does crazy things like changing LSA ID for a network because different
   * network appeared, we choose a different way.
   *
   * In OSPFv3, it is simpler. There is not a requirement for membership of the
557 558
   * result in the input network, so we just allocate a unique ID from ID map
   * and store it in nf->lsa_id for further reference.
559 560 561
   */

  if (ospf_is_v3(p))
562 563 564 565 566 567
  {
    if (!nf->lsa_id)
      nf->lsa_id = idm_alloc(&p->idm);

    return nf->lsa_id;
  }
568

569 570 571
  net_addr_ip4 *net = (void *) nf->fn.addr;
  u32 id = ip4_to_u32(net->prefix);
  int pxlen = net->pxlen;
572 573 574 575 576 577 578 579 580 581 582 583

  if ((pxlen == 0) || (pxlen == 32))
    return id;

  if (id & (1 << (32 - pxlen)))
    return id;
  else
    return id | ~u32_mkmask(pxlen);
}


static void *
584
lsab_alloc(struct ospf_proto *p, uint size)
585
{
586
  uint offset = p->lsab_used;
587 588 589 590 591 592 593 594
  p->lsab_used += size;
  if (p->lsab_used > p->lsab_size)
  {
    p->lsab_size = MAX(p->lsab_used, 2 * p->lsab_size);
    p->lsab = p->lsab ? mb_realloc(p->lsab, p->lsab_size):
      mb_alloc(p->p.pool, p->lsab_size);
  }
  return ((byte *) p->lsab) + offset;
595 596 597
}

static inline void *
598
lsab_allocz(struct ospf_proto *p, uint size)
599
{
600
  void *r = lsab_alloc(p, size);
601 602 603 604 605
  bzero(r, size);
  return r;
}

static inline void *
606
lsab_flush(struct ospf_proto *p)
607
{
608 609 610
  void *r = mb_alloc(p->p.pool, p->lsab_used);
  memcpy(r, p->lsab, p->lsab_used);
  p->lsab_used = 0;
611 612 613
  return r;
}

614 615
static inline void
lsab_reset(struct ospf_proto *p)
616
{
617
  p->lsab_used = 0;
618 619 620
}

static inline void *
621
lsab_offset(struct ospf_proto *p, uint offset)
622
{
623
  return ((byte *) p->lsab) + offset;
624 625
}

626
static inline void * UNUSED
627
lsab_end(struct ospf_proto *p)
628
{
629
  return ((byte *) p->lsab) + p->lsab_used;
630 631
}

632

633 634 635 636 637
/*
 *	Router-LSA handling
 *	Type = LSA_T_RT
 */

638 639 640
static int
configured_stubnet(struct ospf_area *oa, struct ifa *a)
{
641
  /* Does not work for IA_PEER addresses, but it is not called on these */
642 643
  struct ospf_stubnet_config *sn;
  WALK_LIST(sn, oa->ac->stubnet_list)
644 645
  {
    if (sn->summary)
646
    {
647
      if (net_in_netX(&a->prefix, &sn->prefix))
648
	return 1;
649
    }
650 651
    else
    {
652
      if (net_equal(&a->prefix, &sn->prefix))
653 654 655 656
	return 1;
    }
  }

657 658
  return 0;
}
659

660
static int
661 662 663 664 665 666 667 668
bcast_net_active(struct ospf_iface *ifa)
{
  struct ospf_neighbor *neigh;

  if (ifa->state == OSPF_IS_WAITING)
    return 0;

  WALK_LIST(neigh, ifa->neigh_list)
669 670
  {
    if (neigh->state == NEIGHBOR_FULL)
671
    {
672 673
      if (neigh->rid == ifa->drid)
	return 1;
674

675 676
      if (ifa->state == OSPF_IS_DR)
	return 1;
677
    }
678
  }
679 680 681 682

  return 0;
}

683 684 685 686
static inline u32
get_rt_options(struct ospf_proto *p, struct ospf_area *oa, int bitv)
{
  u32 opts = 0;
687

688 689
  if (p->areano > 1)
    opts |= OPT_RT_B;
690

691 692
  if ((p->areano > 1) && oa_is_nssa(oa) && oa->ac->translator)
    opts |= OPT_RT_NT;
693

694 695
  if (p->asbr && !oa_is_stub(oa))
    opts |= OPT_RT_E;
696

697 698
  if (bitv)
    opts |= OPT_RT_V;
699

700 701
  return opts;
}
702

703 704 705 706 707 708 709 710 711 712
static inline void
add_rt2_lsa_link(struct ospf_proto *p, u8 type, u32 id, u32 data, u16 metric)
{
  struct ospf_lsa_rt2_link *ln = lsab_alloc(p, sizeof(struct ospf_lsa_rt2_link));
  ln->type = type;
  ln->id = id;
  ln->data = data;
  ln->metric = metric;
  ln->no_tos = 0;
}
713

714 715 716 717 718 719
static void
prepare_rt2_lsa_body(struct ospf_proto *p, struct ospf_area *oa)
{
  struct ospf_iface *ifa;
  int i = 0, bitv = 0;
  struct ospf_neighbor *neigh;
720

721 722 723
  ASSERT(p->lsab_used == 0);
  lsab_allocz(p, sizeof(struct ospf_lsa_rt));
  /* ospf_lsa_rt header will be filled later */
Ondřej Filip's avatar
Ondřej Filip committed
724

725
  WALK_LIST(ifa, p->iface_list)
726
  {
727
    int net_lsa = 0;
728
    u32 link_cost = p->stub_router ? 0xffff : ifa->cost;
729 730 731

    if ((ifa->type == OSPF_IT_VLINK) && (ifa->voa == oa) &&
	(!EMPTY_LIST(ifa->neigh_list)))
732 733 734
    {
      neigh = (struct ospf_neighbor *) HEAD(ifa->neigh_list);
      if ((neigh->state == NEIGHBOR_FULL) && (ifa->cost <= 0xffff))
735
	bitv = 1;
736
    }
737 738

    if ((ifa->oa != oa) || (ifa->state == OSPF_IS_DOWN))
Ondřej Filip's avatar
Ondřej Filip committed
739
      continue;
740

741 742
    ifa->rt_pos_beg = i;

743
    /* RFC 2328 - 12.4.1.1-4 */
744
    switch (ifa->type)
745 746 747 748 749
    {
    case OSPF_IT_PTP:
    case OSPF_IT_PTMP:
      WALK_LIST(neigh, ifa->neigh_list)
	if (neigh->state == NEIGHBOR_FULL)
Ondřej Filip's avatar
Ondřej Filip committed
750
	{
751 752 753 754 755 756 757 758 759
	  /*
	   * ln->data should be ifa->iface_id in case of no/ptp
	   * address (ifa->addr->flags & IA_PEER) on PTP link (see
	   * RFC 2328 12.4.1.1.), but the iface ID value has no use,
	   * while using IP address even in this case is here for
	   * compatibility with some broken implementations that use
	   * this address as a next-hop.
	   */
	  add_rt2_lsa_link(p, LSART_PTP, neigh->rid, ipa_to_u32(ifa->addr->ip), link_cost);
760
	  i++;
761 762
	}
      break;
763

764 765 766 767 768 769 770
    case OSPF_IT_BCAST:
    case OSPF_IT_NBMA:
      if (bcast_net_active(ifa))
      {
	add_rt2_lsa_link(p, LSART_NET, ipa_to_u32(ifa->drip), ipa_to_u32(ifa->addr->ip), link_cost);
	i++;
	net_lsa = 1;
771
      }
772 773 774 775 776 777 778 779 780
      break;

    case OSPF_IT_VLINK:
      neigh = (struct ospf_neighbor *) HEAD(ifa->neigh_list);
      if ((!EMPTY_LIST(ifa->neigh_list)) && (neigh->state == NEIGHBOR_FULL) && (ifa->cost <= 0xffff))
	add_rt2_lsa_link(p, LSART_VLNK, neigh->rid, ipa_to_u32(ifa->addr->ip), link_cost), i++;
      break;

    default:
781
      log(L_BUG "OSPF: Unknown interface type");
782 783
      break;
    }
784

785 786
    ifa->rt_pos_end = i;

787 788 789
    /* Now we will originate stub area if there is no primary */
    if (net_lsa ||
	(ifa->type == OSPF_IT_VLINK) ||
790
	((ifa->addr->flags & IA_PEER) && ! ifa->cf->stub) ||
791 792
	configured_stubnet(oa, ifa->addr))
      continue;
793

794
      /* Host or network stub entry */
795 796 797
    if ((ifa->addr->flags & IA_HOST) ||
	(ifa->state == OSPF_IS_LOOP) ||
	(ifa->type == OSPF_IT_PTMP))
798
      add_rt2_lsa_link(p, LSART_STUB, ipa_to_u32(ifa->addr->ip), 0xffffffff, 0);
799
    else
800 801
      add_rt2_lsa_link(p, LSART_STUB, ip4_to_u32(net4_prefix(&ifa->addr->prefix)),
		       u32_mkmask(net4_pxlen(&ifa->addr->prefix)), ifa->cost);
802
    i++;
803 804

    ifa->rt_pos_end = i;
805
  }
806

807
  struct ospf_stubnet_config *sn;
808 809
  WALK_LIST(sn, oa->ac->stubnet_list)
    if (!sn->hidden)
810 811
      add_rt2_lsa_link(p, LSART_STUB, ip4_to_u32(net4_prefix(&sn->prefix)),
		       u32_mkmask(net4_pxlen(&sn->prefix)), sn->cost), i++;
812

813
  struct ospf_lsa_rt *rt = p->lsab;
814
  /* Store number of links in lower half of options */
815
  rt->options = get_rt_options(p, oa, bitv) | (u16) i;
816
}
817

818 819
static inline void
add_rt3_lsa_link(struct ospf_proto *p, u8 type, struct ospf_iface *ifa, u32 nif, u32 id)
820
{
821
  struct ospf_lsa_rt3_link *ln = lsab_alloc(p, sizeof(struct ospf_lsa_rt3_link));
822 823 824
  ln->type = type;
  ln->padding = 0;
  ln->metric = ifa->cost;
825
  ln->lif = ifa->iface_id;
826 827 828 829
  ln->nif = nif;
  ln->id = id;
}

830 831
static void
prepare_rt3_lsa_body(struct ospf_proto *p, struct ospf_area *oa)
832 833
{
  struct ospf_iface *ifa;
834
  struct ospf_neighbor *neigh;
835
  int bitv = 0;
836
  int i = 0;
837

838 839 840
  ASSERT(p->lsab_used == 0);
  lsab_allocz(p, sizeof(struct ospf_lsa_rt));
  /* ospf_lsa_rt header will be filled later */
841

842
  WALK_LIST(ifa, p->iface_list)
843 844 845 846 847 848 849 850 851 852 853 854
  {
    if ((ifa->type == OSPF_IT_VLINK) && (ifa->voa == oa) &&
	(!EMPTY_LIST(ifa->neigh_list)))
    {
      neigh = (struct ospf_neighbor *) HEAD(ifa->neigh_list);
      if ((neigh->state == NEIGHBOR_FULL) && (ifa->cost <= 0xffff))
	bitv = 1;
    }

    if ((ifa->oa != oa) || (ifa->state == OSPF_IS_DOWN))
      continue;

855 856
    ifa->rt_pos_beg = i;

857
    /* RFC 5340 - 4.4.3.2 */
858
    switch (ifa->type)
859 860 861 862 863 864 865
    {
    case OSPF_IT_PTP:
    case OSPF_IT_PTMP:
      WALK_LIST(neigh, ifa->neigh_list)
	if (neigh->state == NEIGHBOR_FULL)
	  add_rt3_lsa_link(p, LSART_PTP, ifa, neigh->iface_id, neigh->rid), i++;
      break;
866

867 868 869 870 871
    case OSPF_IT_BCAST:
    case OSPF_IT_NBMA:
      if (bcast_net_active(ifa))
	add_rt3_lsa_link(p, LSART_NET, ifa, ifa->dr_iface_id, ifa->drid), i++;
      break;
872

873 874 875 876 877 878 879
    case OSPF_IT_VLINK:
      neigh = (struct ospf_neighbor *) HEAD(ifa->neigh_list);
      if ((!EMPTY_LIST(ifa->neigh_list)) && (neigh->state == NEIGHBOR_FULL) && (ifa->cost <= 0xffff))
	add_rt3_lsa_link(p, LSART_VLNK, ifa, neigh->iface_id, neigh->rid), i++;
      break;

    default:
880
      log(L_BUG "OSPF: Unknown interface type");
881
      break;
882 883
    }

884 885
    ifa->rt_pos_end = i;
  }
886

887 888 889
  struct ospf_lsa_rt *rt = p->lsab;
  rt->options = get_rt_options(p, oa, bitv) | (oa->options & LSA_OPTIONS_MASK);
}
890

891 892
static void
ospf_originate_rt_lsa(struct ospf_proto *p, struct ospf_area *oa)
893
{
894 895 896 897 898 899
  struct ospf_new_lsa lsa = {
    .type = LSA_T_RT,
    .dom  = oa->areaid,
    .id   = ospf_is_v2(p) ? p->router_id : 0,
    .opts = oa->options
  };
900

901 902
  OSPF_TRACE(D_EVENTS, "Updating router state for area %R", oa->areaid);

903 904 905 906
  if (ospf_is_v2(p))
    prepare_rt2_lsa_body(p, oa);
  else
    prepare_rt3_lsa_body(p, oa);
Ondřej Filip's avatar
Ondřej Filip committed
907

908 909
  oa->rt = ospf_originate_lsa(p, &lsa);
}
910

911

912 913 914 915
/*
 *	Net-LSA handling
 *	Type = LSA_T_NET
 */
916

917 918
static void
prepare_net2_lsa_body(struct ospf_proto *p, struct ospf_iface *ifa)
919
{
920 921 922 923
  struct ospf_lsa_net *net;
  struct ospf_neighbor *n;
  int nodes = ifa->fadj + 1;
  u16 i = 1;
924

925 926
  ASSERT(p->lsab_used == 0);
  net = lsab_alloc(p, sizeof(struct ospf_lsa_net) + 4 * nodes);
927

928
  net->optx = u32_mkmask(ifa->addr->prefix.pxlen);
929
  net->routers[0] = p->router_id;
930

931 932 933 934 935 936 937 938 939
  WALK_LIST(n, ifa->neigh_list)
  {
    if (n->state == NEIGHBOR_FULL)
    {
      net->routers[i] = n->rid;
      i++;
    }
  }
  ASSERT(i == nodes);
940 941
}

942 943
static void
prepare_net3_lsa_body(struct ospf_proto *p, struct ospf_iface *ifa)
944
{
945
  struct ospf_lsa_net *net;
946 947
  int nodes = ifa->fadj + 1;
  u32 options = 0;
948 949 950 951
  u16 i = 1;

  ASSERT(p->lsab_used == 0);
  net = lsab_alloc(p, sizeof(struct ospf_lsa_net) + 4 * nodes);
952

953
  net->routers[0] = p->router_id;
954

955
  struct ospf_neighbor *n;
Ondřej Filip's avatar
Ondřej Filip committed
956
  WALK_LIST(n, ifa->neigh_list)
957
  {
Ondřej Filip's avatar
Ondřej Filip committed
958
    if (n->state == NEIGHBOR_FULL)
959
    {
960 961 962 963 964
      /* In OSPFv3, we would like to merge options from Link LSAs of added neighbors */

      struct top_hash_entry *en =
	ospf_hash_find(p->gr, ifa->iface_id, n->iface_id, n->rid, LSA_T_LINK);

965 966 967 968
      if (en)
	options |= ((struct ospf_lsa_link *) en->lsa_body)->options;

      net->routers[i] = n->rid;
969 970 971
      i++;
    }
  }
972 973
  ASSERT(i == nodes);

974
  net->optx = options & LSA_OPTIONS_MASK;
975 976
}

977 978 979 980 981 982 983 984 985 986 987
static void
ospf_originate_net_lsa(struct ospf_proto *p, struct ospf_iface *ifa)
{
  struct ospf_new_lsa lsa = {
    .type = LSA_T_NET,
    .dom  = ifa->oa->areaid,
    .id   = ospf_is_v2(p) ? ipa_to_u32(ifa->addr->ip) : ifa->iface_id,
    .opts = ifa->oa->options,
    .ifa  = ifa
  };

988 989
  OSPF_TRACE(D_EVENTS, "Updating network state for %s (Id: %R)", ifa->ifname, lsa.id);

990 991
  if (ospf_is_v2(p))
    prepare_net2_lsa_body(p, ifa);
992
  else
993
    prepare_net3_lsa_body(p, ifa);
994

995
  ifa->net_lsa = ospf_originate_lsa(p, &lsa);
996 997
}

998

999 1000 1001 1002 1003
/*
 *	(Net|Rt)-summary-LSA handling
 *	(a.k.a. Inter-Area-(Prefix|Router)-LSA)
 *	Type = LSA_T_SUM_NET, LSA_T_SUM_RT
 */
1004

1005 1006
static inline void
prepare_sum2_lsa_body(struct ospf_proto *p, uint pxlen, u32 metric)
1007
{
1008
  struct ospf_lsa_sum2 *sum;
1009

1010 1011
  sum = lsab_allocz(p, sizeof(struct ospf_lsa_sum2));
  sum->netmask = u32_mkmask(pxlen);
1012 1013 1014
  sum->metric = metric;
}

1015 1016
static inline void
prepare_sum3_net_lsa_body(struct ospf_proto *p, ort *nf, u32 metric)
1017
{
1018
  struct ospf_lsa_sum3_net *sum;
1019

1020 1021
  sum = lsab_allocz(p, sizeof(struct ospf_lsa_sum3_net) +
		    IPV6_PREFIX_SPACE(nf->fn.addr->pxlen));
1022
  sum->metric = metric;
1023
  ospf3_put_prefix(sum->prefix, nf->fn.addr, 0, 0);
1024 1025
}

1026 1027
static inline void
prepare_sum3_rt_lsa_body(struct ospf_proto *p, u32 drid, u32 metric, u32 options)
1028
{
1029
  struct ospf_lsa_sum3_rt *sum;
1030

1031
  sum = lsab_allocz(p, sizeof(struct ospf_lsa_sum3_rt));
1032
  sum->options = options;
1033 1034
  sum->metric = metric;
  sum->drid = drid;
1035 1036
}

Ondřej Filip's avatar
Ondřej Filip committed
1037
void
1038
ospf_originate_sum_net_lsa(struct ospf_proto *p, struct ospf_area *oa, ort *nf, int metric)
Ondřej Filip's avatar
Ondřej Filip committed
1039
{
1040 1041
  struct ospf_new_lsa lsa = {
    .type = LSA_T_SUM_NET,
1042
    .mode = LSA_M_RTCALC,
1043 1044 1045 1046 1047
    .dom  = oa->areaid,
    .id   = ort_to_lsaid(p, nf),
    .opts = oa->options,
    .nf   = nf
  };
1048

1049
  if (ospf_is_v2(p))
1050
    prepare_sum2_lsa_body(p, nf->fn.addr->pxlen, metric);
1051 1052
  else
    prepare_sum3_net_lsa_body(p, nf, metric);
1053

1054
  ospf_originate_lsa(p, &lsa);
1055
}
1056

1057
void
1058
ospf_originate_sum_rt_lsa(struct ospf_proto *p, struct ospf_area *oa, u32 drid, int metric, u32 options)
1059
{
1060 1061
  struct ospf_new_lsa lsa = {
    .type = LSA_T_SUM_RT,
1062
    .mode = LSA_M_RTCALC,
1063
    .dom  = oa->areaid,
1064
    .id   = drid,	/* Router ID of ASBR, irrelevant for OSPFv3 */
1065 1066
    .opts = oa->options
  };
Ondřej Filip's avatar
Ondřej Filip committed
1067

1068 1069
  if (ospf_is_v2(p))
    prepare_sum2_lsa_body(p, 0, metric);
1070
  else
1071
    prepare_sum3_rt_lsa_body(p, drid, metric, options & LSA_OPTIONS_MASK);
1072

1073
  ospf_originate_lsa(p, &lsa);
Ondřej Filip's avatar
Ondřej Filip committed
1074 1075
}

1076

1077
/*
1078 1079
 *	AS-external-LSA and NSSA-LSA handling
 *	Type = LSA_T_EXT, LSA_T_NSSA
1080
 */
1081

1082 1083 1084
static inline void
prepare_ext2_lsa_body(struct ospf_proto *p, uint pxlen,
		      u32 metric, u32 ebit, ip_addr fwaddr, u32 tag)
1085
{
1086
  struct ospf_lsa_ext2 *ext;
1087

1088 1089 1090 1091 1092
  ext = lsab_allocz(p, sizeof(struct ospf_lsa_ext2));
  ext->metric = metric & LSA_METRIC_MASK;
  ext->netmask = u32_mkmask(pxlen);
  ext->fwaddr = ipa_to_u32(fwaddr);
  ext->tag = tag;
1093

1094 1095
  if (ebit)
    ext->metric |= LSA_EXT2_EBIT;
1096
}
1097

1098 1099 1100
static inline void
prepare_ext3_lsa_body(struct ospf_proto *p, ort *nf,
		      u32 metric, u32 ebit, ip_addr fwaddr, u32 tag, int pbit)
1101
{
1102 1103
  struct ospf_lsa_ext3 *ext;
  int bsize = sizeof(struct ospf_lsa_ext3)
1104
    + IPV6_PREFIX_SPACE(nf->fn.addr->pxlen)
1105 1106 1107
    + (ipa_nonzero(fwaddr) ? 16 : 0)
    + (tag ? 4 : 0);

1108 1109 1110
  ext = lsab_allocz(p, bsize);
  ext->metric = metric & LSA_METRIC_MASK;
  u32 *buf = ext->rest;
1111

1112
  buf = ospf3_put_prefix(buf, nf->fn.addr, pbit ? OPT_PX_P : 0, 0);
1113

1114 1115
  if (ebit)
    ext->metric |= LSA_EXT3_EBIT;
1116

1117 1118
  if (ipa_nonzero(fwaddr))
  {
1119
    ext->metric |= LSA_EXT3_FBIT;
1120
    buf = ospf3_put_addr(buf, fwaddr);
1121
  }
1122 1123

  if (tag)
1124
  {
1125
    ext->metric |= LSA_EXT3_TBIT;
1126 1127
    *buf++ = tag;
  }
1128 1129
}

1130 1131
/**
 * originate_ext_lsa - new route received from nest and filters
1132
 * @p: OSPF protocol instance
1133 1134
 * @oa: ospf_area for which LSA is originated
 * @nf: network prefix and mask
1135 1136 1137
 * @mode: the mode of the LSA (LSA_M_EXPORT or LSA_M_RTCALC)
 * @metric: the metric of a route
 * @ebit: E-bit for route metric (bool)
1138 1139
 * @fwaddr: the forwarding address
 * @tag: the route tag
1140
 * @pbit: P-bit for NSSA LSAs (bool), ignored for external LSAs
1141 1142 1143 1144
 *
 * If I receive a message that new route is installed, I try to originate an
 * external LSA. If @oa is an NSSA area, NSSA-LSA is originated instead.
 * @oa should not be a stub area. @src does not specify whether the LSA
1145
 * is external or NSSA, but it specifies the source of origination -
1146 1147 1148
 * the export from ospf_rt_notify(), or the NSSA-EXT translation.
 */
void
1149
ospf_originate_ext_lsa(struct ospf_proto *p, struct ospf_area *oa, ort *nf, u8 mode,
1150
		       u32 metric, u32 ebit, ip_addr fwaddr, u32 tag, int pbit)
1151
{
1152 1153
  struct ospf_new_lsa lsa = {
    .type = oa ? LSA_T_NSSA : LSA_T_EXT,
1154
    .mode = mode, /* LSA_M_EXPORT or LSA_M_RTCALC */
1155 1156 1157 1158 1159
    .dom  = oa ? oa->areaid : 0,
    .id   = ort_to_lsaid(p, nf),
    .opts = oa ? (pbit ? OPT_P : 0) : OPT_E,
    .nf   = nf
  };
1160

1161
  if (ospf_is_v2(p))
1162
    prepare_ext2_lsa_body(p, nf->fn.addr->pxlen, metric, ebit, fwaddr, tag);
1163 1164
  else
    prepare_ext3_lsa_body(p, nf, metric, ebit, fwaddr, tag, oa && pbit);
1165

1166
  ospf_originate_lsa(p, &lsa);
1167 1168
}

1169 1170 1171
static struct top_hash_entry *
ospf_hash_find_(struct top_graph *f, u32 domain, u32 lsa, u32 rtr, u32 type);

1172 1173 1174 1175
static void
ospf_flush_ext_lsa(struct ospf_proto *p, struct ospf_area *oa, ort *nf)
{
  struct top_hash_entry *en;
1176

1177 1178 1179
  u32 type = oa ? LSA_T_NSSA : LSA_T_EXT;
  u32 dom = oa ? oa->areaid : 0;
  u32 id = ort_to_lsaid(p, nf);
1180

1181
  en = ospf_hash_find_(p->gr, dom, id, p->router_id, type);
1182

1183 1184
  if (!en || (en->nf != nf))
    return;
1185

1186
  ospf_flush_lsa(p, en);
1187 1188
}

1189 1190 1191 1192 1193 1194 1195
static inline int
use_gw_for_fwaddr(struct ospf_proto *p, ip_addr gw, struct iface *iface)
{
  struct ospf_iface *ifa;

  if (ipa_zero(gw) || ipa_is_link_local(gw))
    return 0;
1196

1197 1198
  WALK_LIST(ifa, p->iface_list)
    if ((ifa->iface == iface) &&
1199
	(!ospf_is_v2(p) || ipa_in_netX(gw, &ifa->addr->prefix)))
1200 1201 1202 1203
      return 1;

  return 0;
}
1204

1205
static inline ip_addr
1206
find_surrogate_fwaddr(struct ospf_proto *p, struct ospf_area *oa)
1207 1208 1209 1210 1211
{
  struct ospf_iface *ifa;
  struct ifa *a, *cur_addr = NULL;
  int np, cur_np = 0;

1212 1213 1214
  /* RFC 3101 2.3 - surrogate forwarding address selection */

  WALK_LIST(ifa, p->iface_list)
1215 1216 1217 1218 1219
  {
    if ((ifa->oa != oa) ||
	(ifa->type == OSPF_IT_VLINK))
      continue;

1220
    if (ospf_is_v2(p))
1221
    {
1222 1223
      a = ifa->addr;
      if (a->flags & IA_PEER)
1224 1225
	continue;

1226
      np = (a->flags & IA_HOST) ? 3 : (ifa->stub ? 2 : 1);
1227 1228 1229 1230 1231 1232
      if (np > cur_np)
      {
	cur_addr = a;
	cur_np = np;
      }
    }
1233 1234 1235 1236
    else /* OSPFv3 */
    {
      WALK_LIST(a, ifa->iface->addrs)
      {
1237
	if ((a->prefix.type != ospf_get_af(p)) ||
1238
	    (a->flags & IA_SECONDARY) ||
1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250
	    (a->flags & IA_PEER) ||
	    (a->scope <= SCOPE_LINK))
	  continue;

	np = (a->flags & IA_HOST) ? 3 : (ifa->stub ? 2 : 1);
	if (np > cur_np)
	{
	  cur_addr = a;
	  cur_np = np;
	}
      }
    }
1251 1252 1253 1254 1255
  }

  return cur_addr ? cur_addr->ip : IPA_NONE;
}

1256
void
1257
ospf_rt_notify(struct proto *P, struct channel *ch UNUSED, net *n, rte *new, rte *old UNUSED)
1258
{
1259 1260 1261
  struct ospf_proto *p = (struct ospf_proto *) P;
  struct ospf_area *oa = NULL;	/* non-NULL for NSSA-LSA */
  ort *nf;
1262

1263 1264 1265 1266 1267 1268 1269
  /*
   * There are several posibilities:
   * 1) router in regular area - originate external LSA with global scope
   * 2) router in NSSA area - originate area-specific NSSA-LSA
   * 3) router in stub area - cannot export routes
   * 4) area border router - same as (1), it is attached to backbone
   */
Ondřej Filip's avatar
Ondřej Filip committed
1270

1271 1272
  if ((p->areano == 1) && oa_is_nssa(HEAD(p->area_list)))
    oa = HEAD(p->area_list);
1273

1274
  if (!new)
1275
  {
1276
    nf = fib_find(&p->rtf, n->n.addr);
1277

1278
    if (!nf || !nf->external_rte)
1279
      return;
1280

1281 1282
    ospf_flush_ext_lsa(p, oa, nf);
    nf->external_rte = 0;
1283

1284 1285
    /* Old external route might blocked some NSSA translation */
    if ((p->areano > 1) && rt_is_nssa(nf) && nf->n.oa->translate)
1286
      ospf_schedule_rtcalc(p);
1287

1288 1289
    return;
  }
1290

1291
  ASSERT(p->asbr);
1292

1293 1294
  /* Get route attributes */
  rta *a = new->attrs;
1295 1296
  eattr *m1a = ea_find(a->eattrs, EA_OSPF_METRIC1);
  eattr *m2a = ea_find(a->eattrs, EA_OSPF_METRIC2);
1297 1298
  uint m1 = m1a ? m1a->u.data : 0;
  uint m2 = m2a ? m2a->u.data : 10000;
1299

1300 1301 1302 1303 1304 1305
  if (m1 > LSINFINITY)
  {
    log(L_WARN "%s: Invalid ospf_metric1 value %u for route %N",
	p->p.name, m1, n->n.addr);
    m1 = LSINFINITY;
  }
1306

1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319
  if (m2 > LSINFINITY)
  {
    log(L_WARN "%s: Invalid ospf_metric2 value %u for route %N",
	p->p.name, m2, n->n.addr);
    m2 = LSINFINITY;
  }

  /* Ensure monotonicity of metric if both m1 and m2 are used */
  if ((m1 > 0) && (m2 < LSINFINITY))
    m2++;

  uint ebit = m2a || !m1a;
  uint metric = ebit ? m2 : m1;
1320
  uint tag = ea_get_int(a->eattrs, EA_OSPF_TAG, 0);
1321 1322

  ip_addr fwd = IPA_NONE;
1323 1324
  if ((a->dest == RTD_UNICAST) && use_gw_for_fwaddr(p, a->nh.gw, a->nh.iface))
    fwd = a->nh.gw;
1325

1326 1327 1328 1329
  /* NSSA-LSA with P-bit set must have non-zero forwarding address */
  if (oa && ipa_zero(fwd))
  {
    fwd = find_surrogate_fwaddr(p, oa);
1330

1331
    if (ipa_zero(fwd))