topology.c 52.2 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 227 228
   * 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
   * old type is just new type masked by 0xff.  That is not universally true,
   * but it holds for all OSPFv2 types currently supported by BIRD.
229
   */
230 231 232 233 234 235 236 237 238 239

  if (ospf_is_v2(p))
    lsa_set_options(&en->lsa, lsa_opts);

  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;
240
  en->inst_time = current_time();
241
  lsa_generate_checksum(&en->lsa, en->lsa_body);
242

243 244 245 246 247 248 249
  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);
250 251

  return 1;
252 253
}

254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
/**
 * 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;
275

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

278 279 280
  if (!SNODE_VALID(en))
    s_add_tail(&p->lsal, SNODE en);

281
  if (!en->nf || !en->lsa_body)
282 283 284 285
    en->nf = lsa->nf;

  if (en->nf != lsa->nf)
  {
286 287
    log(L_ERR "%s: LSA ID collision for %N",
	p->p.name, lsa->nf->fn.addr);
288 289

    en = NULL;
290 291 292
    goto drop;
  }

293 294
  if (en->mode != lsa->mode)
    en->mode = lsa->mode;
295 296 297 298

  if (en->next_lsa_body)
  {
    /* Ignore the new LSA if it is the same as the scheduled one */
299 300 301
    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)))
302 303 304 305 306 307 308 309 310 311
      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 */
312 313 314 315
  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))))
316 317 318 319 320 321
    goto drop;

  lsa_body = lsab_flush(p);

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

325 326 327 328 329 330 331 332 333 334 335 336 337 338
    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)
339
{
340 341 342 343
  /* 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;
344

345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
  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.
   */

362 363
  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);
364 365 366 367 368 369 370 371 372 373 374 375 376

  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;
377
    ospf_flood_lsa(p, en, NULL);
378 379 380 381 382 383
    return;
  }

  en->lsa.sn++;
  en->lsa.age = 0;
  en->init_age = 0;
384
  en->inst_time = current_time();
385
  lsa_generate_checksum(&en->lsa, en->lsa_body);
386
  ospf_flood_lsa(p, en, NULL);
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
}

/**
 * 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
402
 * origination.
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
 */
void
ospf_flush_lsa(struct ospf_proto *p, struct top_hash_entry *en)
{
  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;

418 419 420
  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);

421
  en->lsa.age = LSA_MAXAGE;
422 423 424 425 426 427
  ospf_flood_lsa(p, en, NULL);

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

  en->mode = LSA_M_BASIC;
428 429 430 431 432 433 434 435
}

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.
436
   * The log message is for 'remove' - we hide empty LSAs from users.
437 438
   */

439 440 441
  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);

442 443 444 445 446 447 448 449 450 451 452 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
  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;
479 480
  btime now_ = current_time();
  int real_age;
481 482 483 484 485 486

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

487
    real_age = en->init_age + (now_ - en->inst_time) TO_S;
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507

    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)
508
    {
509 510
      ospf_flush_lsa(p, en);
      continue;
511
    }
512 513 514 515 516 517

    en->lsa.age = real_age;
  }
}


518
static u32
519 520 521 522 523
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
524 525
   * prefix has to be mapped on itself.  All received prefixes have to be mapped
   * on different u32s.
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
   *
   * 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
546 547
   * 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.
548 549 550
   */

  if (ospf_is_v3(p))
551 552 553 554 555 556
  {
    if (!nf->lsa_id)
      nf->lsa_id = idm_alloc(&p->idm);

    return nf->lsa_id;
  }
557

558 559 560
  net_addr_ip4 *net = (void *) nf->fn.addr;
  u32 id = ip4_to_u32(net->prefix);
  int pxlen = net->pxlen;
561 562 563 564 565 566 567 568 569 570 571 572

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

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


static void *
573
lsab_alloc(struct ospf_proto *p, uint size)
574
{
575
  uint offset = p->lsab_used;
576 577 578 579 580 581 582 583
  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;
584 585 586
}

static inline void *
587
lsab_allocz(struct ospf_proto *p, uint size)
588
{
589
  void *r = lsab_alloc(p, size);
590 591 592 593 594
  bzero(r, size);
  return r;
}

static inline void *
595
lsab_flush(struct ospf_proto *p)
596
{
597 598 599
  void *r = mb_alloc(p->p.pool, p->lsab_used);
  memcpy(r, p->lsab, p->lsab_used);
  p->lsab_used = 0;
600 601 602
  return r;
}

603 604
static inline void
lsab_reset(struct ospf_proto *p)
605
{
606
  p->lsab_used = 0;
607 608 609
}

static inline void *
610
lsab_offset(struct ospf_proto *p, uint offset)
611
{
612
  return ((byte *) p->lsab) + offset;
613 614
}

615
static inline void * UNUSED
616
lsab_end(struct ospf_proto *p)
617
{
618
  return ((byte *) p->lsab) + p->lsab_used;
619 620
}

621

622 623 624 625 626
/*
 *	Router-LSA handling
 *	Type = LSA_T_RT
 */

627 628 629
static int
configured_stubnet(struct ospf_area *oa, struct ifa *a)
{
630
  /* Does not work for IA_PEER addresses, but it is not called on these */
631 632
  struct ospf_stubnet_config *sn;
  WALK_LIST(sn, oa->ac->stubnet_list)
633 634
  {
    if (sn->summary)
635
    {
636
      if (net_in_netX(&a->prefix, &sn->prefix))
637
	return 1;
638
    }
639 640
    else
    {
641
      if (net_equal(&a->prefix, &sn->prefix))
642 643 644 645
	return 1;
    }
  }

646 647
  return 0;
}
648

649
static int
650 651 652 653 654 655 656 657
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)
658 659
  {
    if (neigh->state == NEIGHBOR_FULL)
660
    {
661 662
      if (neigh->rid == ifa->drid)
	return 1;
663

664 665
      if (ifa->state == OSPF_IS_DR)
	return 1;
666
    }
667
  }
668 669 670 671

  return 0;
}

672 673 674 675
static inline u32
get_rt_options(struct ospf_proto *p, struct ospf_area *oa, int bitv)
{
  u32 opts = 0;
676

677 678
  if (p->areano > 1)
    opts |= OPT_RT_B;
679

680 681
  if ((p->areano > 1) && oa_is_nssa(oa) && oa->ac->translator)
    opts |= OPT_RT_NT;
682

683 684
  if (p->asbr && !oa_is_stub(oa))
    opts |= OPT_RT_E;
685

686 687
  if (bitv)
    opts |= OPT_RT_V;
688

689 690
  return opts;
}
691

692 693 694 695 696 697 698 699 700 701
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;
}
702

703 704 705 706 707 708
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;
709

710 711 712
  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
713

714
  WALK_LIST(ifa, p->iface_list)
715
  {
716
    int net_lsa = 0;
717
    u32 link_cost = p->stub_router ? 0xffff : ifa->cost;
718 719 720

    if ((ifa->type == OSPF_IT_VLINK) && (ifa->voa == oa) &&
	(!EMPTY_LIST(ifa->neigh_list)))
721 722 723
    {
      neigh = (struct ospf_neighbor *) HEAD(ifa->neigh_list);
      if ((neigh->state == NEIGHBOR_FULL) && (ifa->cost <= 0xffff))
724
	bitv = 1;
725
    }
726 727

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

730 731
    ifa->rt_pos_beg = i;

732
    /* RFC 2328 - 12.4.1.1-4 */
733
    switch (ifa->type)
734 735 736 737 738
    {
    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
739
	{
740 741 742 743 744 745 746 747 748
	  /*
	   * 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);
749
	  i++;
750 751
	}
      break;
752

753 754 755 756 757 758 759
    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;
760
      }
761 762 763 764 765 766 767 768 769
      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:
770
      log(L_BUG "OSPF: Unknown interface type");
771 772
      break;
    }
773

774 775
    ifa->rt_pos_end = i;

776 777 778
    /* Now we will originate stub area if there is no primary */
    if (net_lsa ||
	(ifa->type == OSPF_IT_VLINK) ||
779
	((ifa->addr->flags & IA_PEER) && ! ifa->cf->stub) ||
780 781
	configured_stubnet(oa, ifa->addr))
      continue;
782

783
      /* Host or network stub entry */
784 785 786
    if ((ifa->addr->flags & IA_HOST) ||
	(ifa->state == OSPF_IS_LOOP) ||
	(ifa->type == OSPF_IT_PTMP))
787
      add_rt2_lsa_link(p, LSART_STUB, ipa_to_u32(ifa->addr->ip), 0xffffffff, 0);
788
    else
789 790
      add_rt2_lsa_link(p, LSART_STUB, ip4_to_u32(net4_prefix(&ifa->addr->prefix)),
		       u32_mkmask(net4_pxlen(&ifa->addr->prefix)), ifa->cost);
791
    i++;
792 793

    ifa->rt_pos_end = i;
794
  }
795

796
  struct ospf_stubnet_config *sn;
797 798
  WALK_LIST(sn, oa->ac->stubnet_list)
    if (!sn->hidden)
799 800
      add_rt2_lsa_link(p, LSART_STUB, ip4_to_u32(net4_prefix(&sn->prefix)),
		       u32_mkmask(net4_pxlen(&sn->prefix)), sn->cost), i++;
801

802
  struct ospf_lsa_rt *rt = p->lsab;
803
  /* Store number of links in lower half of options */
804
  rt->options = get_rt_options(p, oa, bitv) | (u16) i;
805
}
806

807 808
static inline void
add_rt3_lsa_link(struct ospf_proto *p, u8 type, struct ospf_iface *ifa, u32 nif, u32 id)
809
{
810
  struct ospf_lsa_rt3_link *ln = lsab_alloc(p, sizeof(struct ospf_lsa_rt3_link));
811 812 813
  ln->type = type;
  ln->padding = 0;
  ln->metric = ifa->cost;
814
  ln->lif = ifa->iface_id;
815 816 817 818
  ln->nif = nif;
  ln->id = id;
}

819 820
static void
prepare_rt3_lsa_body(struct ospf_proto *p, struct ospf_area *oa)
821 822
{
  struct ospf_iface *ifa;
823
  struct ospf_neighbor *neigh;
824
  int bitv = 0;
825
  int i = 0;
826

827 828 829
  ASSERT(p->lsab_used == 0);
  lsab_allocz(p, sizeof(struct ospf_lsa_rt));
  /* ospf_lsa_rt header will be filled later */
830

831
  WALK_LIST(ifa, p->iface_list)
832 833 834 835 836 837 838 839 840 841 842 843
  {
    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;

844 845
    ifa->rt_pos_beg = i;

846
    /* RFC 5340 - 4.4.3.2 */
847
    switch (ifa->type)
848 849 850 851 852 853 854
    {
    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;
855

856 857 858 859 860
    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;
861

862 863 864 865 866 867 868
    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:
869
      log(L_BUG "OSPF: Unknown interface type");
870
      break;
871 872
    }

873 874
    ifa->rt_pos_end = i;
  }
875

876 877 878
  struct ospf_lsa_rt *rt = p->lsab;
  rt->options = get_rt_options(p, oa, bitv) | (oa->options & LSA_OPTIONS_MASK);
}
879

880 881
static void
ospf_originate_rt_lsa(struct ospf_proto *p, struct ospf_area *oa)
882
{
883 884 885 886 887 888
  struct ospf_new_lsa lsa = {
    .type = LSA_T_RT,
    .dom  = oa->areaid,
    .id   = ospf_is_v2(p) ? p->router_id : 0,
    .opts = oa->options
  };
889

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

892 893 894 895
  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
896

897 898
  oa->rt = ospf_originate_lsa(p, &lsa);
}
899

900

901 902 903 904
/*
 *	Net-LSA handling
 *	Type = LSA_T_NET
 */
905

906 907
static void
prepare_net2_lsa_body(struct ospf_proto *p, struct ospf_iface *ifa)
908
{
909 910 911 912
  struct ospf_lsa_net *net;
  struct ospf_neighbor *n;
  int nodes = ifa->fadj + 1;
  u16 i = 1;
913

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

917
  net->optx = u32_mkmask(ifa->addr->prefix.pxlen);
918
  net->routers[0] = p->router_id;
919

920 921 922 923 924 925 926 927 928
  WALK_LIST(n, ifa->neigh_list)
  {
    if (n->state == NEIGHBOR_FULL)
    {
      net->routers[i] = n->rid;
      i++;
    }
  }
  ASSERT(i == nodes);
929 930
}

931 932
static void
prepare_net3_lsa_body(struct ospf_proto *p, struct ospf_iface *ifa)
933
{
934
  struct ospf_lsa_net *net;
935 936
  int nodes = ifa->fadj + 1;
  u32 options = 0;
937 938 939 940
  u16 i = 1;

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

942
  net->routers[0] = p->router_id;
943

944
  struct ospf_neighbor *n;
Ondřej Filip's avatar
Ondřej Filip committed
945
  WALK_LIST(n, ifa->neigh_list)
946
  {
Ondřej Filip's avatar
Ondřej Filip committed
947
    if (n->state == NEIGHBOR_FULL)
948
    {
949 950 951 952 953
      /* 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);

954 955 956 957
      if (en)
	options |= ((struct ospf_lsa_link *) en->lsa_body)->options;

      net->routers[i] = n->rid;
958 959 960
      i++;
    }
  }
961 962
  ASSERT(i == nodes);

963
  net->optx = options & LSA_OPTIONS_MASK;
964 965
}

966 967 968 969 970 971 972 973 974 975 976
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
  };

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

979 980
  if (ospf_is_v2(p))
    prepare_net2_lsa_body(p, ifa);
981
  else
982
    prepare_net3_lsa_body(p, ifa);
983

984
  ifa->net_lsa = ospf_originate_lsa(p, &lsa);
985 986
}

987

988 989 990 991 992
/*
 *	(Net|Rt)-summary-LSA handling
 *	(a.k.a. Inter-Area-(Prefix|Router)-LSA)
 *	Type = LSA_T_SUM_NET, LSA_T_SUM_RT
 */
993

994 995
static inline void
prepare_sum2_lsa_body(struct ospf_proto *p, uint pxlen, u32 metric)
996
{
997
  struct ospf_lsa_sum2 *sum;
998

999 1000
  sum = lsab_allocz(p, sizeof(struct ospf_lsa_sum2));
  sum->netmask = u32_mkmask(pxlen);
1001 1002 1003
  sum->metric = metric;
}

1004 1005
static inline void
prepare_sum3_net_lsa_body(struct ospf_proto *p, ort *nf, u32 metric)
1006
{
1007
  struct ospf_lsa_sum3_net *sum;
1008

1009 1010
  sum = lsab_allocz(p, sizeof(struct ospf_lsa_sum3_net) +
		    IPV6_PREFIX_SPACE(nf->fn.addr->pxlen));
1011
  sum->metric = metric;
1012
  ospf3_put_prefix(sum->prefix, nf->fn.addr, 0, 0);
1013 1014
}

1015 1016
static inline void
prepare_sum3_rt_lsa_body(struct ospf_proto *p, u32 drid, u32 metric, u32 options)
1017
{
1018
  struct ospf_lsa_sum3_rt *sum;
1019

1020
  sum = lsab_allocz(p, sizeof(struct ospf_lsa_sum3_rt));
1021
  sum->options = options;
1022 1023
  sum->metric = metric;
  sum->drid = drid;
1024 1025
}

Ondřej Filip's avatar
Ondřej Filip committed
1026
void
1027
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
1028
{
1029 1030
  struct ospf_new_lsa lsa = {
    .type = LSA_T_SUM_NET,
1031
    .mode = LSA_M_RTCALC,
1032 1033 1034 1035 1036
    .dom  = oa->areaid,
    .id   = ort_to_lsaid(p, nf),
    .opts = oa->options,
    .nf   = nf
  };
1037

1038
  if (ospf_is_v2(p))
1039
    prepare_sum2_lsa_body(p, nf->fn.addr->pxlen, metric);
1040 1041
  else
    prepare_sum3_net_lsa_body(p, nf, metric);
1042

1043
  ospf_originate_lsa(p, &lsa);
1044
}
1045

1046
void
1047
ospf_originate_sum_rt_lsa(struct ospf_proto *p, struct ospf_area *oa, u32 drid, int metric, u32 options)
1048
{
1049 1050
  struct ospf_new_lsa lsa = {
    .type = LSA_T_SUM_RT,
1051
    .mode = LSA_M_RTCALC,
1052
    .dom  = oa->areaid,
1053
    .id   = drid,	/* Router ID of ASBR, irrelevant for OSPFv3 */
1054 1055
    .opts = oa->options
  };
Ondřej Filip's avatar
Ondřej Filip committed
1056

1057 1058
  if (ospf_is_v2(p))
    prepare_sum2_lsa_body(p, 0, metric);
1059
  else
1060
    prepare_sum3_rt_lsa_body(p, drid, metric, options & LSA_OPTIONS_MASK);
1061

1062
  ospf_originate_lsa(p, &lsa);
Ondřej Filip's avatar
Ondřej Filip committed
1063 1064
}

1065

1066
/*
1067 1068
 *	AS-external-LSA and NSSA-LSA handling
 *	Type = LSA_T_EXT, LSA_T_NSSA
1069
 */
1070

1071 1072 1073
static inline void
prepare_ext2_lsa_body(struct ospf_proto *p, uint pxlen,
		      u32 metric, u32 ebit, ip_addr fwaddr, u32 tag)
1074
{
1075
  struct ospf_lsa_ext2 *ext;
1076

1077 1078 1079 1080 1081
  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;
1082

1083 1084
  if (ebit)
    ext->metric |= LSA_EXT2_EBIT;
1085
}
1086

1087 1088 1089
static inline void
prepare_ext3_lsa_body(struct ospf_proto *p, ort *nf,
		      u32 metric, u32 ebit, ip_addr fwaddr, u32 tag, int pbit)
1090
{
1091 1092
  struct ospf_lsa_ext3 *ext;
  int bsize = sizeof(struct ospf_lsa_ext3)
1093
    + IPV6_PREFIX_SPACE(nf->fn.addr->pxlen)
1094 1095 1096
    + (ipa_nonzero(fwaddr) ? 16 : 0)
    + (tag ? 4 : 0);

1097 1098 1099
  ext = lsab_allocz(p, bsize);
  ext->metric = metric & LSA_METRIC_MASK;
  u32 *buf = ext->rest;
1100

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

1103 1104
  if (ebit)
    ext->metric |= LSA_EXT3_EBIT;
1105

1106 1107
  if (ipa_nonzero(fwaddr))
  {
1108
    ext->metric |= LSA_EXT3_FBIT;
1109
    buf = ospf3_put_addr(buf, fwaddr);
1110
  }
1111 1112

  if (tag)
1113
  {
1114
    ext->metric |= LSA_EXT3_TBIT;
1115 1116
    *buf++ = tag;
  }
1117 1118
}

1119 1120
/**
 * originate_ext_lsa - new route received from nest and filters
1121
 * @p: OSPF protocol instance
1122 1123
 * @oa: ospf_area for which LSA is originated
 * @nf: network prefix and mask
1124 1125 1126
 * @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)
1127 1128
 * @fwaddr: the forwarding address
 * @tag: the route tag
1129
 * @pbit: P-bit for NSSA LSAs (bool), ignored for external LSAs
1130 1131 1132 1133
 *
 * 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
1134
 * is external or NSSA, but it specifies the source of origination -
1135 1136 1137
 * the export from ospf_rt_notify(), or the NSSA-EXT translation.
 */
void
1138
ospf_originate_ext_lsa(struct ospf_proto *p, struct ospf_area *oa, ort *nf, u8 mode,
1139
		       u32 metric, u32 ebit, ip_addr fwaddr, u32 tag, int pbit)
1140
{
1141 1142
  struct ospf_new_lsa lsa = {
    .type = oa ? LSA_T_NSSA : LSA_T_EXT,
1143
    .mode = mode, /* LSA_M_EXPORT or LSA_M_RTCALC */
1144 1145 1146 1147 1148
    .dom  = oa ? oa->areaid : 0,
    .id   = ort_to_lsaid(p, nf),
    .opts = oa ? (pbit ? OPT_P : 0) : OPT_E,
    .nf   = nf
  };
1149

1150
  if (ospf_is_v2(p))
1151
    prepare_ext2_lsa_body(p, nf->fn.addr->pxlen, metric, ebit, fwaddr, tag);
1152 1153
  else
    prepare_ext3_lsa_body(p, nf, metric, ebit, fwaddr, tag, oa && pbit);
1154

1155
  ospf_originate_lsa(p, &lsa);
1156 1157
}

1158 1159 1160
static struct top_hash_entry *
ospf_hash_find_(struct top_graph *f, u32 domain, u32 lsa, u32 rtr, u32 type);

1161 1162 1163 1164
static void
ospf_flush_ext_lsa(struct ospf_proto *p, struct ospf_area *oa, ort *nf)
{
  struct top_hash_entry *en;
1165

1166 1167 1168
  u32 type = oa ? LSA_T_NSSA : LSA_T_EXT;
  u32 dom = oa ? oa->areaid : 0;
  u32 id = ort_to_lsaid(p, nf);
1169

1170
  en = ospf_hash_find_(p->gr, dom, id, p->router_id, type);
1171

1172 1173
  if (!en || (en->nf != nf))
    return;
1174

1175
  ospf_flush_lsa(p, en);
1176 1177
}

1178 1179 1180 1181 1182 1183 1184
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;
1185

1186 1187
  WALK_LIST(ifa, p->iface_list)
    if ((ifa->iface == iface) &&
1188
	(!ospf_is_v2(p) || ipa_in_netX(gw, &ifa->addr->prefix)))
1189 1190 1191 1192
      return 1;

  return 0;
}
1193

1194
static inline ip_addr
1195
find_surrogate_fwaddr(struct ospf_proto *p, struct ospf_area *oa)
1196 1197 1198 1199 1200
{
  struct ospf_iface *ifa;
  struct ifa *a, *cur_addr = NULL;
  int np, cur_np = 0;

1201 1202 1203
  /* RFC 3101 2.3 - surrogate forwarding address selection */

  WALK_LIST(ifa, p->iface_list)
1204 1205 1206 1207 1208
  {
    if ((ifa->oa != oa) ||
	(ifa->type == OSPF_IT_VLINK))
      continue;

1209
    if (ospf_is_v2(p))
1210
    {
1211 1212
      a = ifa->addr;
      if (a->flags & IA_PEER)
1213 1214
	continue;

1215
      np = (a->flags & IA_HOST) ? 3 : (ifa->stub ? 2 : 1);
1216 1217 1218 1219 1220 1221
      if (np > cur_np)
      {
	cur_addr = a;
	cur_np = np;
      }
    }
1222 1223 1224 1225
    else /* OSPFv3 */
    {
      WALK_LIST(a, ifa->iface->addrs)
      {
1226
	if ((a->prefix.type != ospf_get_af(p)) ||
1227
	    (a->flags & IA_SECONDARY) ||
1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239
	    (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;
	}
      }
    }
1240 1241 1242 1243 1244
  }

  return cur_addr ? cur_addr->ip : IPA_NONE;
}

1245
void
1246
ospf_rt_notify(struct proto *P, struct channel *ch UNUSED, net *n, rte *new, rte *old UNUSED)
1247
{
1248 1249 1250
  struct ospf_proto *p = (struct ospf_proto *) P;
  struct ospf_area *oa = NULL;	/* non-NULL for NSSA-LSA */
  ort *nf;
1251
  struct ea_list *ea = new->attrs->eattrs;
1252

1253 1254 1255 1256 1257 1258 1259
  /*
   * 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
1260

1261 1262
  if ((p->areano == 1) && oa_is_nssa(HEAD(p->area_list)))
    oa = HEAD(p->area_list);
1263

1264
  if (!new)
1265
  {
1266
    nf = fib_find(&p->rtf, n->n.addr);
1267

1268
    if (!nf || !nf->external_rte)
1269
      return;
1270

1271 1272
    ospf_flush_ext_lsa(p, oa, nf);
    nf->external_rte = 0;
1273

1274 1275
    /* Old external route might blocked some NSSA translation */
    if ((p->areano > 1) && rt_is_nssa(nf) && nf->n.oa->translate)
1276
      ospf_schedule_rtcalc(p);
1277

1278 1279
    return;
  }
1280

1281
  ASSERT(p->asbr);
1282

1283 1284
  /* Get route attributes */
  rta *a = new->attrs;
1285 1286 1287 1288
  eattr *m1a = ea_find(ea, EA_OSPF_METRIC1);
  eattr *m2a = ea_find(ea, EA_OSPF_METRIC2);
  uint m1 = m1a ? m1a->u.data : 0;
  uint m2 = m2a ? m2a->u.data : 10000;
1289

1290 1291 1292 1293 1294 1295
  if (m1 > LSINFINITY)
  {
    log(L_WARN "%s: Invalid ospf_metric1 value %u for route %N",
	p->p.name, m1, n->n.addr);
    m1 = LSINFINITY;
  }
1296

1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312
  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;
  uint tag = ea_get_int(ea, EA_OSPF_TAG, 0);

  ip_addr fwd = IPA_NONE;
1313 1314
  if ((a->dest == RTD_UNICAST) && use_gw_for_fwaddr(p, a->nh.gw, a->nh.iface))
    fwd = a->nh.gw;
1315

1316 1317 1318 1319
  /* NSSA-LSA with P-bit set must have non-zero forwarding address */
  if (oa && ipa_zero(fwd))
  {
    fwd = find_surrogate_fwaddr(p, oa);
1320

1321
    if (ipa_zero(fwd))
1322
    {
1323 1324
      log(L_ERR "%s: Cannot find forwarding address for NSSA-LSA %N",
	  p->p.name, n->n.addr);
1325
      return;
1326
    }
1327 1328
  }

1329
  nf = fib_get(&p->rtf, n->n.addr);
1330
  ospf_originate_ext_lsa(p, oa, nf, LSA_M_EXPORT, metric, ebit, fwd, tag, 1);