1999-11-17 15:50:41 +00:00
|
|
|
/*
|
|
|
|
* BIRD -- OSPF
|
|
|
|
*
|
2005-03-15 20:51:33 +00:00
|
|
|
* (c) 1999--2005 Ondrej Filip <feela@network.cz>
|
2012-10-19 14:26:51 +00:00
|
|
|
* (c) 2009--2012 Ondrej Zajicek <santiago@crfreenet.org>
|
|
|
|
* (c) 2009--2012 CZ.NIC z.s.p.o.
|
1999-11-17 15:50:41 +00:00
|
|
|
*
|
|
|
|
* Can be freely distributed and used under the terms of the GNU GPL.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ospf.h"
|
2004-06-26 20:15:34 +00:00
|
|
|
#include "nest/password.h"
|
|
|
|
#include "lib/md5.h"
|
1999-11-17 15:50:41 +00:00
|
|
|
|
|
|
|
void
|
2004-06-26 20:15:34 +00:00
|
|
|
ospf_pkt_fill_hdr(struct ospf_iface *ifa, void *buf, u8 h_type)
|
1999-11-17 15:50:41 +00:00
|
|
|
{
|
2009-12-11 00:20:53 +00:00
|
|
|
struct proto_ospf *po = ifa->oa->po;
|
1999-11-17 15:50:41 +00:00
|
|
|
struct ospf_packet *pkt;
|
|
|
|
|
2003-09-14 13:41:24 +00:00
|
|
|
pkt = (struct ospf_packet *) buf;
|
1999-11-17 15:50:41 +00:00
|
|
|
|
2012-10-19 14:26:51 +00:00
|
|
|
pkt->version = ospf_get_version(po);
|
1999-11-17 15:50:41 +00:00
|
|
|
|
2003-09-14 13:41:24 +00:00
|
|
|
pkt->type = h_type;
|
|
|
|
|
2009-12-11 00:20:53 +00:00
|
|
|
pkt->routerid = htonl(po->router_id);
|
2004-07-14 21:46:20 +00:00
|
|
|
pkt->areaid = htonl(ifa->oa->areaid);
|
2003-09-14 13:41:24 +00:00
|
|
|
pkt->checksum = 0;
|
2012-10-02 09:44:05 +00:00
|
|
|
|
|
|
|
if (ospf_is_v2(po))
|
|
|
|
ospf_pkt_set_autype(pkt, ifa->autype);
|
|
|
|
else
|
|
|
|
ospf_pkt_set_instance_id(pkt, ifa->instance_id);
|
1999-11-17 15:50:41 +00:00
|
|
|
}
|
|
|
|
|
2004-06-26 20:15:34 +00:00
|
|
|
unsigned
|
|
|
|
ospf_pkt_maxsize(struct ospf_iface *ifa)
|
|
|
|
{
|
2005-03-15 20:51:33 +00:00
|
|
|
unsigned mtu = (ifa->type == OSPF_IT_VLINK) ? OSPF_VLINK_MTU : ifa->iface->mtu;
|
2010-11-04 16:22:43 +00:00
|
|
|
unsigned headers = SIZE_OF_IP_HEADER;
|
2009-08-21 07:27:52 +00:00
|
|
|
|
2012-10-02 09:44:05 +00:00
|
|
|
/* For OSPFv2 */
|
2010-11-04 16:22:43 +00:00
|
|
|
if (ifa->autype == OSPF_AUTH_CRYPT)
|
|
|
|
headers += OSPF_AUTH_CRYPT_SIZE;
|
2009-08-21 07:27:52 +00:00
|
|
|
|
2010-11-04 16:22:43 +00:00
|
|
|
return mtu - headers;
|
2004-06-26 20:15:34 +00:00
|
|
|
}
|
|
|
|
|
2009-08-21 07:27:52 +00:00
|
|
|
|
2012-10-02 09:44:05 +00:00
|
|
|
/* We assume OSPFv2 in ospf_pkt_finalize() */
|
2009-08-21 07:27:52 +00:00
|
|
|
static void
|
2004-06-26 20:15:34 +00:00
|
|
|
ospf_pkt_finalize(struct ospf_iface *ifa, struct ospf_packet *pkt)
|
1999-11-17 15:50:41 +00:00
|
|
|
{
|
2008-11-08 16:24:23 +00:00
|
|
|
struct password_item *passwd = NULL;
|
2012-10-02 09:44:05 +00:00
|
|
|
union ospf_auth *auth = (void *) (pkt + 1);
|
|
|
|
unsigned plen = ntohs(pkt->length);
|
2004-06-26 20:15:34 +00:00
|
|
|
|
2011-05-18 23:20:00 +00:00
|
|
|
pkt->checksum = 0;
|
2012-10-02 09:44:05 +00:00
|
|
|
ospf_pkt_set_autype(pkt, ifa->autype);
|
|
|
|
bzero(auth, sizeof(union ospf_auth));
|
2011-05-18 23:20:00 +00:00
|
|
|
|
2012-10-02 09:44:05 +00:00
|
|
|
/* Compatibility note: auth may contain anything if autype is
|
2011-05-18 23:20:00 +00:00
|
|
|
none, but nonzero values do not work with Mikrotik OSPF */
|
2004-06-26 20:15:34 +00:00
|
|
|
|
2012-10-02 09:44:05 +00:00
|
|
|
switch (ifa->autype)
|
2004-06-26 20:15:34 +00:00
|
|
|
{
|
|
|
|
case OSPF_AUTH_SIMPLE:
|
2008-11-08 16:24:23 +00:00
|
|
|
passwd = password_find(ifa->passwords, 1);
|
2004-07-13 13:52:54 +00:00
|
|
|
if (!passwd)
|
|
|
|
{
|
2012-10-02 09:44:05 +00:00
|
|
|
log(L_ERR "No suitable password found for authentication");
|
2004-07-13 13:52:54 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-10-02 09:44:05 +00:00
|
|
|
password_cpy(auth->password, passwd->password, sizeof(union ospf_auth));
|
|
|
|
|
2004-07-01 15:01:44 +00:00
|
|
|
case OSPF_AUTH_NONE:
|
2012-10-02 09:44:05 +00:00
|
|
|
{
|
|
|
|
void *body = (void *) (auth + 1);
|
|
|
|
unsigned blen = plen - sizeof(struct ospf_packet) - sizeof(union ospf_auth);
|
|
|
|
pkt->checksum = ipsum_calculate(pkt, sizeof(struct ospf_packet), body, blen, NULL);
|
|
|
|
}
|
2004-06-26 20:15:34 +00:00
|
|
|
break;
|
2012-10-02 09:44:05 +00:00
|
|
|
|
2004-06-26 20:15:34 +00:00
|
|
|
case OSPF_AUTH_CRYPT:
|
2008-11-08 16:24:23 +00:00
|
|
|
passwd = password_find(ifa->passwords, 0);
|
2004-06-26 20:15:34 +00:00
|
|
|
if (!passwd)
|
|
|
|
{
|
|
|
|
log( L_ERR "No suitable password found for authentication" );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-04-08 18:15:01 +00:00
|
|
|
/* Perhaps use random value to prevent replay attacks after
|
|
|
|
reboot when system does not have independent RTC? */
|
2004-06-26 20:15:34 +00:00
|
|
|
if (!ifa->csn)
|
2009-04-08 18:15:01 +00:00
|
|
|
{
|
|
|
|
ifa->csn = (u32) now;
|
|
|
|
ifa->csn_use = now;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We must have sufficient delay between sending a packet and increasing
|
|
|
|
CSN to prevent reordering of packets (in a network) with different CSNs */
|
|
|
|
if ((now - ifa->csn_use) > 1)
|
|
|
|
ifa->csn++;
|
|
|
|
|
|
|
|
ifa->csn_use = now;
|
2004-06-26 20:15:34 +00:00
|
|
|
|
2012-10-02 09:44:05 +00:00
|
|
|
auth->md5.zero = 0;
|
|
|
|
auth->md5.keyid = passwd->id;
|
|
|
|
auth->md5.len = OSPF_AUTH_CRYPT_SIZE;
|
|
|
|
auth->md5.csn = htonl(ifa->csn);
|
|
|
|
|
|
|
|
{
|
|
|
|
void *tail = ((void *) pkt) + plen;
|
|
|
|
char password[OSPF_AUTH_CRYPT_SIZE];
|
|
|
|
password_cpy(password, passwd->password, OSPF_AUTH_CRYPT_SIZE);
|
|
|
|
|
|
|
|
struct MD5Context ctxt;
|
|
|
|
MD5Init(&ctxt);
|
|
|
|
MD5Update(&ctxt, (char *) pkt, plen);
|
|
|
|
MD5Update(&ctxt, password, OSPF_AUTH_CRYPT_SIZE);
|
|
|
|
MD5Final(tail, &ctxt);
|
|
|
|
}
|
2004-06-26 20:15:34 +00:00
|
|
|
break;
|
2012-10-02 09:44:05 +00:00
|
|
|
|
2004-06-26 20:15:34 +00:00
|
|
|
default:
|
|
|
|
bug("Unknown authentication type");
|
|
|
|
}
|
1999-11-17 15:50:41 +00:00
|
|
|
}
|
|
|
|
|
2012-10-02 09:44:05 +00:00
|
|
|
/* We assume OSPFv2 in ospf_pkt_checkauth() */
|
2004-06-05 09:58:23 +00:00
|
|
|
static int
|
2004-07-01 15:01:44 +00:00
|
|
|
ospf_pkt_checkauth(struct ospf_neighbor *n, struct ospf_iface *ifa, struct ospf_packet *pkt, int size)
|
2000-06-06 01:23:03 +00:00
|
|
|
{
|
2004-07-15 16:37:52 +00:00
|
|
|
struct proto_ospf *po = ifa->oa->po;
|
2012-10-02 09:44:05 +00:00
|
|
|
union ospf_auth *auth = (void *) (pkt + 1);
|
2004-06-26 20:15:34 +00:00
|
|
|
struct password_item *pass = NULL, *ptmp;
|
|
|
|
char password[OSPF_AUTH_CRYPT_SIZE];
|
|
|
|
|
2012-10-02 09:44:05 +00:00
|
|
|
unsigned plen = ntohs(pkt->length);
|
|
|
|
u16 autype = ospf_pkt_get_autype(pkt);
|
2004-06-26 20:15:34 +00:00
|
|
|
|
2012-10-02 09:44:05 +00:00
|
|
|
if (autype != ifa->autype)
|
2004-06-06 09:37:54 +00:00
|
|
|
{
|
2012-10-02 09:44:05 +00:00
|
|
|
OSPF_TRACE(D_PACKETS, "OSPF_auth: Method differs (%d)", autype);
|
2004-06-26 20:15:34 +00:00
|
|
|
return 0;
|
2004-06-06 09:37:54 +00:00
|
|
|
}
|
2000-06-06 01:23:03 +00:00
|
|
|
|
2012-10-02 09:44:05 +00:00
|
|
|
switch (autype)
|
2004-06-26 20:15:34 +00:00
|
|
|
{
|
|
|
|
case OSPF_AUTH_NONE:
|
|
|
|
return 1;
|
|
|
|
break;
|
2012-10-02 09:44:05 +00:00
|
|
|
|
2004-06-26 20:15:34 +00:00
|
|
|
case OSPF_AUTH_SIMPLE:
|
2008-11-08 16:24:23 +00:00
|
|
|
pass = password_find(ifa->passwords, 1);
|
2009-01-16 09:58:52 +00:00
|
|
|
if (!pass)
|
2004-06-26 20:15:34 +00:00
|
|
|
{
|
|
|
|
OSPF_TRACE(D_PACKETS, "OSPF_auth: no password found");
|
|
|
|
return 0;
|
|
|
|
}
|
2004-07-13 13:52:54 +00:00
|
|
|
password_cpy(password, pass->password, sizeof(union ospf_auth));
|
1999-11-17 15:50:41 +00:00
|
|
|
|
2012-10-02 09:44:05 +00:00
|
|
|
if (memcmp(auth->password, password, sizeof(union ospf_auth)))
|
2004-06-26 20:15:34 +00:00
|
|
|
{
|
2004-07-13 13:52:54 +00:00
|
|
|
char ppass[sizeof(union ospf_auth) + 1];
|
|
|
|
bzero(ppass, (sizeof(union ospf_auth) + 1));
|
2012-10-02 09:44:05 +00:00
|
|
|
memcpy(ppass, auth->password, sizeof(union ospf_auth));
|
2004-07-13 13:52:54 +00:00
|
|
|
OSPF_TRACE(D_PACKETS, "OSPF_auth: different passwords (%s)", ppass);
|
2004-06-26 20:15:34 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2004-07-13 13:52:54 +00:00
|
|
|
return 1;
|
2004-06-26 20:15:34 +00:00
|
|
|
break;
|
2012-10-02 09:44:05 +00:00
|
|
|
|
2004-06-26 20:15:34 +00:00
|
|
|
case OSPF_AUTH_CRYPT:
|
2012-10-02 09:44:05 +00:00
|
|
|
if (auth->md5.len != OSPF_AUTH_CRYPT_SIZE)
|
2004-06-26 20:15:34 +00:00
|
|
|
{
|
|
|
|
OSPF_TRACE(D_PACKETS, "OSPF_auth: wrong size of md5 digest");
|
|
|
|
return 0;
|
|
|
|
}
|
2009-01-16 09:58:52 +00:00
|
|
|
|
2012-10-02 09:44:05 +00:00
|
|
|
if (plen + OSPF_AUTH_CRYPT_SIZE > size)
|
2004-06-26 20:15:34 +00:00
|
|
|
{
|
2005-03-15 23:42:41 +00:00
|
|
|
OSPF_TRACE(D_PACKETS, "OSPF_auth: size mismatch (%d vs %d)",
|
2012-10-02 09:44:05 +00:00
|
|
|
plen + OSPF_AUTH_CRYPT_SIZE, size);
|
2004-06-26 20:15:34 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-02 09:44:05 +00:00
|
|
|
if (n)
|
|
|
|
{
|
|
|
|
u32 rcv_csn = ntohl(auth->md5.csn);
|
|
|
|
if(rcv_csn < n->csn)
|
|
|
|
{
|
|
|
|
OSPF_TRACE(D_PACKETS, "OSPF_auth: lower sequence number (rcv %d, old %d)", rcv_csn, n->csn);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
n->csn = rcv_csn;
|
|
|
|
}
|
2004-06-26 20:15:34 +00:00
|
|
|
|
2009-01-16 09:58:52 +00:00
|
|
|
if (ifa->passwords)
|
2004-06-26 20:15:34 +00:00
|
|
|
{
|
2009-01-16 09:58:52 +00:00
|
|
|
WALK_LIST(ptmp, *(ifa->passwords))
|
|
|
|
{
|
2012-10-02 09:44:05 +00:00
|
|
|
if (auth->md5.keyid != ptmp->id) continue;
|
2009-01-16 09:58:52 +00:00
|
|
|
if ((ptmp->accfrom > now_real) || (ptmp->accto < now_real)) continue;
|
|
|
|
pass = ptmp;
|
|
|
|
break;
|
|
|
|
}
|
2004-06-26 20:15:34 +00:00
|
|
|
}
|
|
|
|
|
2009-01-16 09:58:52 +00:00
|
|
|
if (!pass)
|
2004-06-26 20:15:34 +00:00
|
|
|
{
|
|
|
|
OSPF_TRACE(D_PACKETS, "OSPF_auth: no suitable md5 password found");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-02 09:44:05 +00:00
|
|
|
password_cpy(password, pass->password, OSPF_AUTH_CRYPT_SIZE);
|
|
|
|
|
2004-06-26 20:15:34 +00:00
|
|
|
{
|
2012-10-02 09:44:05 +00:00
|
|
|
void *tail = ((void *) pkt) + plen;
|
|
|
|
char md5sum[OSPF_AUTH_CRYPT_SIZE];
|
|
|
|
|
|
|
|
struct MD5Context ctxt;
|
|
|
|
MD5Init(&ctxt);
|
|
|
|
MD5Update(&ctxt, (char *) pkt, plen);
|
|
|
|
MD5Update(&ctxt, password, OSPF_AUTH_CRYPT_SIZE);
|
|
|
|
MD5Final(md5sum, &ctxt);
|
|
|
|
if (memcmp(md5sum, tail, OSPF_AUTH_CRYPT_SIZE))
|
2009-04-08 18:15:01 +00:00
|
|
|
{
|
2012-10-02 09:44:05 +00:00
|
|
|
OSPF_TRACE(D_PACKETS, "OSPF_auth: wrong md5 digest");
|
2009-04-08 18:15:01 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2004-06-26 20:15:34 +00:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
break;
|
2012-10-02 09:44:05 +00:00
|
|
|
|
2004-06-26 20:15:34 +00:00
|
|
|
default:
|
|
|
|
OSPF_TRACE(D_PACKETS, "OSPF_auth: unknown auth type");
|
|
|
|
return 0;
|
|
|
|
}
|
1999-11-17 15:50:41 +00:00
|
|
|
}
|
|
|
|
|
2009-08-21 07:27:52 +00:00
|
|
|
|
2000-06-07 21:56:32 +00:00
|
|
|
/**
|
|
|
|
* ospf_rx_hook
|
2010-02-11 09:23:35 +00:00
|
|
|
* @sk: socket we received the packet.
|
2000-06-07 21:56:32 +00:00
|
|
|
* @size: size of the packet
|
|
|
|
*
|
2003-08-23 10:42:41 +00:00
|
|
|
* This is the entry point for messages from neighbors. Many checks (like
|
|
|
|
* authentication, checksums, size) are done before the packet is passed to
|
2000-06-07 21:56:32 +00:00
|
|
|
* non generic functions.
|
|
|
|
*/
|
1999-11-17 15:50:41 +00:00
|
|
|
int
|
2010-02-11 09:23:35 +00:00
|
|
|
ospf_rx_hook(sock *sk, int size)
|
1999-11-17 15:50:41 +00:00
|
|
|
{
|
2010-02-11 09:23:35 +00:00
|
|
|
char *mesg = "OSPF: Bad packet from ";
|
1999-11-17 15:50:41 +00:00
|
|
|
|
2010-03-11 17:07:24 +00:00
|
|
|
/* We want just packets from sk->iface. Unfortunately, on BSD we
|
|
|
|
cannot filter out other packets at kernel level and we receive
|
|
|
|
all packets on all sockets */
|
|
|
|
if (sk->lifindex != sk->iface->index)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
DBG("OSPF: RX hook called (iface %s, src %I, dst %I)\n",
|
|
|
|
sk->iface->name, sk->faddr, sk->laddr);
|
|
|
|
|
|
|
|
/* Initially, the packet is associated with the 'master' iface */
|
|
|
|
struct ospf_iface *ifa = sk->data;
|
|
|
|
struct proto_ospf *po = ifa->oa->po;
|
2010-03-11 17:55:59 +00:00
|
|
|
// struct proto *p = &po->proto;
|
2010-03-11 17:07:24 +00:00
|
|
|
|
2011-03-28 23:41:46 +00:00
|
|
|
int src_local, dst_local UNUSED, dst_mcast;
|
|
|
|
src_local = ipa_in_net(sk->faddr, ifa->addr->prefix, ifa->addr->pxlen);
|
|
|
|
dst_local = ipa_equal(sk->laddr, ifa->addr->ip);
|
2012-10-02 09:44:05 +00:00
|
|
|
dst_mcast = ipa_equal(sk->laddr, ifa->all_routers) || ipa_equal(sk->laddr, ifa->des_routers);
|
2003-09-14 13:41:24 +00:00
|
|
|
|
2012-10-02 09:44:05 +00:00
|
|
|
if (ospf_is_v2(po))
|
|
|
|
{
|
|
|
|
/* First, we eliminate packets with strange address combinations.
|
|
|
|
* In OSPFv2, they might be for other ospf_ifaces (with different IP
|
|
|
|
* prefix) on the same real iface, so we don't log it. We enforce
|
|
|
|
* that (src_local || dst_local), therefore we are eliminating all
|
|
|
|
* such cases.
|
|
|
|
*/
|
|
|
|
if (dst_mcast && !src_local)
|
|
|
|
return 1;
|
|
|
|
if (!dst_mcast && !dst_local)
|
|
|
|
return 1;
|
2010-03-11 17:07:24 +00:00
|
|
|
|
2012-10-02 09:44:05 +00:00
|
|
|
/* Ignore my own broadcast packets */
|
|
|
|
if (ifa->cf->real_bcast && ipa_equal(sk->faddr, ifa->addr->ip))
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* In OSPFv3, src_local and dst_local mean link-local.
|
|
|
|
* RFC 5340 says that local (non-vlink) packets use
|
|
|
|
* link-local src address, but does not enforce it. Strange.
|
|
|
|
*/
|
|
|
|
if (dst_mcast && !src_local)
|
|
|
|
log(L_WARN "OSPF: Received multicast packet from %I (not link-local)", sk->faddr);
|
|
|
|
}
|
2010-03-11 17:07:24 +00:00
|
|
|
|
|
|
|
/* Second, we check packet size, checksum, and the protocol version */
|
2012-10-02 09:44:05 +00:00
|
|
|
struct ospf_packet *pkt = (struct ospf_packet *) ip_skip_header(sk->rbuf, &size);
|
2004-07-14 21:46:20 +00:00
|
|
|
|
2012-10-02 09:44:05 +00:00
|
|
|
if (pkt == NULL)
|
2009-01-11 09:51:54 +00:00
|
|
|
{
|
|
|
|
log(L_ERR "%s%I - bad IP header", mesg, sk->faddr);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2004-06-06 09:37:54 +00:00
|
|
|
if ((unsigned) size < sizeof(struct ospf_packet))
|
|
|
|
{
|
2004-06-06 19:53:52 +00:00
|
|
|
log(L_ERR "%s%I - too short (%u bytes)", mesg, sk->faddr, size);
|
|
|
|
return 1;
|
2004-06-06 09:37:54 +00:00
|
|
|
}
|
2003-09-14 13:41:24 +00:00
|
|
|
|
2012-10-02 09:44:05 +00:00
|
|
|
unsigned plen = ntohs(pkt->length);
|
|
|
|
if (plen < sizeof(struct ospf_packet))
|
2011-04-13 11:19:37 +00:00
|
|
|
{
|
2012-10-02 09:44:05 +00:00
|
|
|
log(L_ERR "%s%I - too low value in size field (%u bytes)", mesg, sk->faddr, plen);
|
2011-04-13 11:19:37 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-10-02 09:44:05 +00:00
|
|
|
if ((plen > size) || ((plen % 4) != 0))
|
2004-06-06 09:37:54 +00:00
|
|
|
{
|
2012-10-02 09:44:05 +00:00
|
|
|
log(L_ERR "%s%I - size field does not match (%d/%d)", mesg, sk->faddr, plen, size);
|
2004-06-06 19:53:52 +00:00
|
|
|
return 1;
|
2004-06-06 09:37:54 +00:00
|
|
|
}
|
2003-09-14 13:41:24 +00:00
|
|
|
|
2010-02-11 09:23:35 +00:00
|
|
|
if ((unsigned) size > sk->rbsize)
|
|
|
|
{
|
|
|
|
log(L_ERR "%s%I - too large (%d vs %d)", mesg, sk->faddr, size, sk->rbsize);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-10-19 14:26:51 +00:00
|
|
|
if (pkt->version != ospf_get_version(po))
|
2004-06-06 09:37:54 +00:00
|
|
|
{
|
2012-10-02 09:44:05 +00:00
|
|
|
log(L_ERR "%s%I - version %u", mesg, sk->faddr, pkt->version);
|
2004-06-06 19:53:52 +00:00
|
|
|
return 1;
|
2004-06-06 09:37:54 +00:00
|
|
|
}
|
2003-09-14 13:41:24 +00:00
|
|
|
|
2012-10-02 09:44:05 +00:00
|
|
|
if (ospf_is_v2(po) && ospf_pkt_get_autype(pkt) != OSPF_AUTH_CRYPT)
|
2004-06-06 09:37:54 +00:00
|
|
|
{
|
2012-10-02 09:44:05 +00:00
|
|
|
unsigned hlen = sizeof(struct ospf_packet) - sizeof(union ospf_auth);
|
|
|
|
unsigned blen = plen - hlen;
|
|
|
|
void *body = ((void *) pkt) + hlen;
|
2003-09-14 13:41:24 +00:00
|
|
|
|
2012-10-02 09:44:05 +00:00
|
|
|
if (! ipsum_verify(pkt, sizeof(struct ospf_packet), body, blen, NULL))
|
|
|
|
{
|
|
|
|
log(L_ERR "%s%I - bad checksum", mesg, sk->faddr);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2010-02-11 09:23:35 +00:00
|
|
|
|
2010-03-11 17:07:24 +00:00
|
|
|
/* Third, we resolve associated iface and handle vlinks. */
|
|
|
|
|
2012-10-02 09:44:05 +00:00
|
|
|
u32 areaid = ntohl(pkt->areaid);
|
|
|
|
u32 rid = ntohl(pkt->routerid);
|
|
|
|
u8 instance_id = ospf_is_v2(po) ? 0 : ospf_pkt_get_instance_id(pkt);
|
2010-03-11 17:07:24 +00:00
|
|
|
|
2012-10-02 09:44:05 +00:00
|
|
|
if ((areaid == ifa->oa->areaid) &&
|
|
|
|
(instance_id == ifa->instance_id))
|
2010-03-11 17:07:24 +00:00
|
|
|
{
|
|
|
|
/* It is real iface, source should be local (in OSPFv2) */
|
2012-10-02 09:44:05 +00:00
|
|
|
if (ospf_is_v2(po) && !src_local)
|
2010-03-11 17:07:24 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else if (dst_mcast || (areaid != 0))
|
|
|
|
{
|
|
|
|
/* Obvious mismatch */
|
|
|
|
|
2012-10-02 09:44:05 +00:00
|
|
|
/* We ignore mismatch in OSPFv3 when there might be
|
|
|
|
another instance with a different instance ID */
|
|
|
|
if (instance_id == ifa->instance_id)
|
|
|
|
log(L_ERR "%s%I - area does not match (%R vs %R)",
|
|
|
|
mesg, sk->faddr, areaid, ifa->oa->areaid);
|
2010-03-11 17:07:24 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else
|
2004-06-06 09:37:54 +00:00
|
|
|
{
|
2010-03-11 17:07:24 +00:00
|
|
|
/* Some vlink? */
|
|
|
|
struct ospf_iface *iff = NULL;
|
|
|
|
|
|
|
|
WALK_LIST(iff, po->iface_list)
|
2010-02-11 09:23:35 +00:00
|
|
|
{
|
2010-03-11 17:07:24 +00:00
|
|
|
if ((iff->type == OSPF_IT_VLINK) &&
|
|
|
|
(iff->voa == ifa->oa) &&
|
2012-10-02 09:44:05 +00:00
|
|
|
(iff->instance_id == instance_id) &&
|
2010-03-11 17:07:24 +00:00
|
|
|
(iff->vid == rid))
|
|
|
|
{
|
|
|
|
/* Vlink should be UP */
|
|
|
|
if (iff->state != OSPF_IS_PTP)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
ifa = iff;
|
|
|
|
goto found;
|
|
|
|
}
|
2010-02-11 09:23:35 +00:00
|
|
|
}
|
|
|
|
|
2012-10-02 09:44:05 +00:00
|
|
|
/* FIXME: this warning is strange - NBMA could trigger it too */
|
|
|
|
if (ospf_is_v2(po))
|
|
|
|
log(L_WARN "OSPF: Received packet for unknown vlink (ID %R, IP %I)", rid, sk->faddr);
|
|
|
|
|
2004-06-06 19:53:52 +00:00
|
|
|
return 1;
|
2004-06-06 09:37:54 +00:00
|
|
|
}
|
2003-09-14 13:41:24 +00:00
|
|
|
|
2010-03-11 17:07:24 +00:00
|
|
|
found:
|
2010-02-11 09:23:35 +00:00
|
|
|
if (ifa->stub) /* This shouldn't happen */
|
|
|
|
return 1;
|
|
|
|
|
2012-10-02 09:44:05 +00:00
|
|
|
if (ipa_equal(sk->laddr, ifa->des_routers) && (ifa->sk_dr == 0))
|
2009-08-21 07:27:52 +00:00
|
|
|
return 1;
|
|
|
|
|
2010-03-11 17:07:24 +00:00
|
|
|
if (rid == po->router_id)
|
2004-06-06 09:37:54 +00:00
|
|
|
{
|
2004-06-06 19:53:52 +00:00
|
|
|
log(L_ERR "%s%I - received my own router ID!", mesg, sk->faddr);
|
|
|
|
return 1;
|
2004-06-06 09:37:54 +00:00
|
|
|
}
|
2003-09-14 13:41:24 +00:00
|
|
|
|
2010-03-11 17:07:24 +00:00
|
|
|
if (rid == 0)
|
2004-06-06 09:37:54 +00:00
|
|
|
{
|
2004-06-06 19:53:52 +00:00
|
|
|
log(L_ERR "%s%I - router id = 0.0.0.0", mesg, sk->faddr);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2010-04-26 17:08:57 +00:00
|
|
|
/* In OSPFv2, neighbors are identified by either IP or Router ID, base on network type */
|
2012-10-02 09:44:05 +00:00
|
|
|
unsigned t = ifa->type;
|
2010-04-26 17:08:57 +00:00
|
|
|
struct ospf_neighbor *n;
|
2012-10-02 09:44:05 +00:00
|
|
|
if (ospf_is_v2(po) && ((t == OSPF_IT_BCAST) || (t == OSPF_IT_NBMA) || (t == OSPF_IT_PTMP)))
|
2010-04-26 17:08:57 +00:00
|
|
|
n = find_neigh_by_ip(ifa, sk->faddr);
|
|
|
|
else
|
|
|
|
n = find_neigh(ifa, rid);
|
2004-06-06 19:53:52 +00:00
|
|
|
|
2012-10-02 09:44:05 +00:00
|
|
|
if (!n && (pkt->type != HELLO_P))
|
2004-06-06 19:53:52 +00:00
|
|
|
{
|
2010-10-22 06:35:19 +00:00
|
|
|
log(L_WARN "OSPF: Received non-hello packet from unknown neighbor (src %I, iface %s)",
|
2010-02-11 09:23:35 +00:00
|
|
|
sk->faddr, ifa->iface->name);
|
2004-06-06 19:53:52 +00:00
|
|
|
return 1;
|
2004-06-06 09:37:54 +00:00
|
|
|
}
|
2003-09-14 13:41:24 +00:00
|
|
|
|
2012-10-02 09:44:05 +00:00
|
|
|
if (ospf_is_v2(po) && ! ospf_pkt_checkauth(n, ifa, pkt, size))
|
2004-06-26 20:15:34 +00:00
|
|
|
{
|
2010-10-22 06:35:19 +00:00
|
|
|
log(L_ERR "%s%I - authentication failed", mesg, sk->faddr);
|
2004-06-26 20:15:34 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2000-06-07 21:56:32 +00:00
|
|
|
/* Dump packet
|
2003-09-14 13:41:24 +00:00
|
|
|
pu8=(u8 *)(sk->rbuf+5*4);
|
2012-10-02 09:44:05 +00:00
|
|
|
for(i=0;i<ntohs(pkt->length);i+=4)
|
2003-09-14 13:41:24 +00:00
|
|
|
DBG("%s: received %u,%u,%u,%u\n",p->name, pu8[i+0], pu8[i+1], pu8[i+2],
|
|
|
|
pu8[i+3]);
|
|
|
|
DBG("%s: received size: %u\n",p->name,size);
|
|
|
|
*/
|
|
|
|
|
2012-10-02 09:44:05 +00:00
|
|
|
switch (pkt->type)
|
2004-06-06 09:37:54 +00:00
|
|
|
{
|
|
|
|
case HELLO_P:
|
|
|
|
DBG("%s: Hello received.\n", p->name);
|
2012-10-02 09:44:05 +00:00
|
|
|
ospf_hello_receive(pkt, ifa, n, sk->faddr);
|
2004-06-06 09:37:54 +00:00
|
|
|
break;
|
|
|
|
case DBDES_P:
|
|
|
|
DBG("%s: Database description received.\n", p->name);
|
2012-10-02 09:44:05 +00:00
|
|
|
ospf_dbdes_receive(pkt, ifa, n);
|
2004-06-06 09:37:54 +00:00
|
|
|
break;
|
|
|
|
case LSREQ_P:
|
|
|
|
DBG("%s: Link state request received.\n", p->name);
|
2012-10-02 09:44:05 +00:00
|
|
|
ospf_lsreq_receive(pkt, ifa, n);
|
2004-06-06 09:37:54 +00:00
|
|
|
break;
|
|
|
|
case LSUPD_P:
|
|
|
|
DBG("%s: Link state update received.\n", p->name);
|
2012-10-02 09:44:05 +00:00
|
|
|
ospf_lsupd_receive(pkt, ifa, n);
|
2004-06-06 09:37:54 +00:00
|
|
|
break;
|
|
|
|
case LSACK_P:
|
|
|
|
DBG("%s: Link state ack received.\n", p->name);
|
2012-10-02 09:44:05 +00:00
|
|
|
ospf_lsack_receive(pkt, ifa, n);
|
2004-06-06 09:37:54 +00:00
|
|
|
break;
|
|
|
|
default:
|
2012-10-02 09:44:05 +00:00
|
|
|
log(L_ERR "%s%I - wrong type %u", mesg, sk->faddr, pkt->type);
|
2004-06-06 19:53:52 +00:00
|
|
|
return 1;
|
2004-06-06 09:37:54 +00:00
|
|
|
};
|
2004-06-06 19:53:52 +00:00
|
|
|
return 1;
|
1999-11-17 15:50:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2004-06-06 09:37:54 +00:00
|
|
|
ospf_tx_hook(sock * sk)
|
1999-11-17 15:50:41 +00:00
|
|
|
{
|
2012-01-22 10:03:30 +00:00
|
|
|
struct ospf_iface *ifa= (struct ospf_iface *) (sk->data);
|
2010-02-11 09:23:35 +00:00
|
|
|
// struct proto *p = (struct proto *) (ifa->oa->po);
|
2012-01-22 10:03:30 +00:00
|
|
|
log(L_ERR "OSPF: TX hook called on %s", ifa->iface->name);
|
1999-11-17 15:50:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2005-02-14 21:34:46 +00:00
|
|
|
ospf_err_hook(sock * sk, int err)
|
1999-11-17 15:50:41 +00:00
|
|
|
{
|
2012-01-22 10:03:30 +00:00
|
|
|
struct ospf_iface *ifa= (struct ospf_iface *) (sk->data);
|
2010-02-11 09:23:35 +00:00
|
|
|
// struct proto *p = (struct proto *) (ifa->oa->po);
|
2012-01-22 10:03:30 +00:00
|
|
|
log(L_ERR "OSPF: Socket error on %s: %M", ifa->iface->name, err);
|
1999-11-17 15:50:41 +00:00
|
|
|
}
|
|
|
|
|
2012-10-02 09:44:05 +00:00
|
|
|
void
|
|
|
|
ospf_send_to(struct ospf_iface *ifa, ip_addr dst)
|
|
|
|
{
|
|
|
|
sock *sk = ifa->sk;
|
|
|
|
struct ospf_packet *pkt = (struct ospf_packet *) sk->tbuf;
|
|
|
|
int plen = ntohs(pkt->length);
|
|
|
|
|
|
|
|
if (sk->tbuf != sk->tpos)
|
|
|
|
log(L_ERR "Aiee, old packet was overwritten in TX buffer");
|
|
|
|
|
|
|
|
if (ospf_is_v2(ifa->oa->po))
|
|
|
|
{
|
|
|
|
if (ifa->autype == OSPF_AUTH_CRYPT)
|
|
|
|
plen += OSPF_AUTH_CRYPT_SIZE;
|
|
|
|
|
|
|
|
ospf_pkt_finalize(ifa, pkt);
|
|
|
|
}
|
|
|
|
|
|
|
|
sk_send_to(sk, plen, dst, 0);
|
|
|
|
}
|
|
|
|
|
2000-04-18 01:06:16 +00:00
|
|
|
void
|
2009-09-04 09:06:51 +00:00
|
|
|
ospf_send_to_agt(struct ospf_iface *ifa, u8 state)
|
2000-04-18 01:06:16 +00:00
|
|
|
{
|
|
|
|
struct ospf_neighbor *n;
|
|
|
|
|
2010-02-11 09:23:35 +00:00
|
|
|
WALK_LIST(n, ifa->neigh_list)
|
|
|
|
if (n->state >= state)
|
|
|
|
ospf_send_to(ifa, n->ip);
|
2003-09-14 13:41:24 +00:00
|
|
|
}
|
2000-06-06 00:08:27 +00:00
|
|
|
|
|
|
|
void
|
2009-09-04 09:06:51 +00:00
|
|
|
ospf_send_to_bdr(struct ospf_iface *ifa)
|
2000-06-06 00:08:27 +00:00
|
|
|
{
|
2012-10-02 09:44:05 +00:00
|
|
|
if (ipa_nonzero(ifa->drip))
|
2009-09-04 09:06:51 +00:00
|
|
|
ospf_send_to(ifa, ifa->drip);
|
2012-10-02 09:44:05 +00:00
|
|
|
if (ipa_nonzero(ifa->bdrip))
|
2009-09-04 09:06:51 +00:00
|
|
|
ospf_send_to(ifa, ifa->bdrip);
|
2000-06-06 00:08:27 +00:00
|
|
|
}
|