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

#include "ospf.h"

11
void
12
flush_lsa(struct top_hash_entry *en, struct ospf_area *oa)
13
{
Ondřej Filip's avatar
Ondřej Filip committed
14 15
  struct proto *p=&oa->po->proto;
  OSPF_TRACE(D_EVENTS, "Going to remove node Type: %u, Id: %I, Rt: %I, Age: %u",
16
    en->lsa.type, en->lsa.id, en->lsa.rt, en->lsa.age);
17
  s_rem_node(SNODE en);
18 19
  if(en->lsa_body!=NULL) mb_free(en->lsa_body);
  en->lsa_body=NULL;
20
  ospf_hash_delete(oa->gr,en);
21 22
}

23 24 25 26
/**
 * ospf_age
 * @oa: ospf area
 *
27 28 29 30
 * This function is periodicaly invoked from area_disp(). It computes the new
 * age of all LSAs and old (@age is higher than %LSA_MAXAGE) LSAs are flushed
 * whenever possible. If an LSA originated by the router itself is older
 * than %LSREFRESHTIME a new instance is originated.
31
 *
32 33
 * The RFC says that a router should check the checksum of every LSA to detect
 * hardware problems. BIRD does not do this to minimalize CPU utilization.
34
 *
35
 * If routing table calculation is scheduled, it also invalidates the old routing
36
 * table calculation results.
37
 */
38
void
Ondřej Filip's avatar
Ondřej Filip committed
39
ospf_age(struct ospf_area *oa)
40
{
41
  struct proto *p=&oa->po->proto;
Ondřej Filip's avatar
Ondřej Filip committed
42
  struct proto_ospf *po=(struct proto_ospf *)p;
Ondřej Filip's avatar
Ondřej Filip committed
43 44 45
  struct top_hash_entry *en,*nxt;
  int flush=can_flush_lsa(oa);

Ondřej Filip's avatar
Ondřej Filip committed
46 47
  OSPF_TRACE(D_EVENTS, "Running ospf_age");

Ondřej Filip's avatar
Ondřej Filip committed
48
  WALK_SLIST_DELSAFE(en,nxt,oa->lsal)
49
  {
50 51 52 53 54 55 56 57 58
    if(oa->calcrt)
    {
      en->color=OUTSPF;
      en->dist=LSINFINITY;
      en->nhi=NULL;
      en->nh=ipa_from_u32(0);
      DBG("Infinitying Type: %u, Id: %I, Rt: %I\n", en->lsa.type, en->lsa.id,
        en->lsa.rt);
    }
Ondřej Filip's avatar
Ondřej Filip committed
59 60 61
    if(en->lsa.age==LSA_MAXAGE)
    {
      if(flush) flush_lsa(en,oa);
Ondřej Filip's avatar
Ondřej Filip committed
62
      continue;
Ondřej Filip's avatar
Ondřej Filip committed
63
    }
64
    if((en->lsa.rt==p->cf->global->router_id)&&(en->lsa.age>=LSREFRESHTIME))
Ondřej Filip's avatar
Ondřej Filip committed
65
    {
Ondřej Filip's avatar
Ondřej Filip committed
66 67
       OSPF_TRACE(D_EVENTS, "Refreshing my LSA: Type: %u, Id: %I, Rt: %I",
         en->lsa.type, en->lsa.id, en->lsa.rt);
Ondřej Filip's avatar
Ondřej Filip committed
68 69
       en->lsa.sn++;
       en->lsa.age=0;
70 71
       en->inst_t=now;
       en->ini_age=0;
72
       lsasum_calculate(&en->lsa,en->lsa_body,po);
73
       ospf_lsupd_flood(NULL,NULL,&en->lsa,NULL,oa,1);
74
       continue;
Ondřej Filip's avatar
Ondřej Filip committed
75
    }
76
    if((en->lsa.age=(en->ini_age+(now-en->inst_t)))>=LSA_MAXAGE)
Ondřej Filip's avatar
Ondřej Filip committed
77
    {
78 79 80 81 82
      if(flush)
      {
        flush_lsa(en,oa);
        schedule_rtcalc(oa);
      }
Ondřej Filip's avatar
Ondřej Filip committed
83 84
      else en->lsa.age=LSA_MAXAGE;
    }
85 86 87
  }
}

88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
void
htonlsah(struct ospf_lsa_header *h, struct ospf_lsa_header *n)
{
  n->age=htons(h->age);
  n->options=h->options;
  n->type=h->type;
  n->id=htonl(h->id);
  n->rt=htonl(h->rt);
  n->sn=htonl(h->sn);
  n->checksum=htons(h->checksum);
  n->length=htons(h->length);
};

void
ntohlsah(struct ospf_lsa_header *n, struct ospf_lsa_header *h)
{
  h->age=ntohs(n->age);
  h->options=n->options;
  h->type=n->type;
  h->id=ntohl(n->id);
  h->rt=ntohl(n->rt);
  h->sn=ntohl(n->sn);
  h->checksum=ntohs(n->checksum);
  h->length=ntohs(n->length);
};

void
htonlsab(void *h, void *n, u8 type, u16 len)
{
  unsigned int i;
  switch(type)
  {
    case LSA_T_RT:
    {
      struct ospf_lsa_rt *hrt, *nrt;
      struct ospf_lsa_rt_link *hrtl,*nrtl;
124
      u16 links;
125 126 127

      nrt=n;
      hrt=h;
128
      links=hrt->links;
129

130
      nrt->veb.byte=hrt->veb.byte;
131 132 133 134
      nrt->padding=0;
      nrt->links=htons(hrt->links);
      nrtl=(struct ospf_lsa_rt_link *)(nrt+1);
      hrtl=(struct ospf_lsa_rt_link *)(hrt+1);
135
      for(i=0;i<links;i++)
136 137 138 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 164 165 166
      {
        (nrtl+i)->id=htonl((hrtl+i)->id);
        (nrtl+i)->data=htonl((hrtl+i)->data);
        (nrtl+i)->type=(hrtl+i)->type;
        (nrtl+i)->notos=(hrtl+i)->notos;
        (nrtl+i)->metric=htons((hrtl+i)->metric);
      }
      break;
    }
    case LSA_T_NET:
    {
      u32 *hid,*nid;

      nid=n;
      hid=h;

      for(i=0;i<(len/sizeof(u32));i++)
      {
        *(nid+i)=htonl(*(hid+i));
      }
      break;
    }
    case LSA_T_SUM_NET:
    case LSA_T_SUM_RT:
    {
      struct ospf_lsa_summ *hs, *ns;
      struct ospf_lsa_summ_net *hn, *nn;

      hs=h;
      ns=n;

167 168
      ns->netmask=hs->netmask;
      ipa_hton(ns->netmask);
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189

      hn=(struct ospf_lsa_summ_net *)(hs+1);
      nn=(struct ospf_lsa_summ_net *)(ns+1);

      for(i=0;i<((len-sizeof(struct ospf_lsa_summ))/
        sizeof(struct ospf_lsa_summ_net));i++)
      {
        (nn+i)->tos=(hn+i)->tos;
	(nn+i)->metric=htons((hn+i)->metric);
	(nn+i)->padding=0;
      }
      break;
    }
    case LSA_T_EXT:
    {
      struct ospf_lsa_ext *he, *ne;
      struct ospf_lsa_ext_tos *ht, *nt;

      he=h;
      ne=n;

190 191
      ne->netmask=he->netmask;
      ipa_hton(ne->netmask);
192 193 194 195 196 197 198 199 200 201

      ht=(struct ospf_lsa_ext_tos *)(he+1);
      nt=(struct ospf_lsa_ext_tos *)(ne+1);

      for(i=0;i<((len-sizeof(struct ospf_lsa_ext))/
        sizeof(struct ospf_lsa_ext_tos));i++)
      {
        (nt+i)->etos=(ht+i)->etos;
        (nt+i)->padding=0;
        (nt+i)->metric=htons((ht+i)->metric);
202 203
        (nt+i)->fwaddr=(ht+i)->fwaddr;
        ipa_hton((nt+i)->fwaddr);
204 205 206 207
        (nt+i)->tag=htonl((ht+i)->tag);
      }
      break;
    }
208
    default: bug("(hton): Unknown LSA");
209 210 211 212 213 214 215 216 217 218 219 220 221
  }
};

void
ntohlsab(void *n, void *h, u8 type, u16 len)
{
  unsigned int i;
  switch(type)
  {
    case LSA_T_RT:
    {
      struct ospf_lsa_rt *hrt, *nrt;
      struct ospf_lsa_rt_link *hrtl,*nrtl;
222
      u16 links;
223 224 225 226

      nrt=n;
      hrt=h;

227
      hrt->veb.byte=nrt->veb.byte;
228
      hrt->padding=0;
229
      links=hrt->links=ntohs(nrt->links);
230 231
      nrtl=(struct ospf_lsa_rt_link *)(nrt+1);
      hrtl=(struct ospf_lsa_rt_link *)(hrt+1);
232
      for(i=0;i<links;i++)
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
      {
        (hrtl+i)->id=ntohl((nrtl+i)->id);
        (hrtl+i)->data=ntohl((nrtl+i)->data);
        (hrtl+i)->type=(nrtl+i)->type;
        (hrtl+i)->notos=(nrtl+i)->notos;
        (hrtl+i)->metric=ntohs((nrtl+i)->metric);
      }
      break;
    }
    case LSA_T_NET:
    {
      u32 *hid,*nid;

      hid=h;
      nid=n;

      for(i=0;i<(len/sizeof(u32));i++)
      {
        *(hid+i)=ntohl(*(nid+i));
      }
      break;
    }
    case LSA_T_SUM_NET:
    case LSA_T_SUM_RT:
    {
      struct ospf_lsa_summ *hs, *ns;
      struct ospf_lsa_summ_net *hn, *nn;

      hs=h;
      ns=n;

264 265
      hs->netmask=ns->netmask;
      ipa_ntoh(hs->netmask);
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286

      hn=(struct ospf_lsa_summ_net *)(hs+1);
      nn=(struct ospf_lsa_summ_net *)(ns+1);

      for(i=0;i<((len-sizeof(struct ospf_lsa_summ))/
        sizeof(struct ospf_lsa_summ_net));i++)
      {
        (hn+i)->tos=(nn+i)->tos;
	(hn+i)->metric=ntohs((nn+i)->metric);
	(hn+i)->padding=0;
      }
      break;
    }
    case LSA_T_EXT:
    {
      struct ospf_lsa_ext *he, *ne;
      struct ospf_lsa_ext_tos *ht, *nt;

      he=h;
      ne=n;

287 288
      he->netmask=ne->netmask;
      ipa_ntoh(he->netmask);
289 290 291 292 293 294 295 296 297 298

      ht=(struct ospf_lsa_ext_tos *)(he+1);
      nt=(struct ospf_lsa_ext_tos *)(ne+1);

      for(i=0;i<((len-sizeof(struct ospf_lsa_ext))/
        sizeof(struct ospf_lsa_ext_tos));i++)
      {
        (ht+i)->etos=(nt+i)->etos;
        (ht+i)->padding=0;
        (ht+i)->metric=ntohs((nt+i)->metric);
299 300
        (ht+i)->fwaddr=(nt+i)->fwaddr;
        ipa_ntoh((ht+i)->fwaddr);
301 302 303 304
        (ht+i)->tag=ntohl((nt+i)->tag);
      }
      break;
    }
305
    default: bug("(ntoh): Unknown LSA");
306 307 308
  }
};

309 310 311 312 313 314 315 316 317
#define MODX 4102		/* larges signed value without overflow */

/* Fletcher Checksum -- Refer to RFC1008. */
#define MODX                 4102
#define LSA_CHECKSUM_OFFSET    15

/* FIXME This is VERY uneficient, I have huge endianity problems */
void
lsasum_calculate(struct ospf_lsa_header *h,void *body,struct proto_ospf *po)
318 319 320 321 322 323
{
  u16 length;
  
  length=h->length;

  htonlsah(h,h);
324
  htonlsab(body,body,h->type,length-sizeof(struct ospf_lsa_header));
325 326 327 328

  (void)lsasum_check(h,body,po);
  
  ntohlsah(h,h);
329
  ntohlsab(body,body,h->type,length-sizeof(struct ospf_lsa_header));
330 331 332 333 334 335 336 337
}

/*
 * Note, that this function expects that LSA is in big endianity
 * It also returns value in big endian
 */
u16
lsasum_check(struct ospf_lsa_header *h,void *body,struct proto_ospf *po)
338 339 340 341
{
  u8 *sp, *ep, *p, *q, *b;
  int c0 = 0, c1 = 0;
  int x, y;
342
  u16 length;
343

344
  b = body;
345
  sp = (char *) &h->options;
346
  length = ntohs(h->length)-2;
347
  h->checksum = 0;
348 349

  for (ep = sp + length; sp < ep; sp = q)
350 351 352 353
  {		/* Actually MODX is very large, do we need the for-cyclus? */
    q = sp + MODX;
    if (q > ep) q = ep;
    for (p = sp; p < q; p++)
354
    {
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
      /* 
       * I count with bytes from header and than from body
       * but if there is no body, it's appended to header
       * (probably checksum in update receiving) and I go on
       * after header
       */
      if((b==NULL) || (p<(u8 *)(h+1)))
      {
	      c0 += *p;
      }
      else
      {
	      c0 += *(b+(p-sp)-sizeof(struct ospf_lsa_header)+2);
      }

      c1 += c0;
371
    }
372 373 374
    c0 %= 255;
    c1 %= 255;
  }
375 376

  x = ((length - LSA_CHECKSUM_OFFSET) * c0 - c1) % 255;
377
  if (x <= 0) x += 255;
378
  y = 510 - c0 - x;
379
  if (y > 255) y -= 255;
380

381 382 383
  ((u8*)&h->checksum)[0] = x;
  ((u8*)&h->checksum)[1] = y;
  return h->checksum;
384 385
}

386 387
int
lsa_comp(struct ospf_lsa_header *l1, struct ospf_lsa_header *l2)
388
			/* Return codes from point of view of l1 */
389
{
390 391 392 393 394 395 396
  u32 sn1,sn2;

  sn1=l1->sn-LSA_INITSEQNO+1;
  sn2=l2->sn-LSA_INITSEQNO+1;

  if(sn1>sn2) return CMP_NEWER;
  if(sn1<sn2) return CMP_OLDER;
Ondřej Filip's avatar
Ondřej Filip committed
397

398
  if(l1->checksum!=l2->checksum)
Ondřej Filip's avatar
Ondřej Filip committed
399 400 401 402 403
    return l1->checksum<l2->checksum ? CMP_OLDER : CMP_NEWER;

  if((l1->age==LSA_MAXAGE)&&(l2->age!=LSA_MAXAGE)) return CMP_NEWER;
  if((l2->age==LSA_MAXAGE)&&(l1->age!=LSA_MAXAGE)) return CMP_OLDER;

Martin Mareš's avatar
Martin Mareš committed
404
  if(ABS(l1->age-l2->age)>LSA_MAXAGEDIFF)
Ondřej Filip's avatar
Ondřej Filip committed
405 406 407
    return l1->age<l2->age ? CMP_NEWER : CMP_OLDER;

  return CMP_SAME;
408 409
}

410 411 412
/**
 * lsa_install_new - install new LSA into database
 * @lsa: LSA header
Martin Mareš's avatar
Martin Mareš committed
413
 * @body: pointer to LSA body
414 415 416
 * @oa: current ospf_area
 *
 * This function ensures installing new LSA into LSA database. Old instance is
Ondřej Filip's avatar
Ondřej Filip committed
417
 * replaced. Several actions are taken to detect if new routing table
418 419
 * calculation is necessary. This is described in 13.2 of RFC 2328.
 */
420
struct top_hash_entry *
421
lsa_install_new(struct ospf_lsa_header *lsa, void *body, struct ospf_area *oa)
422
{
423
  /* LSA can be temporarrily, but body must be mb_allocated. */
424 425
  int change=0;
  unsigned i;
426 427 428 429 430 431 432 433 434
  struct top_hash_entry *en;

  if((en=ospf_hash_find_header(oa->gr,lsa))==NULL)
  {
    en=ospf_hash_get_header(oa->gr,lsa);
    change=1;
  }
  else
  {
435 436
    if((en->lsa.length!=lsa->length)||(en->lsa.options!=lsa->options)||
      ((en->lsa.age==LSA_MAXAGE)||(lsa->age==LSA_MAXAGE))) change=1;
437 438 439
    else
    {
      u8 *k=en->lsa_body,*l=body;
440
      for(i=0;i<(lsa->length-sizeof(struct ospf_lsa_header));i++)
441 442 443 444 445 446 447
      {
        if(*(k+i)!=*(l+i))
	{
	  change=1;
	  break;
	}
      }
448
    }
449
    s_rem_node(SNODE en);
450 451
  }

452 453 454
  DBG("Inst lsa: Id: %I, Rt: %I, Type: %u, Age: %u, Sum: %u, Sn: 0x%x\n",
    lsa->id, lsa->rt, lsa->type, lsa->age, lsa->checksum, lsa->sn);

455 456 457 458 459
  s_add_tail(&oa->lsal, SNODE en);
  en->inst_t=now;
  if(en->lsa_body!=NULL) mb_free(en->lsa_body);
  en->lsa_body=body;
  memcpy(&en->lsa,lsa,sizeof(struct ospf_lsa_header));
460
  en->ini_age=en->lsa.age;
461

462 463
  if(change)
  {
464
    schedule_rtcalc(oa);
465 466 467 468
  }
  
  return en;
}