bird/proto/ospf/dbdes.c

520 lines
12 KiB
C
Raw Normal View History

/*
* BIRD -- OSPF
*
2004-06-05 02:24:15 +08:00
* (c) 1999--2004 Ondrej Filip <feela@network.cz>
2014-06-26 17:58:57 +08:00
* (c) 2009--2014 Ondrej Zajicek <santiago@crfreenet.org>
* (c) 2009--2014 CZ.NIC z.s.p.o.
*
* Can be freely distributed and used under the terms of the GNU GPL.
*/
#include "ospf.h"
2009-08-21 15:27:52 +08:00
2014-06-26 17:58:57 +08:00
struct ospf_dbdes2_packet
2009-08-21 15:27:52 +08:00
{
2014-06-26 17:58:57 +08:00
struct ospf_packet hdr;
union ospf_auth auth;
2009-08-21 15:27:52 +08:00
u16 iface_mtu;
u8 options;
2014-06-26 17:58:57 +08:00
u8 imms; /* I, M, MS bits */
2009-08-21 15:27:52 +08:00
u32 ddseq;
2014-06-26 17:58:57 +08:00
struct ospf_lsa_header lsas[];
};
2009-08-21 15:27:52 +08:00
2014-06-26 17:58:57 +08:00
struct ospf_dbdes3_packet
2009-08-21 15:27:52 +08:00
{
2014-06-26 17:58:57 +08:00
struct ospf_packet hdr;
2009-08-21 15:27:52 +08:00
u32 options;
u16 iface_mtu;
u8 padding;
2014-06-26 17:58:57 +08:00
u8 imms; /* I, M, MS bits */
2009-08-21 15:27:52 +08:00
u32 ddseq;
2014-06-26 17:58:57 +08:00
struct ospf_lsa_header lsas[];
2009-08-21 15:27:52 +08:00
};
2014-06-26 17:58:57 +08:00
static inline uint
ospf_dbdes_hdrlen(struct ospf_proto *p)
{
return ospf_is_v2(p) ?
sizeof(struct ospf_dbdes2_packet) : sizeof(struct ospf_dbdes3_packet);
}
static void
ospf_dbdes_body(struct ospf_proto *p, struct ospf_packet *pkt,
struct ospf_lsa_header **body, uint *count)
{
uint plen = ntohs(pkt->length);
uint hlen = ospf_dbdes_hdrlen(p);
*body = ((void *) pkt) + hlen;
*count = (plen - hlen) / sizeof(struct ospf_lsa_header);
}
2014-07-19 23:28:38 +08:00
2014-06-26 17:58:57 +08:00
static void
ospf_dump_dbdes(struct ospf_proto *p, struct ospf_packet *pkt)
{
2014-06-26 17:58:57 +08:00
struct ospf_lsa_header *lsas;
uint i, lsa_count;
u32 pkt_ddseq;
u16 pkt_iface_mtu;
u8 pkt_imms;
2014-06-26 17:58:57 +08:00
ASSERT(pkt->type == DBDES_P);
ospf_dump_common(p, pkt);
2014-06-26 17:58:57 +08:00
if (ospf_is_v2(p))
{
struct ospf_dbdes2_packet *ps = (void *) pkt;
pkt_iface_mtu = ntohs(ps->iface_mtu);
pkt_imms = ps->imms;
pkt_ddseq = ntohl(ps->ddseq);
}
else /* OSPFv3 */
{
struct ospf_dbdes3_packet *ps = (void *) pkt;
pkt_iface_mtu = ntohs(ps->iface_mtu);
pkt_imms = ps->imms;
pkt_ddseq = ntohl(ps->ddseq);
}
2014-06-26 17:58:57 +08:00
log(L_TRACE "%s: mtu %u", p->p.name, pkt_iface_mtu);
log(L_TRACE "%s: imms %s%s%s", p->p.name,
(pkt_imms & DBDES_I) ? "I " : "",
(pkt_imms & DBDES_M) ? "M " : "",
(pkt_imms & DBDES_MS) ? "MS" : "");
log(L_TRACE "%s: ddseq %u", p->p.name, pkt_ddseq);
2014-06-26 17:58:57 +08:00
ospf_dbdes_body(p, pkt, &lsas, &lsa_count);
for (i = 0; i < lsa_count; i++)
ospf_dump_lsahdr(p, lsas + i);
}
2014-06-26 17:58:57 +08:00
static void
ospf_prepare_dbdes(struct ospf_proto *p, struct ospf_neighbor *n, int body)
{
struct ospf_iface *ifa = n->ifa;
struct ospf_packet *pkt;
uint length;
u16 iface_mtu = (ifa->type == OSPF_IT_VLINK) ? 0 : ifa->iface->mtu;
if (n->ldd_bsize != ifa->tx_length)
{
mb_free(n->ldd_buffer);
n->ldd_buffer = mb_allocz(n->pool, ifa->tx_length);
n->ldd_bsize = ifa->tx_length;
}
pkt = n->ldd_buffer;
ospf_pkt_fill_hdr(ifa, pkt, DBDES_P);
if (ospf_is_v2(p))
{
struct ospf_dbdes2_packet *ps = (void *) pkt;
ps->iface_mtu = htons(iface_mtu);
ps->options = ifa->oa->options;
ps->imms = 0; /* Will be set later */
ps->ddseq = htonl(n->dds);
length = sizeof(struct ospf_dbdes2_packet);
}
else /* OSPFv3 */
{
struct ospf_dbdes3_packet *ps = (void *) pkt;
ps->options = htonl(ifa->oa->options);
ps->iface_mtu = htons(iface_mtu);
ps->padding = 0;
ps->imms = 0; /* Will be set later */
ps->ddseq = htonl(n->dds);
length = sizeof(struct ospf_dbdes3_packet);
}
if (body && (n->myimms & DBDES_M))
{
struct ospf_lsa_header *lsas;
struct top_hash_entry *en;
uint i = 0, lsa_max;
ospf_dbdes_body(p, pkt, &lsas, &lsa_max);
en = (void *) s_get(&(n->dbsi));
while (i < lsa_max)
{
if (!SNODE_VALID(en))
{
n->myimms &= ~DBDES_M; /* Unset More bit */
break;
}
if ((en->lsa.age < LSA_MAXAGE) &&
lsa_flooding_allowed(en->lsa_type, en->domain, ifa))
{
lsa_hton_hdr(&(en->lsa), lsas + i);
i++;
}
en = SNODE_NEXT(en);
}
s_put(&(n->dbsi), SNODE en);
length += i * sizeof(struct ospf_lsa_header);
}
if (ospf_is_v2(p))
((struct ospf_dbdes2_packet *) pkt)->imms = n->myimms;
else
((struct ospf_dbdes3_packet *) pkt)->imms = n->myimms;
pkt->length = htons(length);
}
static void
ospf_do_send_dbdes(struct ospf_proto *p, struct ospf_neighbor *n)
{
struct ospf_iface *ifa = n->ifa;
OSPF_PACKET(ospf_dump_dbdes, n->ldd_buffer,
"DBDES packet sent to %I via %s", n->ip, ifa->ifname);
sk_set_tbuf(ifa->sk, n->ldd_buffer);
ospf_send_to(ifa, n->ip);
sk_set_tbuf(ifa->sk, NULL);
}
2000-06-08 06:53:51 +08:00
/**
2014-06-26 17:58:57 +08:00
* ospf_send_dbdes - transmit database description packet
2000-06-08 06:53:51 +08:00
* @n: neighbor
* @next: whether to send a next packet in a sequence (1) or to retransmit the old one (0)
2000-06-08 06:53:51 +08:00
*
2009-08-21 15:27:52 +08:00
* Sending of a database description packet is described in 10.8 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-08 06:53:51 +08:00
*/
void
2014-07-19 00:24:12 +08:00
ospf_send_dbdes(struct ospf_proto *p, struct ospf_neighbor *n, int next)
{
2014-06-26 17:58:57 +08:00
/* RFC 2328 10.8 */
2014-07-19 00:24:12 +08:00
if (n->ifa->oa->rt == NULL)
2014-06-26 17:58:57 +08:00
return;
2004-06-05 01:49:25 +08:00
switch (n->state)
{
2014-06-26 17:58:57 +08:00
case NEIGHBOR_EXSTART:
n->myimms |= DBDES_I;
/* Send empty packets */
ospf_prepare_dbdes(p, n, 0);
ospf_do_send_dbdes(p, n);
2004-06-05 01:49:25 +08:00
break;
case NEIGHBOR_EXCHANGE:
2014-06-26 17:58:57 +08:00
n->myimms &= ~DBDES_I;
2004-06-05 01:49:25 +08:00
if (next)
2014-06-26 17:58:57 +08:00
ospf_prepare_dbdes(p, n, 1);
2004-06-05 01:49:25 +08:00
2014-06-26 17:58:57 +08:00
/* Send prepared packet */
ospf_do_send_dbdes(p, n);
2014-06-26 17:58:57 +08:00
/* Master should restart RXMT timer for each DBDES exchange */
if (n->myimms & DBDES_MS)
tm_start(n->rxmt_timer, n->ifa->rxmtint);
2014-06-26 17:58:57 +08:00
if (!(n->myimms & DBDES_MS))
2014-07-19 23:28:38 +08:00
if (!(n->myimms & DBDES_M) &&
2014-06-26 17:58:57 +08:00
!(n->imms & DBDES_M))
ospf_neigh_sm(n, INM_EXDONE);
break;
2004-06-05 01:49:25 +08:00
case NEIGHBOR_LOADING:
case NEIGHBOR_FULL:
2014-06-26 17:58:57 +08:00
if (!n->ldd_buffer)
2004-06-05 02:51:29 +08:00
{
2014-06-26 17:58:57 +08:00
OSPF_TRACE(D_PACKETS, "No DBDES packet for repeating");
2004-06-05 02:51:29 +08:00
ospf_neigh_sm(n, INM_KILLNBR);
2004-06-05 03:21:19 +08:00
return;
2004-06-05 02:51:29 +08:00
}
2014-06-26 17:58:57 +08:00
/* Send last packet */
ospf_do_send_dbdes(p, n);
break;
}
}
2014-06-26 17:58:57 +08:00
static int
ospf_process_dbdes(struct ospf_proto *p, struct ospf_packet *pkt, struct ospf_neighbor *n)
{
struct ospf_iface *ifa = n->ifa;
struct ospf_lsa_header *lsas;
uint i, lsa_count;
ospf_dbdes_body(p, pkt, &lsas, &lsa_count);
for (i = 0; i < lsa_count; i++)
{
struct top_hash_entry *en, *req;
struct ospf_lsa_header lsa;
u32 lsa_type, lsa_domain;
lsa_ntoh_hdr(lsas + i, &lsa);
lsa_get_type_domain(&lsa, ifa, &lsa_type, &lsa_domain);
2014-06-26 17:58:57 +08:00
/* RFC 2328 10.6 and RFC 5340 4.2.2 */
2014-06-26 17:58:57 +08:00
if (!lsa_type)
2004-06-05 01:49:25 +08:00
{
2014-06-26 17:58:57 +08:00
log(L_WARN "%s: Bad DBDES from %I - LSA of unknown type", p->p.name, n->ip);
goto err;
2004-06-05 01:49:25 +08:00
}
2014-06-26 17:58:57 +08:00
if (!oa_is_ext(ifa->oa) && (LSA_SCOPE(lsa_type) == LSA_SCOPE_AS))
{
log(L_WARN "%s: Bad DBDES from %I - LSA with AS scope in stub area", p->p.name, n->ip);
goto err;
}
2014-06-26 17:58:57 +08:00
/* Errata 3746 to RFC 2328 - rt-summary-LSAs forbidden in stub areas */
if (!oa_is_ext(ifa->oa) && (lsa_type == LSA_T_SUM_RT))
{
log(L_WARN "%s: Bad DBDES from %I - rt-summary-LSA in stub area", p->p.name, n->ip);
goto err;
}
2014-06-26 17:58:57 +08:00
/* Not explicitly mentioned in RFC 5340 4.2.2 but makes sense */
if (LSA_SCOPE(lsa_type) == LSA_SCOPE_RES)
{
log(L_WARN "%s: Bad DBDES from %I - LSA with invalid scope", p->p.name, n->ip);
goto err;
}
2014-06-26 17:58:57 +08:00
en = ospf_hash_find(p->gr, lsa_domain, lsa.id, lsa.rt, lsa_type);
if (!en || (lsa_comp(&lsa, &(en->lsa)) == CMP_NEWER))
{
req = ospf_hash_get(n->lsrqh, lsa_domain, lsa.id, lsa.rt, lsa_type);
2000-03-30 08:18:59 +08:00
2014-06-26 17:58:57 +08:00
if (!SNODE_VALID(req))
s_add_tail(&n->lsrql, SNODE req);
2000-03-30 08:18:59 +08:00
2014-06-26 17:58:57 +08:00
req->lsa = lsa;
2014-07-19 00:24:12 +08:00
req->lsa_body = LSA_BODY_DUMMY;
}
2000-03-30 08:18:59 +08:00
}
2014-06-26 17:58:57 +08:00
return 0;
err:
ospf_neigh_sm(n, INM_SEQMIS);
return -1;
}
void
2014-06-26 17:58:57 +08:00
ospf_receive_dbdes(struct ospf_packet *pkt, struct ospf_iface *ifa,
2009-08-21 15:27:52 +08:00
struct ospf_neighbor *n)
{
2014-06-26 17:58:57 +08:00
struct ospf_proto *p = ifa->oa->po;
u32 rcv_ddseq, rcv_options;
u16 rcv_iface_mtu;
u8 rcv_imms;
uint plen;
2014-06-26 17:58:57 +08:00
/* RFC 2328 10.6 */
plen = ntohs(pkt->length);
if (plen < ospf_dbdes_hdrlen(p))
{
2014-06-26 17:58:57 +08:00
log(L_ERR "OSPF: Bad DBDES packet from %I - too short (%u B)", n->ip, plen);
return;
}
2014-06-26 17:58:57 +08:00
OSPF_PACKET(ospf_dump_dbdes, pkt, "DBDES packet received from %I via %s", n->ip, ifa->ifname);
ospf_neigh_sm(n, INM_HELLOREC);
2000-05-10 20:22:00 +08:00
2014-06-26 17:58:57 +08:00
if (ospf_is_v2(p))
{
struct ospf_dbdes2_packet *ps = (void *) pkt;
rcv_iface_mtu = ntohs(ps->iface_mtu);
rcv_options = ps->options;
rcv_imms = ps->imms;
rcv_ddseq = ntohl(ps->ddseq);
}
else /* OSPFv3 */
{
struct ospf_dbdes3_packet *ps = (void *) pkt;
rcv_options = ntohl(ps->options);
rcv_iface_mtu = ntohs(ps->iface_mtu);
rcv_imms = ps->imms;
rcv_ddseq = ntohl(ps->ddseq);
}
2014-07-19 23:28:38 +08:00
2004-06-05 01:49:25 +08:00
switch (n->state)
{
2004-06-05 01:49:25 +08:00
case NEIGHBOR_DOWN:
case NEIGHBOR_ATTEMPT:
case NEIGHBOR_2WAY:
return;
2014-06-26 17:58:57 +08:00
2004-06-05 01:49:25 +08:00
case NEIGHBOR_INIT:
ospf_neigh_sm(n, INM_2WAYREC);
if (n->state != NEIGHBOR_EXSTART)
return;
2014-06-26 17:58:57 +08:00
case NEIGHBOR_EXSTART:
if ((ifa->type != OSPF_IT_VLINK) &&
(rcv_iface_mtu != ifa->iface->mtu) &&
(rcv_iface_mtu != 0) &&
(ifa->iface->mtu != 0))
log(L_WARN "OSPF: MTU mismatch with neighbor %I on interface %s (remote %d, local %d)",
n->ip, ifa->ifname, rcv_iface_mtu, ifa->iface->mtu);
if ((rcv_imms == DBDES_IMMS) &&
(n->rid > p->router_id) &&
(plen == ospf_dbdes_hdrlen(p)))
2004-06-05 01:49:25 +08:00
{
/* I'm slave! */
2014-06-26 17:58:57 +08:00
n->dds = rcv_ddseq;
n->ddr = rcv_ddseq;
n->options = rcv_options;
n->myimms &= ~DBDES_MS;
n->imms = rcv_imms;
OSPF_TRACE(D_PACKETS, "I'm slave to %I", n->ip);
2004-06-05 01:49:25 +08:00
ospf_neigh_sm(n, INM_NEGDONE);
2014-07-19 00:24:12 +08:00
ospf_send_dbdes(p, n, 1);
break;
2004-06-05 01:49:25 +08:00
}
2014-06-26 17:58:57 +08:00
if (!(rcv_imms & DBDES_I) &&
!(rcv_imms & DBDES_MS) &&
2014-07-19 23:28:38 +08:00
(n->rid < p->router_id) &&
2014-06-26 17:58:57 +08:00
(n->dds == rcv_ddseq))
2004-06-05 01:49:25 +08:00
{
/* I'm master! */
2014-06-26 17:58:57 +08:00
n->options = rcv_options;
n->ddr = rcv_ddseq - 1; /* It will be set corectly a few lines down */
n->imms = rcv_imms;
OSPF_TRACE(D_PACKETS, "I'm master to %I", n->ip);
ospf_neigh_sm(n, INM_NEGDONE);
2004-06-05 01:49:25 +08:00
}
else
{
2014-06-26 17:58:57 +08:00
DBG("%s: Nothing happend to %I (imms=%d)\n", p->name, n->ip, rcv_imms);
2004-06-05 01:49:25 +08:00
break;
}
2014-06-26 17:58:57 +08:00
2004-06-05 01:49:25 +08:00
case NEIGHBOR_EXCHANGE:
2014-06-26 17:58:57 +08:00
if ((rcv_imms == n->imms) &&
(rcv_options == n->options) &&
(rcv_ddseq == n->ddr))
2004-06-05 01:49:25 +08:00
{
/* Duplicate packet */
2014-06-26 17:58:57 +08:00
OSPF_TRACE(D_PACKETS, "Received duplicate dbdes from %I", n->ip);
if (!(n->myimms & DBDES_MS))
2004-06-05 01:49:25 +08:00
{
/* Slave should retransmit dbdes packet */
2014-07-19 00:24:12 +08:00
ospf_send_dbdes(p, n, 0);
2004-06-05 01:49:25 +08:00
}
return;
}
2014-06-26 17:58:57 +08:00
if ((rcv_imms & DBDES_MS) != (n->imms & DBDES_MS)) /* M/S bit differs */
2004-06-05 01:49:25 +08:00
{
2014-06-26 17:58:57 +08:00
OSPF_TRACE(D_PACKETS, "dbdes - sequence mismatch neighbor %I (bit MS)", n->ip);
2004-06-05 01:49:25 +08:00
ospf_neigh_sm(n, INM_SEQMIS);
break;
2004-06-05 01:49:25 +08:00
}
2014-06-26 17:58:57 +08:00
if (rcv_imms & DBDES_I) /* I bit is set */
2004-06-05 01:49:25 +08:00
{
2014-06-26 17:58:57 +08:00
OSPF_TRACE(D_PACKETS, "dbdes - sequence mismatch neighbor %I (bit I)", n->ip);
2004-06-05 01:49:25 +08:00
ospf_neigh_sm(n, INM_SEQMIS);
break;
2004-06-05 01:49:25 +08:00
}
2014-06-26 17:58:57 +08:00
if (rcv_options != n->options) /* Options differs */
2004-06-05 01:49:25 +08:00
{
2014-06-26 17:58:57 +08:00
OSPF_TRACE(D_PACKETS, "dbdes - sequence mismatch neighbor %I (options)", n->ip);
2004-06-05 01:49:25 +08:00
ospf_neigh_sm(n, INM_SEQMIS);
break;
2004-06-05 01:49:25 +08:00
}
2014-06-26 17:58:57 +08:00
n->ddr = rcv_ddseq;
n->imms = rcv_imms;
if (n->myimms & DBDES_MS)
2004-06-05 01:49:25 +08:00
{
2014-06-26 17:58:57 +08:00
if (rcv_ddseq != n->dds) /* MASTER */
2004-06-05 01:49:25 +08:00
{
2014-06-26 17:58:57 +08:00
OSPF_TRACE(D_PACKETS, "dbdes - sequence mismatch neighbor %I (master)", n->ip);
2004-06-05 01:49:25 +08:00
ospf_neigh_sm(n, INM_SEQMIS);
break;
}
2014-06-26 17:58:57 +08:00
2004-06-05 01:49:25 +08:00
n->dds++;
2014-06-26 17:58:57 +08:00
if (ospf_process_dbdes(p, pkt, n) < 0)
return;
if (!(n->myimms & DBDES_M) && !(n->imms & DBDES_M))
2004-06-05 01:49:25 +08:00
ospf_neigh_sm(n, INM_EXDONE);
else
2014-07-19 00:24:12 +08:00
ospf_send_dbdes(p, n, 1);
2004-06-05 01:49:25 +08:00
}
else
{
2014-06-26 17:58:57 +08:00
if (rcv_ddseq != (n->dds + 1)) /* SLAVE */
2004-06-05 01:49:25 +08:00
{
OSPF_TRACE(D_PACKETS, "dbdes - sequence mismatch neighbor %I (slave)", n->ip);
2004-06-05 01:49:25 +08:00
ospf_neigh_sm(n, INM_SEQMIS);
break;
}
2014-06-26 17:58:57 +08:00
n->ddr = rcv_ddseq;
n->dds = rcv_ddseq;
if (ospf_process_dbdes(p, pkt, n) < 0)
return;
2014-07-19 00:24:12 +08:00
ospf_send_dbdes(p, n, 1);
2014-06-26 17:58:57 +08:00
}
2004-06-05 01:49:25 +08:00
break;
2014-06-26 17:58:57 +08:00
2004-06-05 01:49:25 +08:00
case NEIGHBOR_LOADING:
case NEIGHBOR_FULL:
2014-06-26 17:58:57 +08:00
if ((rcv_imms == n->imms) &&
(rcv_options == n->options) &&
(rcv_ddseq == n->ddr))
2004-06-05 01:49:25 +08:00
{
2014-06-26 17:58:57 +08:00
/* Duplicate packet */
OSPF_TRACE(D_PACKETS, "Received duplicate dbdes from %I", n->ip);
if (!(n->myimms & DBDES_MS))
{
/* Slave should retransmit dbdes packet */
2014-07-19 00:24:12 +08:00
ospf_send_dbdes(p, n, 0);
}
2004-06-05 01:49:25 +08:00
return;
}
else
{
2014-06-26 17:58:57 +08:00
OSPF_TRACE(D_PACKETS, "dbdes - sequence mismatch neighbor %I (full)", n->ip);
DBG("PS=%u, DDR=%u, DDS=%u\n", rcv_ddseq, n->ddr, n->dds);
2004-06-05 01:49:25 +08:00
ospf_neigh_sm(n, INM_SEQMIS);
}
break;
2014-06-26 17:58:57 +08:00
2004-06-05 17:29:38 +08:00
default:
2004-06-05 01:49:25 +08:00
bug("Received dbdes from %I in undefined state.", n->ip);
}
}