bird/proto/ospf/lsalib.c

348 lines
7.9 KiB
C
Raw Normal View History

/*
* BIRD -- OSPF
*
* (c) 1999--2004 Ondrej Filip <feela@network.cz>
*
* Can be freely distributed and used under the terms of the GNU GPL.
*/
#include "ospf.h"
2000-05-03 06:19:41 +08:00
void
flush_lsa(struct top_hash_entry *en, struct proto_ospf *po)
2000-05-03 06:19:41 +08:00
{
struct proto *p = &po->proto;
2004-06-06 17:37:54 +08:00
OSPF_TRACE(D_EVENTS,
2009-07-23 22:51:28 +08:00
"Going to remove node Type: %u, Id: %R, Rt: %R, Age: %u, SN: 0x%x",
en->lsa.type, en->lsa.id, en->lsa.rt, en->lsa.age, en->lsa.sn);
2000-05-10 03:36:32 +08:00
s_rem_node(SNODE en);
2004-06-06 17:37:54 +08:00
if (en->lsa_body != NULL)
mb_free(en->lsa_body);
en->lsa_body = NULL;
ospf_hash_delete(po->gr, en);
2000-05-03 06:19:41 +08:00
}
/**
* ospf_age
* @po: ospf protocol
*
* This function is periodicaly invoked from ospf_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.
*
* 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.
*
* If routing table calculation is scheduled, it also invalidates the old routing
* table calculation results.
*/
2000-05-03 06:19:41 +08:00
void
ospf_age(struct proto_ospf *po)
2000-05-03 06:19:41 +08:00
{
struct proto *p = &po->proto;
2004-06-06 17:37:54 +08:00
struct top_hash_entry *en, *nxt;
int flush = can_flush_lsa(po);
2000-05-31 22:21:56 +08:00
if (po->cleanup) OSPF_TRACE(D_EVENTS, "Running ospf_age cleanup");
2002-03-04 23:52:40 +08:00
WALK_SLIST_DELSAFE(en, nxt, po->lsal)
2000-05-03 06:19:41 +08:00
{
if (po->cleanup)
{
2004-06-06 17:37:54 +08:00
en->color = OUTSPF;
en->dist = LSINFINITY;
en->nhi = NULL;
en->nh = IPA_NONE;
en->lb = IPA_NONE;
2009-07-23 22:51:28 +08:00
DBG("Infinitying Type: %u, Id: %R, Rt: %R\n", en->lsa.type,
en->lsa.id, en->lsa.rt);
}
2004-06-06 17:37:54 +08:00
if (en->lsa.age == LSA_MAXAGE)
2000-05-31 22:21:56 +08:00
{
2004-06-06 17:37:54 +08:00
if (flush)
flush_lsa(en, po);
2000-06-02 20:34:28 +08:00
continue;
2000-05-31 22:21:56 +08:00
}
2009-08-21 15:27:52 +08:00
if ((en->lsa.rt == p->cf->global->router_id) &&
(en->lsa.age >= LSREFRESHTIME))
2000-05-31 22:21:56 +08:00
{
2009-07-23 22:51:28 +08:00
OSPF_TRACE(D_EVENTS, "Refreshing my LSA: Type: %u, Id: %R, Rt: %R",
en->lsa.type, en->lsa.id, en->lsa.rt);
2004-06-06 17:37:54 +08:00
en->lsa.sn++;
en->lsa.age = 0;
en->inst_t = now;
en->ini_age = 0;
2004-06-07 02:45:08 +08:00
lsasum_calculate(&en->lsa, en->lsa_body);
2009-08-21 15:27:52 +08:00
ospf_lsupd_flood(po, NULL, NULL, &en->lsa, en->domain, 1);
2004-06-06 17:37:54 +08:00
continue;
2000-05-31 22:21:56 +08:00
}
2004-06-06 17:37:54 +08:00
if ((en->lsa.age = (en->ini_age + (now - en->inst_t))) >= LSA_MAXAGE)
2000-05-31 22:21:56 +08:00
{
2004-06-06 17:37:54 +08:00
if (flush)
{
flush_lsa(en, po);
schedule_rtcalc(po);
}
2004-06-06 17:37:54 +08:00
else
en->lsa.age = LSA_MAXAGE;
2000-05-31 22:21:56 +08:00
}
2000-05-03 06:19:41 +08:00
}
po->cleanup = 0;
2000-05-03 06:19:41 +08:00
}
void
htonlsah(struct ospf_lsa_header *h, struct ospf_lsa_header *n)
{
2004-06-06 17:37:54 +08:00
n->age = htons(h->age);
2009-08-21 15:27:52 +08:00
#ifdef OSPFv2
2004-06-06 17:37:54 +08:00
n->options = h->options;
2009-08-21 15:27:52 +08:00
#endif
2009-08-28 00:25:46 +08:00
n->type = htont(h->type);
2004-06-06 17:37:54 +08:00
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);
2009-09-08 23:06:47 +08:00
}
void
ntohlsah(struct ospf_lsa_header *n, struct ospf_lsa_header *h)
{
2004-06-06 17:37:54 +08:00
h->age = ntohs(n->age);
2009-08-21 15:27:52 +08:00
#ifdef OSPFv2
2004-06-06 17:37:54 +08:00
h->options = n->options;
2009-08-21 15:27:52 +08:00
#endif
2009-08-28 00:25:46 +08:00
h->type = ntoht(n->type);
2004-06-06 17:37:54 +08:00
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);
2009-09-08 23:06:47 +08:00
}
void
2009-08-21 15:27:52 +08:00
htonlsab(void *h, void *n, u16 type, u16 len)
{
2009-09-08 23:06:47 +08:00
u32 *hid = h;
u32 *nid = n;
int i;
2009-09-08 23:06:47 +08:00
for (i = 0; i < (len / sizeof(u32)); i++)
nid[i] = htonl(hid[i]);
}
void
2009-08-21 15:27:52 +08:00
ntohlsab(void *n, void *h, u16 type, u16 len)
{
2009-09-08 23:06:47 +08:00
u32 *nid = n;
u32 *hid = h;
int i;
2009-09-08 23:06:47 +08:00
for (i = 0; i < (len / sizeof(u32)); i++)
hid[i] = ntohl(nid[i]);
}
2009-08-28 00:25:46 +08:00
void
buf_dump(const char *hdr, const byte *buf, int blen)
{
char b2[1024];
char *bp;
int first = 1;
int i;
const char *lhdr = hdr;
bp = b2;
for(i = 0; i < blen; i++)
{
if ((i > 0) && ((i % 16) == 0))
{
*bp = 0;
log(L_WARN "%s\t%s", lhdr, b2);
lhdr = "";
bp = b2;
}
bp += snprintf(bp, 1022, "%02x ", buf[i]);
}
*bp = 0;
log(L_WARN "%s\t%s", lhdr, b2);
}
#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
2004-06-07 02:45:08 +08:00
lsasum_calculate(struct ospf_lsa_header *h, void *body)
{
2009-08-28 00:25:46 +08:00
u16 length = h->length;
u16 type = h->type;
2004-06-06 17:37:54 +08:00
// log(L_WARN "Checksum %R %R %d start (len %d)", h->id, h->rt, h->type, length);
2004-06-06 17:37:54 +08:00
htonlsah(h, h);
2009-08-28 00:25:46 +08:00
htonlsab(body, body, type, length - sizeof(struct ospf_lsa_header));
/*
2009-08-28 00:25:46 +08:00
char buf[1024];
memcpy(buf, h, sizeof(struct ospf_lsa_header));
memcpy(buf + sizeof(struct ospf_lsa_header), body, length - sizeof(struct ospf_lsa_header));
buf_dump("CALC", buf, length);
*/
2004-06-07 02:45:08 +08:00
(void) lsasum_check(h, body);
2004-06-06 17:37:54 +08:00
// log(L_WARN "Checksum result %4x", h->checksum);
2009-08-28 00:25:46 +08:00
2004-06-06 17:37:54 +08:00
ntohlsah(h, h);
2009-08-28 00:25:46 +08:00
ntohlsab(body, body, type, length - sizeof(struct ospf_lsa_header));
}
/*
* Note, that this function expects that LSA is in big endianity
* It also returns value in big endian
*/
u16
2004-06-07 02:45:08 +08:00
lsasum_check(struct ospf_lsa_header *h, void *body)
{
u8 *sp, *ep, *p, *q, *b;
int c0 = 0, c1 = 0;
int x, y;
u16 length;
b = body;
2009-08-28 00:25:46 +08:00
sp = (char *) h;
2009-08-21 15:27:52 +08:00
sp += 2; /* Skip Age field */
2004-06-06 17:37:54 +08:00
length = ntohs(h->length) - 2;
h->checksum = 0;
for (ep = sp + length; sp < ep; sp = q)
2004-06-06 17:37:54 +08:00
{ /* Actually MODX is very large, do we need the for-cyclus? */
q = sp + MODX;
2004-06-06 17:37:54 +08:00
if (q > ep)
q = ep;
for (p = sp; p < q; p++)
{
/*
* 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
*/
2004-06-06 17:37:54 +08:00
if ((b == NULL) || (p < (u8 *) (h + 1)))
{
2004-06-06 17:37:54 +08:00
c0 += *p;
}
else
{
2004-06-06 17:37:54 +08:00
c0 += *(b + (p - sp) - sizeof(struct ospf_lsa_header) + 2);
}
c1 += c0;
}
c0 %= 255;
c1 %= 255;
}
x = ((length - LSA_CHECKSUM_OFFSET) * c0 - c1) % 255;
2004-06-06 17:37:54 +08:00
if (x <= 0)
x += 255;
y = 510 - c0 - x;
2004-06-06 17:37:54 +08:00
if (y > 255)
y -= 255;
2004-06-06 17:37:54 +08:00
((u8 *) & h->checksum)[0] = x;
((u8 *) & h->checksum)[1] = y;
return h->checksum;
}
2000-04-03 03:04:23 +08:00
int
lsa_comp(struct ospf_lsa_header *l1, struct ospf_lsa_header *l2)
/* Return codes from point of view of l1 */
2000-04-03 03:04:23 +08:00
{
2004-06-06 17:37:54 +08:00
u32 sn1, sn2;
2004-06-06 17:37:54 +08:00
sn1 = l1->sn - LSA_INITSEQNO + 1;
sn2 = l2->sn - LSA_INITSEQNO + 1;
2004-06-06 17:37:54 +08:00
if (sn1 > sn2)
return CMP_NEWER;
if (sn1 < sn2)
return CMP_OLDER;
2000-05-30 23:01:51 +08:00
2004-06-06 17:37:54 +08:00
if (l1->checksum != l2->checksum)
return l1->checksum < l2->checksum ? CMP_OLDER : CMP_NEWER;
2000-05-30 23:01:51 +08:00
2004-06-06 17:37:54 +08:00
if ((l1->age == LSA_MAXAGE) && (l2->age != LSA_MAXAGE))
return CMP_NEWER;
if ((l2->age == LSA_MAXAGE) && (l1->age != LSA_MAXAGE))
return CMP_OLDER;
2000-05-30 23:01:51 +08:00
2004-06-06 17:37:54 +08:00
if (ABS(l1->age - l2->age) > LSA_MAXAGEDIFF)
return l1->age < l2->age ? CMP_NEWER : CMP_OLDER;
2000-05-30 23:01:51 +08:00
return CMP_SAME;
2000-04-03 03:04:23 +08:00
}
/**
* lsa_install_new - install new LSA into database
2009-08-21 15:27:52 +08:00
* @po: OSPF protocol
* @lsa: LSA header
2009-08-21 15:27:52 +08:00
* @domain: domain of LSA
2000-06-08 20:42:34 +08:00
* @body: pointer to LSA body
2009-08-21 15:27:52 +08:00
*
* This function ensures installing new LSA into LSA database. Old instance is
2002-04-03 23:41:05 +08:00
* replaced. Several actions are taken to detect if new routing table
* calculation is necessary. This is described in 13.2 of RFC 2328.
*/
struct top_hash_entry *
2009-08-21 15:27:52 +08:00
lsa_install_new(struct proto_ospf *po, struct ospf_lsa_header *lsa, u32 domain, void *body)
{
/* LSA can be temporarrily, but body must be mb_allocated. */
2004-06-06 17:37:54 +08:00
int change = 0;
struct top_hash_entry *en;
2009-08-25 22:42:14 +08:00
if ((en = ospf_hash_find_header(po->gr, domain, lsa)) == NULL)
{
2009-08-25 22:42:14 +08:00
en = ospf_hash_get_header(po->gr, domain, lsa);
2004-06-06 17:37:54 +08:00
change = 1;
}
else
{
2009-08-21 15:27:52 +08:00
if ((en->lsa.length != lsa->length)
#ifdef OSPFv2
|| (en->lsa.options != lsa->options)
#endif
|| (en->lsa.age == LSA_MAXAGE)
|| (lsa->age == LSA_MAXAGE)
|| memcmp(en->lsa_body, body, lsa->length - sizeof(struct ospf_lsa_header)))
2004-06-06 17:37:54 +08:00
change = 1;
2009-08-21 15:27:52 +08:00
s_rem_node(SNODE en);
}
2009-07-23 22:51:28 +08:00
DBG("Inst lsa: Id: %R, Rt: %R, Type: %u, Age: %u, Sum: %u, Sn: 0x%x\n",
lsa->id, lsa->rt, lsa->type, lsa->age, lsa->checksum, lsa->sn);
s_add_tail(&po->lsal, SNODE en);
2004-06-06 17:37:54 +08:00
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));
en->ini_age = en->lsa.age;
if (change)
schedule_rtcalc(po);
2004-06-06 17:37:54 +08:00
return en;
}