0
0
mirror of https://gitlab.nic.cz/labs/bird.git synced 2024-12-23 02:01:55 +00:00
bird/proto/ospf/hello.c

359 lines
8.1 KiB
C
Raw Normal View History

/*
* BIRD -- OSPF
*
2004-07-01 15:01:04 +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"
2009-08-21 07:27:52 +00:00
#ifdef OSPFv2
struct ospf_hello_packet
{
struct ospf_packet ospf_packet;
ip_addr netmask;
u16 helloint;
u8 options;
u8 priority;
u32 deadint;
u32 dr;
u32 bdr;
};
#endif
#ifdef OSPFv3
struct ospf_hello_packet
{
struct ospf_packet ospf_packet;
u32 iface_id;
u8 priority;
u8 options3;
u8 options2;
u8 options;
u16 helloint;
u16 deadint;
u32 dr;
u32 bdr;
};
#endif
void
2009-08-21 07:27:52 +00:00
ospf_hello_receive(struct ospf_packet *ps_i, struct ospf_iface *ifa,
struct ospf_neighbor *n, ip_addr faddr)
{
struct proto_ospf *po = ifa->oa->po;
struct proto *p = &po->proto;
char *beg = "OSPF: Bad HELLO packet from ";
unsigned int size, i, twoway, peers;
u32 tmp;
u32 *pnrid;
size = ntohs(ps_i->length);
if (size < sizeof(struct ospf_hello_packet))
{
log(L_ERR "%s%I - too short (%u B)", beg, faddr, size);
return;
}
struct ospf_hello_packet *ps = (void *) ps_i;
OSPF_TRACE(D_PACKETS, "HELLO packet received from %I via %s%s", faddr,
(ifa->type == OSPF_IT_VLINK ? "vlink-" : ""), ifa->iface->name);
2009-08-21 07:27:52 +00:00
#ifdef OSPFv2
ip_addr mask = ps->netmask;
2000-06-08 02:28:04 +00:00
ipa_ntoh(mask);
if ((ifa->type != OSPF_IT_VLINK) &&
(ifa->type != OSPF_IT_PTP) &&
!ipa_equal(mask, ipa_mkmask(ifa->addr->pxlen)))
{
log(L_ERR "%s%I - netmask mismatch (%I)", beg, faddr, mask);
return;
}
2009-08-21 07:27:52 +00:00
#endif
2004-06-04 17:12:27 +00:00
2009-08-21 07:27:52 +00:00
tmp = ntohs(ps->helloint);
if (tmp != ifa->helloint)
{
log(L_ERR "%s%I - hello interval mismatch (%d)", beg, faddr, tmp);
return;
}
2009-08-21 07:27:52 +00:00
#ifdef OSPFv2
tmp = ntohl(ps->deadint);
#else /* OSPFv3 */
tmp = ntohs(ps->deadint);
#endif
if (tmp != ifa->deadint)
{
log(L_ERR "%s%I - dead interval mismatch (%d)", beg, faddr, tmp);
return;
}
2011-07-20 21:40:20 +00:00
/* Check whether bits E, N match */
if ((ps->options ^ ifa->oa->options) & (OPT_E | OPT_N))
{
2011-07-20 21:40:20 +00:00
log(L_ERR "%s%I - area type mismatch (%x)", beg, faddr, ps->options);
return;
}
if (!n)
{
if ((ifa->type == OSPF_IT_NBMA) || (ifa->type == OSPF_IT_PTMP))
{
struct nbma_node *nn = find_nbma_node(ifa, faddr);
if (!nn && ifa->strictnbma)
{
log(L_WARN "Ignoring new neighbor: %I on %s", faddr,
2004-06-04 17:12:27 +00:00
ifa->iface->name);
return;
}
if (nn && (ifa->type == OSPF_IT_NBMA) &&
(((ps->priority == 0) && nn->eligible) ||
((ps->priority > 0) && !nn->eligible)))
{
log(L_ERR "Eligibility mismatch for neighbor: %I on %s",
faddr, ifa->iface->name);
return;
}
if (nn)
nn->found = 1;
}
OSPF_TRACE(D_EVENTS, "New neighbor found: %I on %s", faddr,
2004-06-04 17:12:27 +00:00
ifa->iface->name);
n = ospf_neighbor_new(ifa);
n->rid = ntohl(((struct ospf_packet *) ps)->routerid);
2004-06-04 17:12:27 +00:00
n->ip = faddr;
2009-08-21 07:27:52 +00:00
n->dr = ntohl(ps->dr);
n->bdr = ntohl(ps->bdr);
2004-06-04 17:12:27 +00:00
n->priority = ps->priority;
2009-08-21 07:27:52 +00:00
#ifdef OSPFv3
n->iface_id = ntohl(ps->iface_id);
#endif
}
ospf_neigh_sm(n, INM_HELLOREC);
2004-06-04 17:12:27 +00:00
pnrid = (u32 *) ((struct ospf_hello_packet *) (ps + 1));
2004-07-01 15:01:04 +00:00
peers = (size - sizeof(struct ospf_hello_packet))/ sizeof(u32);
2004-06-04 17:12:27 +00:00
twoway = 0;
2004-07-01 15:01:04 +00:00
for (i = 0; i < peers; i++)
{
if (ntohl(pnrid[i]) == po->router_id)
{
2000-05-10 23:42:13 +00:00
DBG("%s: Twoway received from %I\n", p->name, faddr);
ospf_neigh_sm(n, INM_2WAYREC);
2004-06-04 17:12:27 +00:00
twoway = 1;
break;
}
}
2004-06-04 17:12:27 +00:00
if (!twoway)
ospf_neigh_sm(n, INM_1WAYREC);
u32 olddr = n->dr;
u32 oldbdr = n->bdr;
u32 oldpriority = n->priority;
2009-08-21 07:27:52 +00:00
#ifdef OSPFv3
u32 oldiface_id = n->iface_id;
2009-08-21 07:27:52 +00:00
#endif
n->dr = ntohl(ps->dr);
n->bdr = ntohl(ps->bdr);
n->priority = ps->priority;
2009-08-21 07:27:52 +00:00
#ifdef OSPFv3
n->iface_id = ntohl(ps->iface_id);
#endif
/* Check priority change */
2004-06-04 17:12:27 +00:00
if (n->state >= NEIGHBOR_2WAY)
{
2009-08-21 07:27:52 +00:00
#ifdef OSPFv2
u32 neigh = ipa_to_u32(n->ip);
2009-08-21 07:27:52 +00:00
#else /* OSPFv3 */
u32 neigh = n->rid;
2009-08-21 07:27:52 +00:00
#endif
2004-06-04 17:12:27 +00:00
if (n->priority != oldpriority)
2004-06-06 08:12:42 +00:00
ospf_iface_sm(ifa, ISM_NEICH);
2009-08-21 07:27:52 +00:00
#ifdef OSPFv3
if (n->iface_id != oldiface_id)
ospf_iface_sm(ifa, ISM_NEICH);
#endif
/* Neighbor is declaring itself ad DR and there is no BDR */
if ((n->dr == neigh) && (n->bdr == 0)
2004-06-04 17:12:27 +00:00
&& (n->state != NEIGHBOR_FULL))
2004-06-06 08:12:42 +00:00
ospf_iface_sm(ifa, ISM_BACKS);
/* Neighbor is declaring itself as BDR */
if ((n->bdr == neigh) && (n->state != NEIGHBOR_FULL))
2004-06-06 08:12:42 +00:00
ospf_iface_sm(ifa, ISM_BACKS);
/* Neighbor is newly declaring itself as DR or BDR */
if (((n->dr == neigh) && (n->dr != olddr))
|| ((n->bdr == neigh) && (n->bdr != oldbdr)))
2004-06-06 08:12:42 +00:00
ospf_iface_sm(ifa, ISM_NEICH);
/* Neighbor is no more declaring itself as DR or BDR */
if (((olddr == neigh) && (n->dr != olddr))
|| ((oldbdr == neigh) && (n->bdr != oldbdr)))
2004-06-06 08:12:42 +00:00
ospf_iface_sm(ifa, ISM_NEICH);
}
if (ifa->type == OSPF_IT_NBMA)
{
2004-06-04 17:12:27 +00:00
if ((ifa->priority == 0) && (n->priority > 0))
ospf_hello_send(n->ifa, OHS_HELLO, n);
}
ospf_neigh_sm(n, INM_HELLOREC);
}
void
ospf_hello_send(struct ospf_iface *ifa, int kind, struct ospf_neighbor *dirn)
{
struct ospf_hello_packet *pkt;
struct ospf_packet *op;
struct proto *p;
struct ospf_neighbor *neigh, *n1;
u16 length;
int i;
struct nbma_node *nb;
if (ifa->state <= OSPF_IS_LOOP)
return;
2004-06-04 17:12:27 +00:00
if (ifa->stub)
return; /* Don't send any packet on stub iface */
p = (struct proto *) (ifa->oa->po);
2010-03-14 15:36:59 +00:00
DBG("%s: Hello/Poll timer fired on interface %s with IP %I\n",
p->name, ifa->iface->name, ifa->addr->ip);
/* Now we should send a hello packet */
pkt = ospf_tx_buffer(ifa);
op = &pkt->ospf_packet;
/* Now fill ospf_hello header */
ospf_pkt_fill_hdr(ifa, pkt, HELLO_P);
2000-06-05 23:44:26 +00:00
2009-08-21 07:27:52 +00:00
#ifdef OSPFv2
pkt->netmask = ipa_mkmask(ifa->addr->pxlen);
2000-06-05 23:44:26 +00:00
ipa_hton(pkt->netmask);
if ((ifa->type == OSPF_IT_VLINK) || (ifa->type == OSPF_IT_PTP))
pkt->netmask = IPA_NONE;
2009-08-21 07:27:52 +00:00
#endif
2004-06-04 17:12:27 +00:00
pkt->helloint = ntohs(ifa->helloint);
pkt->priority = ifa->priority;
2009-08-21 07:27:52 +00:00
#ifdef OSPFv3
2012-10-31 16:14:35 +00:00
pkt->iface_id = htonl(ifa->iface_id);
2009-08-21 07:27:52 +00:00
pkt->options3 = ifa->oa->options >> 16;
pkt->options2 = ifa->oa->options >> 8;
#endif
pkt->options = ifa->oa->options;
#ifdef OSPFv2
pkt->deadint = htonl(ifa->deadint);
2009-08-21 07:27:52 +00:00
pkt->dr = htonl(ipa_to_u32(ifa->drip));
pkt->bdr = htonl(ipa_to_u32(ifa->bdrip));
#else /* OSPFv3 */
pkt->deadint = htons(ifa->deadint);
2009-08-21 07:27:52 +00:00
pkt->dr = htonl(ifa->drid);
pkt->bdr = htonl(ifa->bdrid);
#endif
2000-06-05 23:44:26 +00:00
/* Fill all neighbors */
2004-06-04 17:12:27 +00:00
i = 0;
if (kind != OHS_SHUTDOWN)
2000-06-05 23:44:26 +00:00
{
u32 *pp = (u32 *) (((u8 *) pkt) + sizeof(struct ospf_hello_packet));
WALK_LIST(neigh, ifa->neigh_list)
{
if ((i+1) * sizeof(u32) + sizeof(struct ospf_hello_packet) > ospf_pkt_bufsize(ifa))
{
log(L_WARN "%s: Too many neighbors on interface %s", p->name, ifa->iface->name);
break;
}
*(pp + i) = htonl(neigh->rid);
i++;
}
2000-06-05 23:44:26 +00:00
}
2004-06-04 17:12:27 +00:00
length = sizeof(struct ospf_hello_packet) + i * sizeof(u32);
op->length = htons(length);
2000-06-05 23:44:26 +00:00
switch(ifa->type)
2000-06-05 23:44:26 +00:00
{
case OSPF_IT_BCAST:
case OSPF_IT_PTP:
2012-05-14 09:47:41 +00:00
ospf_send_to_all(ifa);
break;
case OSPF_IT_NBMA:
if (dirn) /* Response to received hello */
{
ospf_send_to(ifa, dirn->ip);
break;
}
int to_all = ifa->state > OSPF_IS_DROTHER;
int me_elig = ifa->priority > 0;
if (kind == OHS_POLL) /* Poll timer */
{
WALK_LIST(nb, ifa->nbma_list)
if (!nb->found && (to_all || (me_elig && nb->eligible)))
ospf_send_to(ifa, nb->ip);
}
else /* Hello timer */
{
WALK_LIST(n1, ifa->neigh_list)
if (to_all || (me_elig && (n1->priority > 0)) ||
(n1->rid == ifa->drid) || (n1->rid == ifa->bdrid))
ospf_send_to(ifa, n1->ip);
}
break;
case OSPF_IT_PTMP:
WALK_LIST(n1, ifa->neigh_list)
ospf_send_to(ifa, n1->ip);
WALK_LIST(nb, ifa->nbma_list)
if (!nb->found)
ospf_send_to(ifa, nb->ip);
/* If there is no other target, we also send HELLO packet to the other end */
if (ipa_nonzero(ifa->addr->opposite) && !ifa->strictnbma &&
EMPTY_LIST(ifa->neigh_list) && EMPTY_LIST(ifa->nbma_list))
ospf_send_to(ifa, ifa->addr->opposite);
break;
case OSPF_IT_VLINK:
ospf_send_to(ifa, ifa->vip);
break;
default:
bug("Bug in ospf_hello_send()");
2000-06-05 23:44:26 +00:00
}
OSPF_TRACE(D_PACKETS, "HELLO packet sent via %s%s",
(ifa->type == OSPF_IT_VLINK ? "vlink-" : ""), ifa->iface->name);
}