topology.c 51.3 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

Ondřej Filip's avatar
Ondřej Filip committed
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 73 74

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

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 131

  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;
      en->inst_time = now;
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 163
       * 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;
      en->inst_time = now;
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 199 200 201 202 203 204 205 206 207
  /* Enforce MinLSInterval */
  if ((en->init_age == 0) && en->inst_time && ((en->inst_time + MINLSINTERVAL) > now))
    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 240

  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;
  en->inst_time = now;
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 281 282 283 284 285
  if (!SNODE_VALID(en))
    s_add_tail(&p->lsal, SNODE en);

  if (en->lsa_body == NULL)
    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 384
    return;
  }

  en->lsa.sn++;
  en->lsa.age = 0;
  en->init_age = 0;
  en->inst_time = now;
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 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
  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;
  bird_clock_t real_age;

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

    real_age = en->init_age + (now - en->inst_time);

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

    en->lsa.age = real_age;
  }
}

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

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

    return nf->lsa_id;
  }
555

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

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

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


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

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

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

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

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

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

619

620 621 622 623 624
/*
 *	Router-LSA handling
 *	Type = LSA_T_RT
 */

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

644 645
  return 0;
}
646

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

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

  return 0;
}

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

675 676
  if (p->areano > 1)
    opts |= OPT_RT_B;
677

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

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

684 685
  if (bitv)
    opts |= OPT_RT_V;
686

687 688
  return opts;
}
689

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

701 702 703 704 705 706
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;
707

708 709 710
  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
711

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

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

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

728 729
    ifa->rt_pos_beg = i;

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

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

772 773
    ifa->rt_pos_end = i;

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

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

    ifa->rt_pos_end = i;
792
  }
793

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

800
  struct ospf_lsa_rt *rt = p->lsab;
801
  /* Store number of links in lower half of options */
802
  rt->options = get_rt_options(p, oa, bitv) | (u16) i;
803
}
Ondřej Filip's avatar
Ondřej Filip committed
804

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

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

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

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

842 843
    ifa->rt_pos_beg = i;

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

854 855 856 857 858
    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;
859

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

871 872
    ifa->rt_pos_end = i;
  }
873

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

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

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

890 891 892 893
  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
894

895 896
  oa->rt = ospf_originate_lsa(p, &lsa);
}
897

898

899 900 901 902
/*
 *	Net-LSA handling
 *	Type = LSA_T_NET
 */
903

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

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

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

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

929 930
static void
prepare_net3_lsa_body(struct ospf_proto *p, struct ospf_iface *ifa)
Ondřej Filip's avatar
Ondřej Filip committed
931
{
932
  struct ospf_lsa_net *net;
933 934
  int nodes = ifa->fadj + 1;
  u32 options = 0;
935 936 937 938
  u16 i = 1;

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

940
  net->routers[0] = p->router_id;
941

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

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

      net->routers[i] = n->rid;
Ondřej Filip's avatar
Ondřej Filip committed
956 957 958
      i++;
    }
  }
959 960
  ASSERT(i == nodes);

961
  net->optx = options & LSA_OPTIONS_MASK;
Ondřej Filip's avatar
Ondřej Filip committed
962 963
}

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

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

977 978
  if (ospf_is_v2(p))
    prepare_net2_lsa_body(p, ifa);
979
  else
980
    prepare_net3_lsa_body(p, ifa);
981

982
  ifa->net_lsa = ospf_originate_lsa(p, &lsa);
983 984
}

985

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

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

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

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

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

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

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

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

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

1041
  ospf_originate_lsa(p, &lsa);
1042
}
1043

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

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

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

1063

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

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

1075 1076 1077 1078 1079
  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;
1080

1081 1082
  if (ebit)
    ext->metric |= LSA_EXT2_EBIT;
1083
}