rt.c 32.7 KB
Newer Older
1
/*
2 3 4 5 6
 * BIRD -- OSPF
 * 
 * (c) 2000--2004 Ondrej Filip <feela@network.cz>
 * 
 * Can be freely distributed and used under the terms of the GNU GPL.
7 8 9
 */

#include "ospf.h"
10

11 12 13
static void add_cand(list * l, struct top_hash_entry *en, 
		     struct top_hash_entry *par, u32 dist,
		     struct ospf_area *oa);
14 15 16
static int calc_next_hop(struct ospf_area *oa,
			 struct top_hash_entry *en,
			 struct top_hash_entry *par);
17 18
static void rt_sync(struct proto_ospf *po);

19
/* In ospf_area->rtr we store paths to routers, but we use RID (and not IP address)
20
   as index, so we need to encapsulate RID to IP address */
21 22 23 24 25 26 27
#ifdef OSPFv2
#define ipa_from_rid(x) _MI(x)
#else /* OSPFv3 */
#define ipa_from_rid(x) _MI(0,0,0,x)
#endif


28 29 30 31
static void
fill_ri(orta * orta)
{
  orta->type = RTS_DUMMY;
32
  orta->options = 0;
33 34 35
  orta->metric1 = LSINFINITY;
  orta->metric2 = LSINFINITY;
  orta->tag = 0;
36
  orta->rid = 0;
37 38 39
  orta->oa = NULL;
  orta->ifa = NULL;
  orta->nh = IPA_NONE;
40
}
41

42
void
43
ospf_rt_initort(struct fib_node *fn)
44
{
45 46 47 48
  ort *ri = (ort *) fn;
  fill_ri(&ri->n);
  memcpy(&ri->o, &ri->n, sizeof(orta));
}
49

50 51 52 53 54 55 56
/* Whether the ASBR or the forward address destination is preferred in AS external route selection
   according to 16.4.1. */
static inline int
epath_preferred(orta *ep)
{
  return (ep->type == RTS_OSPF) && (ep->oa->areaid != 0);
}
Ondřej Filip's avatar
Ondřej Filip committed
57

58
/* If new is better return 1 */
59
static int
60
ri_better(struct proto_ospf *po, orta *new, orta *old)
61 62 63 64 65 66 67
{
  if (old->type == RTS_DUMMY)
    return 1;

  if (old->metric1 == LSINFINITY)
    return 1;

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
  if (new->type < old->type)
    return 1;

  if (new->type > old->type)
    return 0;

  if (new->metric1 < old->metric1)
    return 1;

  if (new->metric1 > old->metric1)
    return 0;

  return 0;
}


/* 16.4. (3), return 1 if new is better */
static int
ri_better_asbr(struct proto_ospf *po, orta *new, orta *old)
{
  /* We know that the old ASBB is valid */

  if (!po->rfc1583)
91
  {
92 93 94 95 96 97 98 99
    int new_pref = epath_preferred(new);
    int old_pref = epath_preferred(old);

    if (new_pref > old_pref)
      return 1;

    if (new_pref < old_pref)
      return 0;
100
  }
Ondřej Filip's avatar
Ondřej Filip committed
101

102
  if (new->metric1 < old->metric1)
103 104
    return 1;

105
  if (new->metric1 > old->metric1)
Ondřej Filip's avatar
Ondřej Filip committed
106
    return 0;
107

108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
  /* Larger area ID is preferred */
  if (new->oa->areaid > old->oa->areaid)
    return 1;

  return 0;
}

/* 16.4. (6), return 1 if new is better */
static int
ri_better_ext(struct proto_ospf *po, orta *new, orta *old)
{
  if (old->type == RTS_DUMMY)
    return 1;

  if (old->metric1 == LSINFINITY)
    return 1;

  /* 16.4. (6a) */
  if (new->type < old->type)
    return 1;

  if (new->type > old->type)
    return 0;

  /* 16.4. (6b), same type */
133
  if (new->type == RTS_OSPF_EXT2)
Ondřej Filip's avatar
Ondřej Filip committed
134
  {
135 136 137 138 139
    if (new->metric2 < old->metric2)
      return 1;

    if (new->metric2 > old->metric2)
      return 0;
140
  }
Ondřej Filip's avatar
Ondřej Filip committed
141

142 143
  /* 16.4. (6c) */
  if (!po->rfc1583)
144
  {
145 146
    u32 new_pref = new->options & ORTA_PREF;
    u32 old_pref = old->options & ORTA_PREF;
Ondřej Filip's avatar
Ondřej Filip committed
147

148 149
    if (new_pref > old_pref)
      return 1;
150

151 152
    if (new_pref < old_pref)
      return 0;
153 154
  }

155
  /* 16.4. (6d) */
156 157 158 159 160 161
  if (new->metric1 < old->metric1)
    return 1;

  if (new->metric1 > old->metric1)
    return 0;

162 163
  return 0;
}
164

165 166 167 168 169 170
static inline void
ri_install_net(struct proto_ospf *po, ip_addr prefix, int pxlen, orta *new)
{
  ort *old = (ort *) fib_get(&po->rtf, &prefix, pxlen);
  if (ri_better(po, new, &old->n))
    memcpy(&old->n, new, sizeof(orta));
171 172
}

173 174
static inline void
ri_install_rt(struct ospf_area *oa, u32 rid, orta *new)
175
{
176 177 178 179 180
  ip_addr addr = ipa_from_rid(rid);
  ort *old = (ort *) fib_get(&oa->rtr, &addr, MAX_PREFIX_LENGTH);
  if (ri_better(oa->po, new, &old->n))
    memcpy(&old->n, new, sizeof(orta));
}
181

182 183 184 185 186 187 188
static inline void
ri_install_ext(struct proto_ospf *po, ip_addr prefix, int pxlen, orta *new)
{
  ort *old = (ort *) fib_get(&po->rtf, &prefix, pxlen);
  if (ri_better_ext(po, new, &old->n))
    memcpy(&old->n, new, sizeof(orta));
}
Ondřej Filip's avatar
Ondřej Filip committed
189

190 191 192 193 194 195 196 197 198 199
static inline void
update_anet(struct ospf_area *oa, ip_addr prefix, int pxlen, u32 metric)
{
  struct area_net *anet = (struct area_net *) fib_route(&oa->net_fib, prefix, pxlen);
  if (!anet)
    return;
  
  anet->active = 1;
  if (metric > anet->metric)
    anet->metric = metric;
200 201
}

202

203 204 205 206 207 208 209 210 211 212
#ifdef OSPFv2

static struct ospf_iface *
find_stub_src(struct ospf_area *oa, ip_addr px, int pxlen)
{
  struct ospf_iface *iff;

  WALK_LIST(iff, oa->po->iface_list)
    if ((iff->type != OSPF_IT_VLINK) &&
	(iff->oa == oa) &&
213
	ipa_equal(iff->addr->prefix, px) && 
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
	(iff->addr->pxlen == pxlen))
      return iff;

  return NULL;
}

#else /* OSPFv3 */

static struct ospf_iface *
find_stub_src(struct ospf_area *oa, ip_addr px, int pxlen)
{
  struct ospf_iface *iff;
  struct ifa *a;

  WALK_LIST(iff, oa->po->iface_list)
    if ((iff->type != OSPF_IT_VLINK) &&
	(iff->oa == oa))
      WALK_LIST(a, iff->iface->addrs)
	if (ipa_equal(a->prefix, px) && 
	    (a->pxlen == pxlen) &&
	    !(a->flags & IA_SECONDARY))
	  return iff;

  return NULL;
}

#endif

242 243 244
static void
add_network(struct ospf_area *oa, ip_addr px, int pxlen, int metric, struct top_hash_entry *en)
{
245 246 247 248 249 250 251 252 253 254 255
  orta nf = {
    .type = RTS_OSPF,
    .options = 0,
    .metric1 = metric,
    .metric2 = LSINFINITY,
    .tag = 0,
    .rid = en->lsa.rt,
    .oa = oa,
    .ifa = en->nhi,
    .nh = en->nh
  };
256

257 258 259 260 261 262 263 264 265 266
  if (en == oa->rt)
  {
    /* 
     * Local stub networks does not have proper iface in en->nhi
     * (because they all have common top_hash_entry en).
     * We have to find iface responsible for that stub network.
     * Some stubnets does not have any iface. Ignore them.
     */

    nf.ifa = find_stub_src(oa, px, pxlen);
267
    nf.nh = IPA_NONE;
268 269 270 271 272

    if (!nf.ifa)
      return;
  }

273 274
  ri_install_net(oa->po, px, pxlen, &nf);
  update_anet(oa, px, pxlen, metric);
275 276 277 278 279 280 281
}

#ifdef OSPFv3
static void
process_prefixes(struct ospf_area *oa)
{
  struct proto_ospf *po = oa->po;
282
  // struct proto *p = &po->proto;
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
  struct top_hash_entry *en, *src;
  struct ospf_lsa_prefix *px;
  ip_addr pxa;
  int pxlen;
  u8 pxopts;
  u16 metric;
  u32 *buf;
  int i;

  WALK_SLIST(en, po->lsal)
  {
    if (en->lsa.type != LSA_T_PREFIX)
      continue;

    if (en->domain != oa->areaid)
      continue;

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

    px = en->lsa_body;
304 305 306 307 308 309

    /* For router prefix-LSA, we would like to find the first router-LSA */
    if (px->ref_type == LSA_T_RT)
      src = ospf_hash_find_rt(po->gr, oa->areaid, px->ref_rt);
    else
      src = ospf_hash_find(po->gr, oa->areaid, px->ref_id, px->ref_rt, px->ref_type);
310 311 312 313

    if (!src)
      continue;

314 315 316 317
    /* Reachable in SPF */
    if (src->color != INSPF)
      continue;

318 319 320 321 322 323 324 325 326
    if (src->lsa.age == LSA_MAXAGE)
      continue;

    if ((src->lsa.type != LSA_T_RT) && (src->lsa.type != LSA_T_NET))
      continue;

    buf = px->rest;
    for (i = 0; i < px->pxcount; i++)
      {
327
	buf = lsa_get_ipv6_prefix(buf, &pxa, &pxlen, &pxopts, &metric);
328 329 330 331

	if (pxopts & OPT_PX_NU)
	  continue;

332 333 334 335
	/* Store the first global address to use it later as a vlink endpoint */
	if ((pxopts & OPT_PX_LA) && ipa_zero(src->lb))
	  src->lb = pxa;

336 337 338 339 340 341
	add_network(oa, pxa, pxlen, src->dist + metric, src);
      }
  }
}
#endif

342 343 344 345

static void
ospf_rt_spfa_rtlinks(struct ospf_area *oa, struct top_hash_entry *act, struct top_hash_entry *en)
{
346
  // struct proto *p = &oa->po->proto;
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
  struct proto_ospf *po = oa->po;
  u32 i;

  struct ospf_lsa_rt *rt = en->lsa_body;
  struct ospf_lsa_rt_link *rr = (struct ospf_lsa_rt_link *) (rt + 1);

  for (i = 0; i < lsa_rt_count(&en->lsa); i++)
    {
      struct ospf_lsa_rt_link *rtl = rr + i;
      struct top_hash_entry *tmp = NULL;

      DBG("     Working on link: %R (type: %u)  ", rtl->id, rtl->type);
      switch (rtl->type)
	{
#ifdef OSPFv2
	case LSART_STUB:
363 364 365 366
	  /* This violates RFC 2328! But it is mostly harmless. */
	  add_network(oa, ipa_from_u32(rtl->id),
		      ipa_mklen(ipa_from_u32(rtl->data)),
		      act->dist + rtl->metric, act);
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
	  break;
#endif

	case LSART_NET:
#ifdef OSPFv2
	  /* In OSPFv2, rtl->id is IP addres of DR, Router ID is not known */
	  tmp = ospf_hash_find_net(po->gr, oa->areaid, rtl->id);
#else /* OSPFv3 */
	  tmp = ospf_hash_find(po->gr, oa->areaid, rtl->nif, rtl->id, LSA_T_NET);
#endif
	  break;

	case LSART_VLNK:
	case LSART_PTP:
	  tmp = ospf_hash_find_rt(po->gr, oa->areaid, rtl->id);
	  break;
383

384 385 386 387
	default:
	  log("Unknown link type in router lsa. (rid = %R)", act->lsa.id);
	  break;
	}
388

389 390 391 392 393 394 395
      if (tmp)
	DBG("Going to add cand, Mydist: %u, Req: %u\n",
	    tmp->dist, act->dist + rtl->metric);
      add_cand(&oa->cand, tmp, act, act->dist + rtl->metric, oa);
    }
}

396
/* RFC 2328 16.1. calculating shortest paths for an area */
397
static void
398
ospf_rt_spfa(struct ospf_area *oa)
399
{
Ondřej Filip's avatar
Ondřej Filip committed
400 401
  struct proto *p = &oa->po->proto;
  struct proto_ospf *po = oa->po;
402
  struct ospf_lsa_rt *rt;
403
  struct ospf_lsa_net *ln;
Ondřej Filip's avatar
Ondřej Filip committed
404
  struct top_hash_entry *act, *tmp;
405
  u32 i, *rts;
Ondřej Filip's avatar
Ondřej Filip committed
406 407
  node *n;

Ondřej Filip's avatar
Ondřej Filip committed
408 409
  if (oa->rt == NULL)
    return;
410

411
  OSPF_TRACE(D_EVENTS, "Starting routing table calculation for area %R", oa->areaid);
412

413
  if (oa->rt->dist != LSINFINITY)
414
    bug("Aging was not processed.");
415

416
  /* 16.1. (1) */
417
  init_list(&oa->cand);		/* Empty list of candidates */
Ondřej Filip's avatar
Ondřej Filip committed
418
  oa->trcap = 0;
419

420 421
  DBG("LSA db prepared, adding me into candidate list.\n");

Ondřej Filip's avatar
Ondřej Filip committed
422 423
  oa->rt->dist = 0;
  oa->rt->color = CANDIDATE;
424
  add_head(&oa->cand, &oa->rt->cn);
425 426
  DBG("RT LSA: rt: %R, id: %R, type: %u\n",
      oa->rt->lsa.rt, oa->rt->lsa.id, oa->rt->lsa.type);
427

Ondřej Filip's avatar
Ondřej Filip committed
428
  while (!EMPTY_LIST(oa->cand))
429
  {
Ondřej Filip's avatar
Ondřej Filip committed
430 431
    n = HEAD(oa->cand);
    act = SKIP_BACK(struct top_hash_entry, cn, n);
432 433
    rem_node(n);

434 435
    DBG("Working on LSA: rt: %R, id: %R, type: %u\n",
	act->lsa.rt, act->lsa.id, act->lsa.type);
436

Ondřej Filip's avatar
Ondřej Filip committed
437 438
    act->color = INSPF;
    switch (act->lsa.type)
439
    {
Ondřej Filip's avatar
Ondřej Filip committed
440 441
    case LSA_T_RT:
      rt = (struct ospf_lsa_rt *) act->lsa_body;
442
      if (rt->options & OPT_RT_V)
Ondřej Filip's avatar
Ondřej Filip committed
443
	oa->trcap = 1;
444

445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
      /*
       * In OSPFv3, all routers are added to per-area routing
       * tables. But we use it just for ASBRs and ABRs. For the
       * purpose of the last step in SPF - prefix-LSA processing in
       * process_prefixes(), we use information stored in LSA db.
       */
      if ((rt->options & OPT_RT_E) || (rt->options & OPT_RT_B))
      {
	orta nf = {
	  .type = RTS_OSPF,
	  .options = rt->options,
	  .metric1 = act->dist,
	  .metric2 = LSINFINITY,
	  .tag = 0,
	  .rid = act->lsa.rt,
	  .oa = oa,
	  .ifa = act->nhi,
	  .nh = act->nh
	};
	ri_install_rt(oa, act->lsa.rt, &nf);
      }
466

467
#ifdef OSPFv2
468
      ospf_rt_spfa_rtlinks(oa, act, act);
469
#else /* OSPFv3 */
470 471 472
      for (tmp = ospf_hash_find_rt_first(po->gr, act->domain, act->lsa.rt);
	   tmp; tmp = ospf_hash_find_rt_next(tmp))
	ospf_rt_spfa_rtlinks(oa, act, tmp);
473
#endif
474

Ondřej Filip's avatar
Ondřej Filip committed
475 476 477
      break;
    case LSA_T_NET:
      ln = act->lsa_body;
478 479 480 481 482

#ifdef OSPFv2
      add_network(oa, ipa_and(ipa_from_u32(act->lsa.id), ln->netmask),
		  ipa_mklen(ln->netmask), act->dist, act);
#endif
483

Ondřej Filip's avatar
Ondřej Filip committed
484
      rts = (u32 *) (ln + 1);
485
      for (i = 0; i < lsa_net_count(&act->lsa); i++)
Ondřej Filip's avatar
Ondřej Filip committed
486
      {
487
	DBG("     Working on router %R ", rts[i]);
488
	tmp = ospf_hash_find_rt(po->gr, oa->areaid, rts[i]);
Ondřej Filip's avatar
Ondřej Filip committed
489 490 491 492
	if (tmp != NULL)
	  DBG("Found :-)\n");
	else
	  DBG("Not found!\n");
493
	add_cand(&oa->cand, tmp, act, act->dist, oa);
Ondřej Filip's avatar
Ondřej Filip committed
494 495
      }
      break;
496
    }
497
  }
Ondřej Filip's avatar
Ondřej Filip committed
498

499 500 501
#ifdef OSPFv3
  process_prefixes(oa);
#endif
Ondřej Filip's avatar
Ondřej Filip committed
502 503 504
}

static int
505
link_back(struct ospf_area *oa, struct top_hash_entry *en, struct top_hash_entry *par)
Ondřej Filip's avatar
Ondřej Filip committed
506 507 508 509 510
{
  u32 i, *rts;
  struct ospf_lsa_net *ln;
  struct ospf_lsa_rt *rt;
  struct ospf_lsa_rt_link *rtl, *rr;
511
  struct top_hash_entry *tmp;
512
  struct proto_ospf *po = oa->po;
Ondřej Filip's avatar
Ondřej Filip committed
513

514 515
  if (!en || !par) return 0;

516 517 518
  /* In OSPFv2, en->lb is set here. In OSPFv3, en->lb is just cleared here,
     it is set in process_prefixes() to any global addres in the area */

519
  en->lb = IPA_NONE;
520 521 522
#ifdef OSPFv3
  en->lb_id = 0;
#endif
523
  switch (en->lsa.type)
Ondřej Filip's avatar
Ondřej Filip committed
524 525
  {
    case LSA_T_RT:
526
      rt = (struct ospf_lsa_rt *) en->lsa_body;
Ondřej Filip's avatar
Ondřej Filip committed
527
      rr = (struct ospf_lsa_rt_link *) (rt + 1);
528
      for (i = 0; i < lsa_rt_count(&en->lsa); i++)
Ondřej Filip's avatar
Ondřej Filip committed
529 530 531 532 533 534 535
      {
	rtl = (rr + i);
	switch (rtl->type)
	{
	case LSART_STUB:
	  break;
	case LSART_NET:
536 537 538 539 540 541 542
#ifdef OSPFv2
	  /* In OSPFv2, rtl->id is IP addres of DR, Router ID is not known */
	  tmp = ospf_hash_find_net(po->gr, oa->areaid, rtl->id);
#else /* OSPFv3 */
	  tmp = ospf_hash_find(po->gr, oa->areaid, rtl->nif, rtl->id, LSA_T_NET);
#endif
	  if (tmp == par)
543 544
	  {
#ifdef OSPFv2
545
	    en->lb = ipa_from_u32(rtl->data);
546 547
#else /* OSPFv3 */
	    en->lb_id = rtl->lif;
548 549 550
#endif
	    return 1;
	  }
551

Ondřej Filip's avatar
Ondřej Filip committed
552 553 554
	  break;
	case LSART_VLNK:
	case LSART_PTP:
555 556
	  tmp = ospf_hash_find_rt(po->gr, oa->areaid, rtl->id);
	  if (tmp == par)
Ondřej Filip's avatar
Ondřej Filip committed
557
            return 1;
558

Ondřej Filip's avatar
Ondřej Filip committed
559 560
	  break;
	default:
561
	  log(L_WARN "Unknown link type in router lsa. (rid = %R)", en->lsa.rt);
Ondřej Filip's avatar
Ondřej Filip committed
562 563 564 565 566
	  break;
	}
      }
      break;
    case LSA_T_NET:
567
      ln = en->lsa_body;
Ondřej Filip's avatar
Ondřej Filip committed
568
      rts = (u32 *) (ln + 1);
569
      for (i = 0; i < lsa_net_count(&en->lsa); i++)
Ondřej Filip's avatar
Ondřej Filip committed
570
      {
571 572
	tmp = ospf_hash_find_rt(po->gr, oa->areaid, rts[i]);
	if (tmp == par)
Ondřej Filip's avatar
Ondřej Filip committed
573 574 575 576
          return 1;
      }
      break;
    default:
577
      bug("Unknown lsa type %x.", en->lsa.type);
Ondřej Filip's avatar
Ondřej Filip committed
578 579 580 581
  }
  return 0;
}

582 583
  
/* RFC 2328 16.2. calculating inter-area routes */
Ondřej Filip's avatar
Ondřej Filip committed
584
static void
585
ospf_rt_sum(struct ospf_area *oa)
Ondřej Filip's avatar
Ondřej Filip committed
586 587
{
  struct proto_ospf *po = oa->po;
588
  struct proto *p = &po->proto;
Ondřej Filip's avatar
Ondřej Filip committed
589
  struct top_hash_entry *en;
590 591 592 593 594
  ip_addr ip = IPA_NONE;
  u32 dst_rid = 0;
  u32 metric, options;
  struct area_net *anet;
  ort *abr;
595
  int pxlen = -1, type = -1;
Ondřej Filip's avatar
Ondřej Filip committed
596

597
  OSPF_TRACE(D_EVENTS, "Starting routing table calculation for inter-area (area %R)", oa->areaid);
Ondřej Filip's avatar
Ondřej Filip committed
598

599
  WALK_SLIST(en, po->lsal)
Ondřej Filip's avatar
Ondřej Filip committed
600
  {
601
    if ((en->lsa.type != LSA_T_SUM_RT) && (en->lsa.type != LSA_T_SUM_NET))
602
      continue;
603 604 605 606

    if (en->domain != oa->areaid)
      continue;

607
    /* 16.2. (1a) */
Ondřej Filip's avatar
Ondřej Filip committed
608 609
    if (en->lsa.age == LSA_MAXAGE)
      continue;
610

611
    /* 16.2. (2) */
612
    if (en->lsa.rt == po->router_id)
Ondřej Filip's avatar
Ondřej Filip committed
613 614
      continue;

615

Ondřej Filip's avatar
Ondřej Filip committed
616 617
    if (en->lsa.type == LSA_T_SUM_NET)
    {
618 619 620
      struct ospf_area *oaa;
      int skip = 0;

621 622 623 624 625 626
#ifdef OSPFv2
      struct ospf_lsa_sum *ls = en->lsa_body;
      pxlen = ipa_mklen(ls->netmask);
      ip = ipa_and(ipa_from_u32(en->lsa.id), ls->netmask);
#else /* OSPFv3 */
      u8 pxopts;
627
      u16 rest;
628
      struct ospf_lsa_sum_net *ls = en->lsa_body;
629
      lsa_get_ipv6_prefix(ls->prefix, &ip, &pxlen, &pxopts, &rest);
630

631 632 633 634 635 636
      if (pxopts & OPT_PX_NU)
	continue;
#endif

      metric = ls->metric & METRIC_MASK;
      options = 0;
Ondřej Filip's avatar
Ondřej Filip committed
637
      type = ORT_NET;
638 639 640 641 642 643 644 645 646 647 648 649

      /* 16.2. (3) */
      WALK_LIST(oaa, po->area_list)
      {
        if ((anet = fib_find(&oaa->net_fib, &ip, pxlen)) && anet->active)
	{
	  skip = 1;
	  break;
	}
      }
      if (skip)
	continue;
Ondřej Filip's avatar
Ondřej Filip committed
650
    }
651
    else /* LSA_T_SUM_RT */
Ondřej Filip's avatar
Ondřej Filip committed
652
    {
653 654 655 656 657 658 659 660 661 662 663 664
#ifdef OSPFv2
      struct ospf_lsa_sum *ls = en->lsa_body;
      dst_rid = en->lsa.id;
      options = 0;
#else /* OSPFv3 */
      struct ospf_lsa_sum_rt *ls = en->lsa_body;
      dst_rid = ls->drid; 
      options = ls->options & OPTIONS_MASK;
#endif

      metric = ls->metric & METRIC_MASK;
      options |= ORTA_ASBR;
Ondřej Filip's avatar
Ondřej Filip committed
665 666
      type = ORT_ROUTER;
    }
667

668 669 670
    /* 16.2. (1b) */
    if (metric == LSINFINITY)
      continue;
Ondřej Zajíček's avatar
Ondřej Zajíček committed
671

672 673 674 675 676
    /* 16.2. (4) */
    ip_addr abrip = ipa_from_rid(en->lsa.rt);
    abr = (ort *) fib_find(&oa->rtr, &abrip, MAX_PREFIX_LENGTH);
    if (!abr || !abr->n.type)
      continue;
Ondřej Filip's avatar
Ondřej Filip committed
677

678 679 680 681 682 683
    if (abr->n.metric1 == LSINFINITY)
      continue;

    if (!(abr->n.options & ORTA_ABR))
      continue;

684 685 686 687
    /* This check is not mentioned in RFC 2328 */
    if (abr->n.type != RTS_OSPF)
      continue;

688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
    /* 16.2. (5) */
    orta nf = {
      .type = RTS_OSPF_IA,
      .options = options,
      .metric1 = abr->n.metric1 + metric,
      .metric2 = LSINFINITY,
      .tag = 0,
      .rid = en->lsa.rt, /* ABR ID */
      .oa = oa,
      .ifa = abr->n.ifa,
      .nh = abr->n.nh
    };

    if (type == ORT_NET)
      ri_install_net(po, ip, pxlen, &nf);
    else
      ri_install_rt(oa, dst_rid, &nf);
Ondřej Filip's avatar
Ondřej Filip committed
705
  }
706
}
707 708

/* RFC 2328 16.3. examining summary-LSAs in transit areas */
Ondřej Filip's avatar
Ondřej Filip committed
709
static void
710
ospf_rt_sum_tr(struct ospf_area *oa)
Ondřej Filip's avatar
Ondřej Filip committed
711
{
712
  // struct proto *p = &oa->po->proto;
Ondřej Filip's avatar
Ondřej Filip committed
713
  struct proto_ospf *po = oa->po;
714 715
  struct ospf_area *bb = po->backbone;
  ip_addr ip, abrip;
Ondřej Filip's avatar
Ondřej Filip committed
716
  struct top_hash_entry *en;
717 718
  u32 dst_rid, metric, options;
  int pxlen = -1, type = -1;
719
  ort *re = NULL, *abr;
720

721 722

  if (!bb) return;
723

724
  WALK_SLIST(en, po->lsal)
Ondřej Filip's avatar
Ondřej Filip committed
725
  {
726 727 728 729
    if ((en->lsa.type != LSA_T_SUM_RT) && (en->lsa.type != LSA_T_SUM_NET))
      continue;

    if (en->domain != oa->areaid)
730
      continue;
731

732
    /* 16.3 (1a) */
Ondřej Filip's avatar
Ondřej Filip committed
733 734
    if (en->lsa.age == LSA_MAXAGE)
      continue;
735

736
    /* 16.3 (2) */
737
    if (en->lsa.rt == po->router_id)
Ondřej Filip's avatar
Ondřej Filip committed
738 739 740 741
      continue;

    if (en->lsa.type == LSA_T_SUM_NET)
    {
742 743 744 745 746 747
#ifdef OSPFv2
      struct ospf_lsa_sum *ls = en->lsa_body;
      pxlen = ipa_mklen(ls->netmask);
      ip = ipa_and(ipa_from_u32(en->lsa.id), ls->netmask);
#else /* OSPFv3 */
      u8 pxopts;
748
      u16 rest;
749
      struct ospf_lsa_sum_net *ls = en->lsa_body;
750
      lsa_get_ipv6_prefix(ls->prefix, &ip, &pxlen, &pxopts, &rest);
751

752 753 754 755 756 757 758
      if (pxopts & OPT_PX_NU)
	continue;
#endif

      metric = ls->metric & METRIC_MASK;
      options = 0;
      type = ORT_NET;
759
      re = fib_find(&po->rtf, &ip, pxlen);
Ondřej Filip's avatar
Ondřej Filip committed
760
    }
761
    else // en->lsa.type == LSA_T_SUM_RT
Ondřej Filip's avatar
Ondřej Filip committed
762
    {
763 764 765 766 767 768 769 770 771 772 773
#ifdef OSPFv2
      struct ospf_lsa_sum *ls = en->lsa_body;
      dst_rid = en->lsa.id;
      options = 0;
#else /* OSPFv3 */
      struct ospf_lsa_sum_rt *ls = en->lsa_body;
      dst_rid = ls->drid; 
      options = ls->options & OPTIONS_MASK;
#endif

      ip = ipa_from_rid(dst_rid);
774
      pxlen = MAX_PREFIX_LENGTH;
775 776
      metric = ls->metric & METRIC_MASK;
      options |= ORTA_ASBR;
Ondřej Filip's avatar
Ondřej Filip committed
777
      type = ORT_ROUTER;
778
      re = fib_find(&bb->rtr, &ip, pxlen);
Ondřej Filip's avatar
Ondřej Filip committed
779 780
    }

781 782 783
    /* 16.3 (1b) */ 
    if (metric == LSINFINITY) 
      continue; 
784

785 786 787
    /* 16.3 (3) */
    if (!re || !re->n.type)
      continue;
788

789 790
    if (re->n.oa->areaid != 0)
      continue;
791

792 793
    if ((re->n.type != RTS_OSPF) && (re->n.type != RTS_OSPF_IA))
      continue;
794

795 796 797 798 799
    /* 16.3. (4) */
    abrip = ipa_from_rid(en->lsa.rt);
    abr = fib_find(&oa->rtr, &abrip, MAX_PREFIX_LENGTH);
    if (!abr || !abr->n.type)
      continue;
800

801
    metric = abr->n.metric1 + metric; /* IAC */
Ondřej Filip's avatar
Ondřej Filip committed
802

803 804
    /* 16.3. (5) */
    if (metric <= re->n.metric1)
Ondřej Filip's avatar
Ondřej Filip committed
805
    {
806 807 808 809 810
      /* We want to replace the next-hop even if the metric is equal
	 to replace a virtual next-hop through vlink with a real one */
      re->n.metric1 = metric;
      re->n.nh = abr->n.nh;
      re->n.ifa = abr->n.ifa;
Ondřej Filip's avatar
Ondřej Filip committed
811 812
    }
  }
813
}
814

815 816 817 818 819
/* RFC 2328 G.3 - incomplete resolution of virtual next hops */
static void
ospf_cleanup_vlinks(struct proto_ospf *po)
{
  FIB_WALK(&po->rtf, nftmp)
Ondřej Filip's avatar
Ondřej Filip committed
820
  {
821 822 823
    ort *nf = (ort *) nftmp;
    if (nf->n.type && (nf->n.ifa->type == OSPF_IT_VLINK))
      fill_ri(&nf->n);
824
  }
825
  FIB_WALK_END;
826 827
}

Ondřej Filip's avatar
Ondřej Filip committed
828
/**
829
 * ospf_ext_spf - calculate external paths
Ondřej Filip's avatar
Ondřej Filip committed
830 831 832
 * @po: protocol
 *
 * After routing table for any area is calculated, calculation of external
833
 * path is invoked. This process is described in 16.4 of RFC 2328.
Ondřej Filip's avatar
Ondřej Filip committed
834 835
 * Inter- and Intra-area paths are always prefered over externals.
 */
836
static void
837
ospf_ext_spf(struct proto_ospf *po)
838
{
839
  ort *nf1, *nf2;
840 841
  orta nfa;
  struct top_hash_entry *en;
Ondřej Filip's avatar
Ondřej Filip committed
842
  struct proto *p = &po->proto;
843
  struct ospf_lsa_ext *le;
844 845
  int pxlen, ebit, rt_fwaddr_valid;
  ip_addr ip, nh, rtid, rt_fwaddr;
846
  struct ospf_iface *nhi = NULL;
847
  u32 br_metric, rt_metric, rt_tag;
Ondřej Filip's avatar
Ondřej Filip committed
848
  struct ospf_area *atmp;
849

850
  OSPF_TRACE(D_EVENTS, "Starting routing table calculation for ext routes");
851

852
  WALK_SLIST(en, po->lsal)
853
  {
854
    /* 16.4. (1) */
Ondřej Filip's avatar
Ondřej Filip committed
855 856
    if (en->lsa.type != LSA_T_EXT)
      continue;
857

Ondřej Filip's avatar
Ondřej Filip committed
858 859
    if (en->lsa.age == LSA_MAXAGE)
      continue;
860 861

    /* 16.4. (2) */
862
    if (en->lsa.rt == po->router_id)
Ondřej Filip's avatar
Ondřej Filip committed
863
      continue;
864

865 866
    DBG("%s: Working on LSA. ID: %R, RT: %R, Type: %u\n",
	p->name, en->lsa.id, en->lsa.rt, en->lsa.type);
867

868 869 870 871 872 873
    le = en->lsa_body;

    rt_metric = le->metric & METRIC_MASK;
    ebit = le->metric & LSA_EXT_EBIT;

    if (rt_metric == LSINFINITY)
Ondřej Filip's avatar
Ondřej Filip committed
874
      continue;
875 876

#ifdef OSPFv2
Ondřej Filip's avatar
Ondřej Filip committed
877
    ip = ipa_and(ipa_from_u32(en->lsa.id), le->netmask);
878 879 880 881 882 883
    pxlen = ipa_mklen(le->netmask);
    rt_fwaddr = le->fwaddr;
    rt_fwaddr_valid = !ipa_equal(rt_fwaddr, IPA_NONE);
    rt_tag = le->tag;
#else /* OSPFv3 */
    u8 pxopts;
884
    u16 rest;
885
    u32 *buf = le->rest;
886
    buf = lsa_get_ipv6_prefix(buf, &ip, &pxlen, &pxopts, &rest);
887 888 889 890 891 892

    if (pxopts & OPT_PX_NU)
      continue;

    rt_fwaddr_valid = le->metric & LSA_EXT_FBIT;
    if (rt_fwaddr_valid)
893
      buf = lsa_get_ipv6_addr(buf, &rt_fwaddr);
894 895 896 897 898 899 900 901 902 903
    else 
      rt_fwaddr = IPA_NONE;

    if (le->metric & LSA_EXT_TBIT)
      rt_tag = *buf++;
    else
      rt_tag = 0;
#endif

    if (pxlen < 0)
904
    {
Ondřej Zajíček's avatar
Ondřej Zajíček committed
905 906
      log(L_WARN "%s: Invalid mask in LSA (Type: %04x, Id: %R, Rt: %R)",
	  p->name, en->lsa.type, en->lsa.id, en->lsa.rt);
907
      continue;
908
    }
909 910
    nhi = NULL;
    nh = IPA_NONE;
911

912 913
    /* 16.4. (3) */
    rtid = ipa_from_rid(en->lsa.rt);
Ondřej Filip's avatar
Ondřej Filip committed
914 915 916
    nf1 = NULL;
    WALK_LIST(atmp, po->area_list)
    {
917 918 919 920 921 922 923 924 925
      ort *nfh = fib_find(&atmp->rtr, &rtid, MAX_PREFIX_LENGTH);
      if (!nfh || !nfh->n.type)
	continue;

      if (nfh->n.metric1 == LSINFINITY)
	continue;

      if (!nf1 || ri_better_asbr(po, &nfh->n, &nf1->n))
	nf1 = nfh;
Ondřej Filip's avatar
Ondřej Filip committed
926 927 928
    }

    if (!nf1)
929
      continue;			/* No AS boundary router found */
930

931
    if (!(nf1->n.options & ORTA_ASBR))
932
      continue;			/* It is not ASBR */
933

934
    if (!rt_fwaddr_valid)
935
    {
936
      nf2 = nf1;
937 938
      nh = nf1->n.nh;
      nhi = nf1->n.ifa;
939
      br_metric = nf1->n.metric1;
940
    }
941
    else
942
    {
943
      /* FIXME: what about more specific dummy route? */
944
      nf2 = fib_route(&po->rtf, rt_fwaddr, MAX_PREFIX_LENGTH);
945
      if (!nf2) /* nf2->n.type is checked later */
Ondřej Filip's avatar
Ondřej Filip committed
946
	continue;
Ondřej Filip's avatar
Ondřej Filip committed
947

948 949 950 951 952
      if (nf2->n.metric1 == LSINFINITY)
        continue;

      if ((nf2->n.type != RTS_OSPF) && (nf2->n.type != RTS_OSPF_IA))
	continue;
Ondřej Filip's avatar
Ondřej Filip committed
953

954 955 956
      /* If nh is zero, it is a device route */
      nh = ipa_nonzero(nf2->n.nh) ? nf2->n.nh : rt_fwaddr;
      nhi = nf2->n.ifa;
957
      br_metric = nf2->n.metric1;
958
    }
959

960 961 962 963 964 965 966 967 968 969 970 971 972
    if (ebit)
    {
      nfa.type = RTS_OSPF_EXT2;
      nfa.metric1 = br_metric;
      nfa.metric2 = rt_metric;
    }
    else
    {
      nfa.type = RTS_OSPF_EXT1;
      nfa.metric1 = br_metric + rt_metric;
      nfa.metric2 = LSINFINITY;
    }

973 974 975
    /* Mark the LSA as reachable */
    en->color = INSPF;

976 977 978
    /* Whether the route is preferred in route selection according to 16.4.1 */
    nfa.options = epath_preferred(&nf2->n) ? ORTA_PREF : 0;

979
    nfa.tag = rt_tag;
980
    nfa.rid = en->lsa.rt;
981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
    nfa.oa = nf1->n.oa; /* undefined in RFC 2328 */
    nfa.ifa = nhi;
    nfa.nh = nh;

    ri_install_ext(po, ip, pxlen, &nfa);
  }
}

/* RFC 2328 16.7. p2 - find new/lost vlink endpoints */
static void
ospf_check_vlinks(struct proto_ospf *po)
{
  struct proto *p = &po->proto;

  struct ospf_iface *iface;
  WALK_LIST(iface, po->iface_list)
  {
    if (iface->type == OSPF_IT_VLINK)
    {
      struct top_hash_entry *tmp;
      tmp = ospf_hash_find_rt(po->gr, iface->voa->areaid, iface->vid);

      if (tmp && (tmp->color == INSPF)
	  && (tmp->dist < LSINFINITY)
	  && ipa_nonzero(tmp->lb))
      {
        if ((iface->state != OSPF_IS_PTP)
	    || (iface->vifa != tmp->nhi)
	    || !ipa_equal(iface->vip, tmp->lb))
        {
          OSPF_TRACE(D_EVENTS, "Vlink peer %R found", tmp->lsa.id);
          ospf_iface_sm(iface, ISM_DOWN);
	  iface->vifa = tmp->nhi;
          iface->iface = tmp->nhi->iface;
	  iface->addr = tmp->nhi->addr;
	  iface->sk = tmp->nhi->sk;
	  iface->cost = tmp->dist;
          iface->vip = tmp->lb;
          ospf_iface_sm(iface, ISM_UP);
        }
	else if ((iface->state == OSPF_IS_PTP) && (iface->cost != tmp->dist))
	{
	  iface->cost = tmp->dist;
	  schedule_rt_lsa(po->backbone);
	}
      }
      else
      {
        if (iface->state > OSPF_IS_DOWN)
        {
          OSPF_TRACE(D_EVENTS, "Vlink peer %R lost", iface->vid);
	  ospf_iface_sm(iface, ISM_DOWN);
        }
      }
    }
  }
}
/**
 * ospf_rt_spf - calculate internal routes
 * @po: OSPF protocol
 *
 * Calculation of internal paths in an area is described in 16.1 of RFC 2328.
 * It's based on Dijkstra's shortest path tree algorithms.
 * This function is invoked from ospf_disp().
 */
void
ospf_rt_spf(struct proto_ospf *po)
{
  struct proto *p = &po->proto;
  struct ospf_area *oa;
  ort *ri;
  struct area_net *anet;

  if (po->areano == 0) return;

  OSPF_TRACE(D_EVENTS, "Starting routing table calculation");

  /* 16. (1) - Invalidate old routing table */
  FIB_WALK(&po->rtf, nftmp)
  {
    ri = (ort *) nftmp;
    memcpy(&ri->o, &ri->n, sizeof(orta));	/* Backup old data */
    fill_ri(&ri->n);
1064
  }
1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
  FIB_WALK_END;


  WALK_LIST(oa, po->area_list)
  {
    FIB_WALK(&oa->rtr, nftmp)
    {
      ri = (ort *) nftmp;
      memcpy(&ri->o, &ri->n, sizeof(orta));	/* Backup old data */
      fill_ri(&ri->n);
    }
    FIB_WALK_END;

    FIB_WALK(&oa->net_fib, nftmp)
    {
      anet = (struct area_net *) nftmp;
      anet->active = 0;
      anet->metric = 1;
    }
    FIB_WALK_END;

    /* 16. (2) */
    ospf_rt_spfa(oa);
  }

  /* 16. (3) */
  if ((po->areano == 1) || (!po->backbone))
    ospf_rt_sum(HEAD(po->area_list));
  else
    ospf_rt_sum(po->backbone);

  /* 16. (4) */
  WALK_LIST(oa, po->area_list)
    if (oa->trcap && (oa->areaid != 0))
      ospf_rt_sum_tr(oa);

  /* G.3 */
  if (po->backbone && po->backbone->rt &&
      (((struct ospf_lsa_rt *) po->backbone->rt->lsa_body)->options & OPT_RT_V))
    ospf_cleanup_vlinks(po);

  /* 16. (5) */
  ospf_ext_spf(po);

  rt_sync(po);
1110

1111
  po->calcrt = 0;
1112 1113
}

1114
/* Add LSA into list of candidates in Dijkstra's algorithm */
1115
static void
Ondřej Filip's avatar
Ondřej Filip committed
1116
add_cand(list * l, struct top_hash_entry *en, struct top_hash_entry *par,
1117
	 u32 dist, struct ospf_area *oa)
1118
{
Ondřej Filip's avatar
Ondřej Filip committed
1119 1120
  node *prev, *n;
  int added = 0;
1121
  struct top_hash_entry *act;
1122

1123
  /* 16.1. (2b) */
Ondřej Filip's avatar
Ondřej Filip committed
1124 1125 1126 1127
  if (en == NULL)
    return;
  if (en->lsa.age == LSA_MAXAGE)
    return;
Ondřej Filip's avatar
Ondřej Filip committed
1128

1129 1130 1131 1132 1133 1134 1135 1136 1137
#ifdef OSPFv3
  if (en->lsa.type == LSA_T_RT)
    {
      struct ospf_lsa_rt *rt = en->lsa_body;
      if (!(rt->options & OPT_V6) || !(rt->options & OPT_R))
	return;
    }
#endif

1138
  /* 16.1. (2c) */
Ondřej Filip's avatar
Ondřej Filip committed
1139 1140
  if (en->color == INSPF)
    return;
1141

1142
  /* 16.1. (2d) */
Ondřej Filip's avatar
Ondřej Filip committed
1143 1144
  if (dist >= en->dist)
    return;
1145
  /*
1146
   * The line above (=) is not a bug, but we don't support multiple
1147
   * next hops. I'll start as soon as nest will
1148
   */
Ondřej Filip's avatar
Ondřej Filip committed
1149

1150
  /* We should check whether there is a reverse link from en to par, */
Ondřej Filip's avatar
Ondřej Filip committed
1151 1152 1153
  if (!link_back(oa, en, par))
    return;

1154 1155 1156 1157 1158 1159 1160
  if (!calc_next_hop(oa, en, par))
  {
    log(L_WARN "Cannot find next hop for LSA (Type: %04x, Id: %R, Rt: %R)",
	en->lsa.type, en->lsa.id, en->lsa.rt);
    return;
  }

1161 1162
  DBG("     Adding candidate: rt: %R, id: %R, type: %u\n",
      en->lsa.rt, en->lsa.id, en->lsa.type);
Ondřej Filip's avatar
Ondřej Filip committed
1163

1164 1165
  if (en->color == CANDIDATE)
  {				/* We found a shorter path */
1166
    rem_node(&en->cn);
1167
  }
Ondřej Filip's avatar
Ondřej Filip committed
1168 1169
  en->dist = dist;
  en->color = CANDIDATE;
1170

Ondřej Filip's avatar
Ondřej Filip committed
1171
  prev = NULL;
1172

Ondřej Filip's avatar
Ondřej Filip committed
1173
  if (EMPTY_LIST(*l))
1174
  {
Ondřej Filip's avatar
Ondřej Filip committed
1175
    add_head(l, &en->cn);
1176 1177 1178
  }
  else
  {
Ondřej Filip's avatar
Ondřej Filip committed
1179
    WALK_LIST(n, *l)
1180
    {
Ondřej Filip's avatar
Ondřej Filip committed
1181 1182 1183
      act = SKIP_BACK(struct top_hash_entry, cn, n);
      if ((act->dist > dist) ||
	  ((act->dist == dist) && (act->lsa.type == LSA_T_NET)))
1184
      /* FIXME - shouldn't be here LSA_T_RT ??? */
1185
      {
Ondřej Filip's avatar
Ondřej Filip committed
1186 1187 1188 1189 1190 1191
	if (prev == NULL)
	  add_head(l, &en->cn);
	else
	  insert_node(&en->cn, prev);
	added = 1;
	break;
1192
      }
Ondřej Filip's avatar
Ondřej Filip committed
1193
      prev = n;
1194 1195
    }

Ondřej Filip's avatar
Ondřej Filip committed
1196
    if (!added)
1197
    {
Ondřej Filip's avatar
Ondřej Filip committed
1198
      add_tail(l, &en->cn);
1199 1200 1201
    }
  }
}
1202

1203 1204 1205 1206 1207

static inline int
match_dr(struct ospf_iface *ifa, struct top_hash_entry *en)
{
#ifdef OSPFv2
1208
  return (ifa->drid == en->lsa.rt) && (ipa_to_u32(ifa->drip) == en->lsa.id);
1209 1210 1211 1212 1213
#else /* OSPFv3 */
  return (ifa->drid == en->lsa.rt) && (ifa->dr_iface_id == en->lsa.id);
#endif
}

1214
static int
1215 1216
calc_next_hop(struct ospf_area *oa, struct top_hash_entry *en,
	      struct top_hash_entry *par)
1217
{
1218
  // struct proto *p = &oa->po->proto;
1219
  struct ospf_neighbor *neigh, *m;
Ondřej Filip's avatar
Ondřej Filip committed
1220
  struct proto_ospf *po = oa->po;
1221
  struct ospf_iface *ifa;
1222

1223
  /* 16.1.1. The next hop calculation */
1224
  DBG("     Next hop called.\n");
1225
  if (ipa_zero(par->nh))
1226
  {
1227
    u32 rid = en->lsa.rt;
1228 1229
    DBG("     Next hop calculating for id: %R rt: %R type: %u\n",
	en->lsa.id, en->lsa.rt, en->lsa.type);
1230

1231 1232 1233 1234 1235 1236 1237 1238 1239
    /* 
     * There are three cases:
     * 1) en is a local network (and par is root)
     * 2) en is a ptp or ptmp neighbor (and par is root)
     * 3) en is a bcast or nbma neighbor (and par is local network)
     */

    /* The first case - local network */
    if ((en->lsa.type == LSA_T_NET) && (par == oa->rt))
1240
    {
1241 1242
      WALK_LIST(ifa, po->iface_list)
	if (match_dr(ifa, en))
Ondřej Filip's avatar
Ondřej Filip committed
1243
	  {
1244 1245 1246
	    en->nh = IPA_NONE;
	    en->nhi = ifa;
	    return 1;
Ondřej Filip's avatar
Ondřej Filip committed
1247
	  }
1248
      return 0;
1249
    }
1250

1251 1252
    /* The second case - ptp or ptmp neighbor */
    if ((en->lsa.type == LSA_T_RT) && (par == oa->rt))
1253
    {
1254 1255 1256 1257 1258 1259 1260
      /*
       * We don't know which iface was used to reach this neighbor
       * (there might be more parallel ifaces) so we will find
       * the best PTP iface with given fully adjacent neighbor.
       */
      neigh = NULL;
      WALK_LIST(ifa, po->iface_list)
1261
	if ((ifa->type == OSPF_IT_PTP) || (ifa->type == OSPF_IT_VLINK))
1262
	{
1263 1264 1265 1266 1267 1268
	  m = find_neigh(ifa, rid);
	  if (m && (m->state == NEIGHBOR_FULL))
	  {
	    if (!neigh || (m->ifa->cost < neigh->ifa->cost))
	      neigh = m;
	  }
1269
	}
1270 1271 1272 1273 1274 1275 1276

      if (!neigh)
	return 0;

      en->nh = neigh->ip;
      en->nhi = neigh->ifa;
      return 1;
1277
    }
1278

1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305
    /* The third case - bcast or nbma neighbor */
    if ((en->lsa.type == LSA_T_RT) && (par->lsa.type == LSA_T_NET))
    {
      /* par->nhi should be defined from parent's calc_next_hop() */
      if (!par->nhi)
	goto bad;

#ifdef OSPFv2
      /*
       * In this case, next-hop is the same as link-back, which is
       * already computed in link_back().
       */
      if (ipa_zero(en->lb))
	goto bad;

      en->nh = en->lb;
      en->nhi = par->nhi;
      return 1;

#else /* OSPFv3 */
      /*
       * Next-hop is taken from lladdr field of Link-LSA, en->lb_id
       * is computed in link_back().
       */
      struct top_hash_entry *llsa;
      llsa = ospf_hash_find(po->gr, par->nhi->iface->index, en->lb_id, rid, LSA_T_LINK);

1306
      if (!llsa || ipa_zero(llsa->lladdr))
1307 1308 1309 1310 1311 1312 1313 1314 1315
	return 0;

      en->nh = llsa->lladdr;
      en->nhi = par->nhi;
      return 1;
#endif
    }

  bad:
1316 1317 1318
    /* Probably bug or some race condition, we log it */
    log(L_ERR "Unexpected case in next hop calculation");
    return 0;
1319
  }
1320

1321
  en->nh = par->nh;
1322 1323
  en->nhi = par->nhi;
  return 1;
1324
}
1325 1326 1327 1328 1329 1330

static void
rt_sync(struct proto_ospf *po)
{
  struct proto *p = &po->proto;
  struct fib_iterator fit;
Ondřej Filip's avatar
Ondřej Filip committed
1331
  struct fib *fib = &po->rtf;
1332
  ort *nf;
1333
  struct ospf_area *oa, *oaa;
Ondřej Filip's avatar
Ondřej Filip committed
1334
  struct area_net *anet;
1335
  int flush;
Ondřej Filip's avatar
Ondřej Filip committed
1336

1337 1338 1339
  /* This is used for forced reload of routes */
  int reload = (po->calcrt == 2);

Ondřej Filip's avatar
Ondřej Filip committed
1340
  OSPF_TRACE(D_EVENTS, "Starting routing table synchronisation");
1341 1342 1343

  DBG("Now syncing my rt table with nest's\n");
  FIB_ITERATE_INIT(&fit, fib);
Ondřej Filip's avatar
Ondřej Filip committed
1344
again1:
1345 1346 1347
  FIB_ITERATE_START(fib, &fit, nftmp)
  {
    nf = (ort *) nftmp;
1348
    check_sum_lsa(po, nf, ORT_NET);
1349
    if (reload || memcmp(&nf->n, &nf->o, sizeof(orta)))
1350
    {				/* Some difference */
1351 1352 1353 1354 1355 1356 1357 1358
      net *ne = net_get(p->table, nf->fn.prefix, nf->fn.pxlen);

      rta a0 = {
	.proto = p,
	.source = nf->n.type,
	.scope = SCOPE_UNIVERSE,
	.cast = RTC_UNICAST
      };
1359

1360 1361
      if (!nf->n.type)
	goto remove1;
1362

1363 1364 1365
      /* The distance is unreachable (or farther) */
      if (nf->n.metric1 >= LSINFINITY)
	goto remove1;
1366