0
0
mirror of https://gitlab.nic.cz/labs/bird.git synced 2024-11-18 00:58:42 +00:00
bird/proto/ospf/dbdes.c

356 lines
8.8 KiB
C
Raw Normal View History

/*
* BIRD -- OSPF
*
2004-06-04 18:24:15 +00:00
* (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-06-07 22:53:51 +00:00
/**
2004-06-04 17:49:25 +00:00
* ospf_dbdes_send - transmit database description packet
2000-06-07 22:53:51 +00:00
* @n: neighbor
*
* Sending of a database description packet is described in 10.6 of RFC 2328.
* Reception of each packet is acknowledged in the sequence number of another.
* When I send a packet to a neighbor I keep a copy in a buffer. If the neighbor
* does not reply, I don't create a new packet but just send the content
* of the buffer.
2000-06-07 22:53:51 +00:00
*/
void
2004-06-04 17:49:25 +00:00
ospf_dbdes_send(struct ospf_neighbor *n)
{
struct ospf_dbdes_packet *pkt;
struct ospf_packet *op;
2004-06-04 17:49:25 +00:00
struct ospf_iface *ifa = n->ifa;
struct ospf_area *oa = ifa->oa;
u16 length;
2004-06-04 17:49:25 +00:00
struct proto *p = (struct proto *) (ifa->proto);
u16 i, j;
2004-06-04 17:49:25 +00:00
if ((oa->rt == NULL) || (EMPTY_LIST(oa->lsal)))
originate_rt_lsa(oa);
2004-06-04 17:49:25 +00:00
switch (n->state)
{
2004-06-04 17:49:25 +00:00
case NEIGHBOR_EXSTART: /* Send empty packets */
n->myimms.bit.i = 1;
pkt = (struct ospf_dbdes_packet *) (ifa->ip_sk->tbuf);
op = (struct ospf_packet *) pkt;
fill_ospf_pkt_hdr(ifa, pkt, DBDES_P);
pkt->iface_mtu = htons(ifa->iface->mtu); /* FIXME NOT for VLINK! */
pkt->options = ifa->options;
pkt->imms = n->myimms;
pkt->ddseq = htonl(n->dds);
length = sizeof(struct ospf_dbdes_packet);
op->length = htons(length);
ospf_pkt_finalize(ifa, op);
sk_send_to(ifa->ip_sk, length, n->ip, OSPF_PROTO);
OSPF_TRACE(D_PACKETS, "DB_DES (I) sent to %I via %s.", n->ip,
ifa->iface->name);
break;
case NEIGHBOR_EXCHANGE:
n->myimms.bit.i = 0;
if (((n->myimms.bit.ms) && (n->dds == n->ddr + 1)) ||
((!(n->myimms.bit.ms)) && (n->dds == n->ddr)))
{
snode *sn; /* Send next */
struct ospf_lsa_header *lsa;
pkt = n->ldbdes;
op = (struct ospf_packet *) pkt;
2000-06-03 01:29:00 +00:00
fill_ospf_pkt_hdr(ifa, pkt, DBDES_P);
2004-06-04 17:49:25 +00:00
pkt->iface_mtu = htons(ifa->iface->mtu);
pkt->options = ifa->options;
pkt->ddseq = htonl(n->dds);
2004-06-04 17:49:25 +00:00
j = i = (ifa->iface->mtu - sizeof(struct ospf_dbdes_packet) - SIPH) / sizeof(struct ospf_lsa_header); /* Number of possible lsaheaders to send */
lsa = (n->ldbdes + sizeof(struct ospf_dbdes_packet));
2004-06-04 17:49:25 +00:00
if (n->myimms.bit.m)
{
2004-06-04 17:49:25 +00:00
sn = s_get(&(n->dbsi));
DBG("Number of LSA: %d\n", j);
for (; i > 0; i--)
{
2004-06-04 17:49:25 +00:00
struct top_hash_entry *en;
2004-06-04 17:49:25 +00:00
en = (struct top_hash_entry *) sn;
htonlsah(&(en->lsa), lsa);
DBG("Working on: %d\n", i);
DBG("\tX%01x %-1I %-1I %p\n", en->lsa.type, en->lsa.id,
en->lsa.rt, en->lsa_body);
2000-03-29 00:34:28 +00:00
2004-06-04 17:49:25 +00:00
if (sn == STAIL(n->ifa->oa->lsal))
{
2004-06-04 17:49:25 +00:00
i--;
break; /* Should set some flag? */
}
2004-06-04 17:49:25 +00:00
sn = sn->next;
lsa++;
}
2000-03-29 00:34:28 +00:00
2004-06-04 17:49:25 +00:00
if (sn == STAIL(n->ifa->oa->lsal))
{
DBG("Number of LSA NOT sent: %d\n", i);
DBG("M bit unset.\n");
n->myimms.bit.m = 0; /* Unset more bit */
}
else
s_put(&(n->dbsi), sn);
}
2004-06-04 17:49:25 +00:00
pkt->imms.byte = n->myimms.byte;
2004-06-04 17:49:25 +00:00
length = (j - i) * sizeof(struct ospf_lsa_header) +
sizeof(struct ospf_dbdes_packet);
op->length = htons(length);
ospf_pkt_finalize(ifa, op);
DBG("%s: DB_DES (M) prepared for %I.\n", p->name, n->ip);
}
case NEIGHBOR_LOADING:
case NEIGHBOR_FULL:
2004-06-04 19:21:19 +00:00
length = ntohs(((struct ospf_packet *)n->ldbdes)->length);
2004-06-04 17:49:25 +00:00
2004-06-04 18:51:29 +00:00
if(!length)
{
OSPF_TRACE(D_PACKETS, "No packet in my buffer for repeating");
ospf_neigh_sm(n, INM_KILLNBR);
2004-06-04 19:21:19 +00:00
return;
2004-06-04 18:51:29 +00:00
}
2004-06-04 18:24:15 +00:00
memcpy(ifa->ip_sk->tbuf, n->ldbdes, length);
/* Copy last sent packet again */
2004-06-04 17:49:25 +00:00
sk_send_to(ifa->ip_sk, length, n->ip, OSPF_PROTO);
OSPF_TRACE(D_PACKETS, "DB_DES (M) sent to %I via %s.", n->ip,
ifa->iface->name);
if (!n->myimms.bit.ms)
{
if ((n->myimms.bit.m == 0) && (n->imms.bit.m == 0) &&
(n->state == NEIGHBOR_EXCHANGE))
{
2004-06-04 17:49:25 +00:00
ospf_neigh_sm(n, INM_EXDONE);
}
2004-06-04 17:49:25 +00:00
}
break;
2004-06-04 17:49:25 +00:00
default: /* Ignore it */
break;
}
}
void
2004-06-04 17:49:25 +00:00
ospf_dbdes_reqladd(struct ospf_dbdes_packet *ps, struct ospf_neighbor *n)
{
2004-06-04 17:49:25 +00:00
struct ospf_lsa_header *plsa, lsa;
struct top_hash_entry *he, *sn;
struct top_graph *gr = n->ifa->oa->gr;
2000-03-30 00:18:59 +00:00
struct ospf_packet *op;
2004-06-04 17:49:25 +00:00
int i, j;
2004-06-04 17:49:25 +00:00
op = (struct ospf_packet *) ps;
2004-06-04 17:49:25 +00:00
plsa = (void *) (ps + 1);
2000-03-30 00:18:59 +00:00
2004-06-04 17:49:25 +00:00
j = (ntohs(op->length) - sizeof(struct ospf_dbdes_packet)) /
sizeof(struct ospf_lsa_header);
2000-03-30 00:18:59 +00:00
2004-06-04 17:49:25 +00:00
for (i = 0; i < j; i++)
2000-03-30 00:18:59 +00:00
{
2004-06-04 17:49:25 +00:00
ntohlsah(plsa + i, &lsa);
if (((he = ospf_hash_find(gr, lsa.id, lsa.rt, lsa.type)) == NULL) ||
(lsa_comp(&lsa, &(he->lsa)) == 1))
{
/* Is this condition necessary? */
2004-06-04 17:49:25 +00:00
if (ospf_hash_find(n->lsrqh, lsa.id, lsa.rt, lsa.type) == NULL)
{
2004-06-04 17:49:25 +00:00
sn = ospf_hash_get(n->lsrqh, lsa.id, lsa.rt, lsa.type);
ntohlsah(plsa + i, &(sn->lsa));
s_add_tail(&(n->lsrql), SNODE sn);
}
}
2000-03-30 00:18:59 +00:00
}
}
void
2004-06-04 17:49:25 +00:00
ospf_dbdes_receive(struct ospf_dbdes_packet *ps,
struct ospf_iface *ifa, u16 size)
{
2004-06-04 17:49:25 +00:00
struct proto *p = (struct proto *) ifa->proto;
u32 nrid, myrid = p->cf->global->router_id;
struct ospf_neighbor *n;
2004-06-04 17:49:25 +00:00
nrid = ntohl(((struct ospf_packet *) ps)->routerid);
2004-06-04 17:49:25 +00:00
if ((n = find_neigh(ifa, nrid)) == NULL)
{
2004-06-04 17:49:25 +00:00
OSPF_TRACE(D_PACKETS, "Received dbdes from unknown neigbor! %I.", nrid);
return;
}
2004-06-04 17:49:25 +00:00
if (ifa->iface->mtu < size)
{
2000-06-06 02:34:57 +00:00
OSPF_TRACE(D_PACKETS, "Received dbdes larger than MTU from %I!", n->ip);
2004-06-04 17:49:25 +00:00
return;
}
2000-06-06 02:34:57 +00:00
OSPF_TRACE(D_PACKETS, "Received dbdes from %I via %s.", n->ip,
2004-06-04 17:49:25 +00:00
ifa->iface->name);
ospf_neigh_sm(n, INM_HELLOREC);
2000-05-10 12:22:00 +00:00
2004-06-04 17:49:25 +00:00
switch (n->state)
{
2004-06-04 17:49:25 +00:00
case NEIGHBOR_DOWN:
case NEIGHBOR_ATTEMPT:
case NEIGHBOR_2WAY:
return;
break;
case NEIGHBOR_INIT:
ospf_neigh_sm(n, INM_2WAYREC);
if (n->state != NEIGHBOR_EXSTART)
return;
case NEIGHBOR_EXSTART:
if ((ps->imms.bit.m && ps->imms.bit.ms && ps->imms.bit.i)
&& (n->rid > myrid) && (size == sizeof(struct ospf_dbdes_packet)))
{
/* I'm slave! */
n->dds = ntohl(ps->ddseq);
n->ddr = ntohl(ps->ddseq);
n->options = ps->options;
n->myimms.bit.ms = 0;
n->imms.byte = ps->imms.byte;
OSPF_TRACE(D_PACKETS, "I'm slave to %I.", n->ip);
ospf_neigh_sm(n, INM_NEGDONE);
ospf_dbdes_send(n);
break;
2004-06-04 17:49:25 +00:00
}
else
{
if (((ps->imms.bit.i == 0) && (ps->imms.bit.ms == 0)) &&
(n->rid < myrid) && (n->dds == ntohl(ps->ddseq)))
{
/* I'm master! */
n->options = ps->options;
n->ddr = ntohl(ps->ddseq) - 1;
n->imms.byte = ps->imms.byte;
OSPF_TRACE(D_PACKETS, "I'm master to %I.", n->ip);
ospf_neigh_sm(n, INM_NEGDONE);
}
else
{
DBG("%s: Nothing happend to %I (imms=%u)\n", p->name, n->ip,
ps->imms.byte);
break;
}
}
if (ps->imms.bit.i)
break;
case NEIGHBOR_EXCHANGE:
if ((ps->imms.byte == n->imms.byte) && (ps->options == n->options) &&
(ntohl(ps->ddseq) == n->ddr))
{
/* Duplicate packet */
OSPF_TRACE(D_PACKETS, "Received duplicate dbdes from %I.", n->ip);
if (n->imms.bit.ms == 0)
{
ospf_dbdes_send(n);
}
return;
}
2004-06-04 17:49:25 +00:00
n->ddr = ntohl(ps->ddseq);
2004-06-04 17:49:25 +00:00
if (ps->imms.bit.ms != n->imms.bit.ms) /* M/S bit differs */
{
OSPF_TRACE(D_PACKETS, "dbdes - sequence mismatch neighbor %I (bit MS)",
n->ip);
ospf_neigh_sm(n, INM_SEQMIS);
break;
2004-06-04 17:49:25 +00:00
}
if (ps->imms.bit.i) /* I bit is set */
{
OSPF_TRACE(D_PACKETS, "dbdes - sequence mismatch neighbor %I (bit I)",
n->ip);
ospf_neigh_sm(n, INM_SEQMIS);
break;
2004-06-04 17:49:25 +00:00
}
n->imms.byte = ps->imms.byte;
if (ps->options != n->options) /* Options differs */
{
OSPF_TRACE(D_PACKETS, "dbdes - sequence mismatch neighbor %I (options)",
n->ip);
ospf_neigh_sm(n, INM_SEQMIS);
break;
2004-06-04 17:49:25 +00:00
}
2004-06-04 17:49:25 +00:00
if (n->myimms.bit.ms)
{
if (ntohl(ps->ddseq) != n->dds) /* MASTER */
{
OSPF_TRACE(D_PACKETS,
"dbdes - sequence mismatch neighbor %I (master)", n->ip);
ospf_neigh_sm(n, INM_SEQMIS);
break;
}
n->dds++;
DBG("Incrementing dds\n");
ospf_dbdes_reqladd(ps, n);
if ((n->myimms.bit.m == 0) && (ps->imms.bit.m == 0))
{
ospf_neigh_sm(n, INM_EXDONE);
}
else
{
ospf_dbdes_send(n);
}
}
else
{
if (ntohl(ps->ddseq) != (n->dds + 1)) /* SLAVE */
{
OSPF_TRACE(D_PACKETS, "dbdes - sequence mismatch neighbor %I (slave)",
n->ip);
ospf_neigh_sm(n, INM_SEQMIS);
break;
}
n->ddr = ntohl(ps->ddseq);
n->dds = ntohl(ps->ddseq);
ospf_dbdes_reqladd(ps, n);
ospf_dbdes_send(n);
}
break;
case NEIGHBOR_LOADING:
case NEIGHBOR_FULL:
if ((ps->imms.byte == n->imms.byte) && (ps->options == n->options)
&& (ntohl(ps->ddseq) == n->ddr))
/* Only duplicate are accepted */
{
OSPF_TRACE(D_PACKETS, "Received duplicate dbdes from %I.", n->ip);
return;
}
else
{
OSPF_TRACE(D_PACKETS, "dbdes - sequence mismatch neighbor %I (full)",
n->ip);
ospf_neigh_sm(n, INM_SEQMIS);
}
break;
2004-06-05 09:29:38 +00:00
default:
2004-06-04 17:49:25 +00:00
bug("Received dbdes from %I in undefined state.", n->ip);
}
}