OSPF: Use message authentication interface
Based on former commit from Pavel Tvrdik
This commit is contained in:
parent
390601f038
commit
29239ba2bb
4 changed files with 94 additions and 73 deletions
|
@ -42,6 +42,20 @@ ospf_iface_finish(void)
|
||||||
|
|
||||||
if ((ip->autype == OSPF_AUTH_NONE) && (ip->passwords != NULL))
|
if ((ip->autype == OSPF_AUTH_NONE) && (ip->passwords != NULL))
|
||||||
log(L_WARN "Password option without authentication option does not make sense");
|
log(L_WARN "Password option without authentication option does not make sense");
|
||||||
|
|
||||||
|
if (ip->passwords)
|
||||||
|
{
|
||||||
|
struct password_item *pass;
|
||||||
|
WALK_LIST(pass, *ip->passwords)
|
||||||
|
{
|
||||||
|
if (pass->alg && (ip->autype != OSPF_AUTH_CRYPT))
|
||||||
|
cf_error("Password algorithm option requires cryptographic authentication");
|
||||||
|
|
||||||
|
/* Set default OSPF crypto algorithms */
|
||||||
|
if (!pass->alg && (ip->autype == OSPF_AUTH_CRYPT))
|
||||||
|
pass->alg = ospf_cfg_is_v2() ? ALG_MD5 : ALG_HMAC_SHA256;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "ospf.h"
|
#include "ospf.h"
|
||||||
|
#include "nest/password.h"
|
||||||
|
|
||||||
|
|
||||||
const char *ospf_is_names[] = {
|
const char *ospf_is_names[] = {
|
||||||
|
@ -51,6 +52,18 @@ ifa_tx_length(struct ospf_iface *ifa)
|
||||||
return ifa->cf->tx_length ?: ifa->iface->mtu;
|
return ifa->cf->tx_length ?: ifa->iface->mtu;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static inline uint
|
||||||
|
ifa_tx_hdrlen(struct ospf_iface *ifa)
|
||||||
|
{
|
||||||
|
uint hlen = SIZE_OF_IP_HEADER;
|
||||||
|
|
||||||
|
/* Relevant just for OSPFv2 */
|
||||||
|
if (ifa->autype == OSPF_AUTH_CRYPT)
|
||||||
|
hlen += max_mac_length(ifa->passwords);
|
||||||
|
|
||||||
|
return hlen;
|
||||||
|
}
|
||||||
|
|
||||||
static inline uint
|
static inline uint
|
||||||
ifa_bufsize(struct ospf_iface *ifa)
|
ifa_bufsize(struct ospf_iface *ifa)
|
||||||
{
|
{
|
||||||
|
@ -67,11 +80,7 @@ ifa_flood_queue_size(struct ospf_iface *ifa)
|
||||||
int
|
int
|
||||||
ospf_iface_assure_bufsize(struct ospf_iface *ifa, uint plen)
|
ospf_iface_assure_bufsize(struct ospf_iface *ifa, uint plen)
|
||||||
{
|
{
|
||||||
plen += SIZE_OF_IP_HEADER;
|
plen += ifa->tx_hdrlen;
|
||||||
|
|
||||||
/* This is relevant just for OSPFv2 */
|
|
||||||
if (ifa->autype == OSPF_AUTH_CRYPT)
|
|
||||||
plen += OSPF_AUTH_CRYPT_SIZE;
|
|
||||||
|
|
||||||
if (plen <= ifa->sk->tbsize)
|
if (plen <= ifa->sk->tbsize)
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -574,6 +583,7 @@ ospf_iface_new(struct ospf_area *oa, struct ifa *addr, struct ospf_iface_patt *i
|
||||||
ifa->stub = ospf_iface_stubby(ip, addr);
|
ifa->stub = ospf_iface_stubby(ip, addr);
|
||||||
ifa->ioprob = OSPF_I_OK;
|
ifa->ioprob = OSPF_I_OK;
|
||||||
ifa->tx_length = ifa_tx_length(ifa);
|
ifa->tx_length = ifa_tx_length(ifa);
|
||||||
|
ifa->tx_hdrlen = ifa_tx_hdrlen(ifa);
|
||||||
ifa->check_link = ip->check_link;
|
ifa->check_link = ip->check_link;
|
||||||
ifa->ecmp_weight = ip->ecmp_weight;
|
ifa->ecmp_weight = ip->ecmp_weight;
|
||||||
ifa->check_ttl = (ip->ttl_security == 1);
|
ifa->check_ttl = (ip->ttl_security == 1);
|
||||||
|
@ -684,6 +694,7 @@ ospf_iface_new_vlink(struct ospf_proto *p, struct ospf_iface_patt *ip)
|
||||||
ifa->deadint = ip->deadint;
|
ifa->deadint = ip->deadint;
|
||||||
ifa->inftransdelay = ip->inftransdelay;
|
ifa->inftransdelay = ip->inftransdelay;
|
||||||
ifa->tx_length = ospf_is_v2(p) ? IP4_MIN_MTU : IP6_MIN_MTU;
|
ifa->tx_length = ospf_is_v2(p) ? IP4_MIN_MTU : IP6_MIN_MTU;
|
||||||
|
ifa->tx_hdrlen = ifa_tx_hdrlen(ifa);
|
||||||
ifa->autype = ip->autype;
|
ifa->autype = ip->autype;
|
||||||
ifa->passwords = ip->passwords;
|
ifa->passwords = ip->passwords;
|
||||||
ifa->instance_id = ip->instance_id;
|
ifa->instance_id = ip->instance_id;
|
||||||
|
@ -824,6 +835,9 @@ ospf_iface_reconfigure(struct ospf_iface *ifa, struct ospf_iface_patt *new)
|
||||||
/* Update passwords */
|
/* Update passwords */
|
||||||
ifa->passwords = new->passwords;
|
ifa->passwords = new->passwords;
|
||||||
|
|
||||||
|
/* Update header length */
|
||||||
|
ifa->tx_hdrlen = ifa_tx_hdrlen(ifa);
|
||||||
|
|
||||||
/* Remaining options are just for proper interfaces */
|
/* Remaining options are just for proper interfaces */
|
||||||
if (ifa->type == OSPF_IT_VLINK)
|
if (ifa->type == OSPF_IT_VLINK)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
|
@ -180,11 +180,7 @@ struct ospf_iface_patt
|
||||||
|
|
||||||
#define OSPF_RXBUF_MINSIZE 256 /* Minimal allowed size */
|
#define OSPF_RXBUF_MINSIZE 256 /* Minimal allowed size */
|
||||||
u8 instance_id;
|
u8 instance_id;
|
||||||
u8 autype; /* Not really used in OSPFv3 */
|
u8 autype; /* OSPF_AUTH_*, not really used in OSPFv3 */
|
||||||
#define OSPF_AUTH_NONE 0
|
|
||||||
#define OSPF_AUTH_SIMPLE 1
|
|
||||||
#define OSPF_AUTH_CRYPT 2
|
|
||||||
#define OSPF_AUTH_CRYPT_SIZE 16
|
|
||||||
u8 strictnbma;
|
u8 strictnbma;
|
||||||
u8 check_link;
|
u8 check_link;
|
||||||
u8 ecmp_weight;
|
u8 ecmp_weight;
|
||||||
|
@ -334,6 +330,7 @@ struct ospf_iface
|
||||||
u8 marked; /* Used in OSPF reconfigure, 2 for force restart */
|
u8 marked; /* Used in OSPF reconfigure, 2 for force restart */
|
||||||
u16 rxbuf; /* Buffer size */
|
u16 rxbuf; /* Buffer size */
|
||||||
u16 tx_length; /* Soft TX packet length limit, usually MTU */
|
u16 tx_length; /* Soft TX packet length limit, usually MTU */
|
||||||
|
u16 tx_hdrlen; /* Expected packet header length, less than tx_length */
|
||||||
u8 check_link; /* Whether iface link change is used */
|
u8 check_link; /* Whether iface link change is used */
|
||||||
u8 ecmp_weight; /* Weight used for ECMP */
|
u8 ecmp_weight; /* Weight used for ECMP */
|
||||||
u8 link_lsa_suppression; /* Suppression of Link-LSA origination */
|
u8 link_lsa_suppression; /* Suppression of Link-LSA origination */
|
||||||
|
@ -424,6 +421,11 @@ struct ospf_neighbor
|
||||||
#define ISM_UNLOOP 5 /* Link up */
|
#define ISM_UNLOOP 5 /* Link up */
|
||||||
#define ISM_DOWN 6 /* Interface down */
|
#define ISM_DOWN 6 /* Interface down */
|
||||||
|
|
||||||
|
/* OSPF authentication types */
|
||||||
|
#define OSPF_AUTH_NONE 0
|
||||||
|
#define OSPF_AUTH_SIMPLE 1
|
||||||
|
#define OSPF_AUTH_CRYPT 2
|
||||||
|
|
||||||
|
|
||||||
/* OSPF neighbor states */
|
/* OSPF neighbor states */
|
||||||
#define NEIGHBOR_DOWN 0
|
#define NEIGHBOR_DOWN 0
|
||||||
|
@ -455,7 +457,6 @@ struct ospf_neighbor
|
||||||
#define TRANS_WAIT 2 /* Waiting before the end of translation */
|
#define TRANS_WAIT 2 /* Waiting before the end of translation */
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* Generic option flags */
|
/* Generic option flags */
|
||||||
#define OPT_V6 0x01 /* OSPFv3, LSA relevant for IPv6 routing calculation */
|
#define OPT_V6 0x01 /* OSPFv3, LSA relevant for IPv6 routing calculation */
|
||||||
#define OPT_E 0x02 /* Related to AS-external LSAs */
|
#define OPT_E 0x02 /* Related to AS-external LSAs */
|
||||||
|
@ -491,7 +492,7 @@ struct ospf_packet
|
||||||
u8 autype; /* Undefined for OSPFv3 */
|
u8 autype; /* Undefined for OSPFv3 */
|
||||||
};
|
};
|
||||||
|
|
||||||
struct ospf_md5
|
struct ospf_auth_crypto
|
||||||
{
|
{
|
||||||
u16 zero;
|
u16 zero;
|
||||||
u8 keyid;
|
u8 keyid;
|
||||||
|
@ -502,7 +503,7 @@ struct ospf_md5
|
||||||
union ospf_auth
|
union ospf_auth
|
||||||
{
|
{
|
||||||
u8 password[8];
|
u8 password[8];
|
||||||
struct ospf_md5 md5;
|
struct ospf_auth_crypto c32;
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Packet types */
|
/* Packet types */
|
||||||
|
@ -896,7 +897,6 @@ void ospf_sh_neigh_info(struct ospf_neighbor *n);
|
||||||
|
|
||||||
/* packet.c */
|
/* packet.c */
|
||||||
void ospf_pkt_fill_hdr(struct ospf_iface *ifa, void *buf, u8 h_type);
|
void ospf_pkt_fill_hdr(struct ospf_iface *ifa, void *buf, u8 h_type);
|
||||||
uint ospf_pkt_maxsize(struct ospf_iface *ifa);
|
|
||||||
int ospf_rx_hook(sock * sk, uint size);
|
int ospf_rx_hook(sock * sk, uint size);
|
||||||
// void ospf_tx_hook(sock * sk);
|
// void ospf_tx_hook(sock * sk);
|
||||||
void ospf_err_hook(sock * sk, int err);
|
void ospf_err_hook(sock * sk, int err);
|
||||||
|
@ -905,6 +905,9 @@ void ospf_send_to(struct ospf_iface *ifa, ip_addr ip);
|
||||||
void ospf_send_to_agt(struct ospf_iface *ifa, u8 state);
|
void ospf_send_to_agt(struct ospf_iface *ifa, u8 state);
|
||||||
void ospf_send_to_bdr(struct ospf_iface *ifa);
|
void ospf_send_to_bdr(struct ospf_iface *ifa);
|
||||||
|
|
||||||
|
static inline uint ospf_pkt_maxsize(struct ospf_iface *ifa)
|
||||||
|
{ return ifa->tx_length - ifa->tx_hdrlen; }
|
||||||
|
|
||||||
static inline void ospf_send_to_all(struct ospf_iface *ifa)
|
static inline void ospf_send_to_all(struct ospf_iface *ifa)
|
||||||
{ ospf_send_to(ifa, ifa->all_routers); }
|
{ ospf_send_to(ifa, ifa->all_routers); }
|
||||||
|
|
||||||
|
|
|
@ -32,25 +32,12 @@ ospf_pkt_fill_hdr(struct ospf_iface *ifa, void *buf, u8 h_type)
|
||||||
pkt->autype = ifa->autype;
|
pkt->autype = ifa->autype;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint
|
|
||||||
ospf_pkt_maxsize(struct ospf_iface *ifa)
|
|
||||||
{
|
|
||||||
uint headers = SIZE_OF_IP_HEADER;
|
|
||||||
|
|
||||||
/* Relevant just for OSPFv2 */
|
|
||||||
if (ifa->autype == OSPF_AUTH_CRYPT)
|
|
||||||
headers += OSPF_AUTH_CRYPT_SIZE;
|
|
||||||
|
|
||||||
return ifa->tx_length - headers;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* We assume OSPFv2 in ospf_pkt_finalize() */
|
/* We assume OSPFv2 in ospf_pkt_finalize() */
|
||||||
static void
|
static void
|
||||||
ospf_pkt_finalize(struct ospf_iface *ifa, struct ospf_packet *pkt)
|
ospf_pkt_finalize(struct ospf_iface *ifa, struct ospf_packet *pkt, uint *plen)
|
||||||
{
|
{
|
||||||
struct password_item *passwd = NULL;
|
struct password_item *pass = NULL;
|
||||||
union ospf_auth *auth = (void *) (pkt + 1);
|
union ospf_auth *auth = (void *) (pkt + 1);
|
||||||
uint plen = ntohs(pkt->length);
|
|
||||||
|
|
||||||
pkt->checksum = 0;
|
pkt->checksum = 0;
|
||||||
pkt->autype = ifa->autype;
|
pkt->autype = ifa->autype;
|
||||||
|
@ -62,25 +49,25 @@ ospf_pkt_finalize(struct ospf_iface *ifa, struct ospf_packet *pkt)
|
||||||
switch (ifa->autype)
|
switch (ifa->autype)
|
||||||
{
|
{
|
||||||
case OSPF_AUTH_SIMPLE:
|
case OSPF_AUTH_SIMPLE:
|
||||||
passwd = password_find(ifa->passwords, 1);
|
pass = password_find(ifa->passwords, 1);
|
||||||
if (!passwd)
|
if (!pass)
|
||||||
{
|
{
|
||||||
log(L_ERR "No suitable password found for authentication");
|
log(L_ERR "No suitable password found for authentication");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
strncpy(auth->password, passwd->password, sizeof(auth->password));
|
strncpy(auth->password, pass->password, sizeof(auth->password));
|
||||||
|
|
||||||
case OSPF_AUTH_NONE:
|
case OSPF_AUTH_NONE:
|
||||||
{
|
{
|
||||||
void *body = (void *) (auth + 1);
|
void *body = (void *) (auth + 1);
|
||||||
uint blen = plen - sizeof(struct ospf_packet) - sizeof(union ospf_auth);
|
uint blen = *plen - sizeof(struct ospf_packet) - sizeof(union ospf_auth);
|
||||||
pkt->checksum = ipsum_calculate(pkt, sizeof(struct ospf_packet), body, blen, NULL);
|
pkt->checksum = ipsum_calculate(pkt, sizeof(struct ospf_packet), body, blen, NULL);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case OSPF_AUTH_CRYPT:
|
case OSPF_AUTH_CRYPT:
|
||||||
passwd = password_find(ifa->passwords, 0);
|
pass = password_find(ifa->passwords, 0);
|
||||||
if (!passwd)
|
if (!pass)
|
||||||
{
|
{
|
||||||
log(L_ERR "No suitable password found for authentication");
|
log(L_ERR "No suitable password found for authentication");
|
||||||
return;
|
return;
|
||||||
|
@ -101,20 +88,25 @@ ospf_pkt_finalize(struct ospf_iface *ifa, struct ospf_packet *pkt)
|
||||||
|
|
||||||
ifa->csn_use = now;
|
ifa->csn_use = now;
|
||||||
|
|
||||||
auth->md5.zero = 0;
|
uint auth_len = mac_type_length(pass->alg);
|
||||||
auth->md5.keyid = passwd->id;
|
byte *auth_tail = ((byte *) pkt + *plen);
|
||||||
auth->md5.len = OSPF_AUTH_CRYPT_SIZE;
|
*plen += auth_len;
|
||||||
auth->md5.csn = htonl(ifa->csn);
|
|
||||||
|
|
||||||
void *tail = ((void *) pkt) + plen;
|
ASSERT(*plen < ifa->sk->tbsize);
|
||||||
char password[OSPF_AUTH_CRYPT_SIZE];
|
|
||||||
strncpy(password, passwd->password, sizeof(password));
|
|
||||||
|
|
||||||
struct hash_context ctx;
|
auth->c32.zero = 0;
|
||||||
md5_init(&ctx);
|
auth->c32.keyid = pass->id;
|
||||||
md5_update(&ctx, (char *) pkt, plen);
|
auth->c32.len = auth_len;
|
||||||
md5_update(&ctx, password, OSPF_AUTH_CRYPT_SIZE);
|
auth->c32.csn = htonl(ifa->csn);
|
||||||
memcpy((byte *) tail, md5_final(&ctx), MD5_SIZE);
|
|
||||||
|
/* Append key for keyed hash, append padding for HMAC (RFC 5709 3.3) */
|
||||||
|
if (pass->alg < ALG_HMAC)
|
||||||
|
strncpy(auth_tail, pass->password, auth_len);
|
||||||
|
else
|
||||||
|
memset32(auth_tail, HMAC_MAGIC, auth_len / 4);
|
||||||
|
|
||||||
|
mac_fill(pass->alg, pass->password, pass->length,
|
||||||
|
(byte *) pkt, *plen, auth_tail);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
@ -155,13 +147,19 @@ ospf_pkt_checkauth(struct ospf_neighbor *n, struct ospf_iface *ifa, struct ospf_
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
case OSPF_AUTH_CRYPT:
|
case OSPF_AUTH_CRYPT:
|
||||||
if (auth->md5.len != OSPF_AUTH_CRYPT_SIZE)
|
pass = password_find_by_id(ifa->passwords, auth->c32.keyid);
|
||||||
DROP("invalid MD5 digest length", auth->md5.len);
|
if (!pass)
|
||||||
|
DROP("no suitable password found", auth->c32.keyid);
|
||||||
|
|
||||||
if (plen + OSPF_AUTH_CRYPT_SIZE > len)
|
uint auth_len = mac_type_length(pass->alg);
|
||||||
DROP("length mismatch", len);
|
|
||||||
|
|
||||||
u32 rcv_csn = ntohl(auth->md5.csn);
|
if (plen + auth->c32.len > len)
|
||||||
|
DROP("packet length mismatch", len);
|
||||||
|
|
||||||
|
if (auth->c32.len != auth_len)
|
||||||
|
DROP("wrong authentication length", auth->c32.len);
|
||||||
|
|
||||||
|
u32 rcv_csn = ntohl(auth->c32.csn);
|
||||||
if (n && (rcv_csn < n->csn))
|
if (n && (rcv_csn < n->csn))
|
||||||
// DROP("lower sequence number", rcv_csn);
|
// DROP("lower sequence number", rcv_csn);
|
||||||
{
|
{
|
||||||
|
@ -172,22 +170,19 @@ ospf_pkt_checkauth(struct ospf_neighbor *n, struct ospf_iface *ifa, struct ospf_
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
pass = password_find_by_id(ifa->passwords, auth->md5.keyid);
|
byte *auth_tail = ((byte *) pkt) + plen;
|
||||||
if (!pass)
|
byte *auth_data = alloca(auth_len);
|
||||||
DROP("no suitable password found", auth->md5.keyid);
|
memcpy(auth_data, auth_tail, auth_len);
|
||||||
|
|
||||||
byte *tail = ((byte *) pkt) + plen;
|
/* Append key for keyed hash, append padding for HMAC (RFC 5709 3.3) */
|
||||||
char received[OSPF_AUTH_CRYPT_SIZE];
|
if (pass->alg < ALG_HMAC)
|
||||||
memcpy(received, tail, OSPF_AUTH_CRYPT_SIZE);
|
strncpy(auth_tail, pass->password, auth_len);
|
||||||
strncpy(tail, pass->password, OSPF_AUTH_CRYPT_SIZE);
|
else
|
||||||
|
memset32(auth_tail, HMAC_MAGIC, auth_len / 4);
|
||||||
|
|
||||||
struct hash_context ctx;
|
if (!mac_verify(pass->alg, pass->password, pass->length,
|
||||||
md5_init(&ctx);
|
(byte *) pkt, plen + auth_len, auth_data))
|
||||||
md5_update(&ctx, (byte *) pkt, plen + OSPF_AUTH_CRYPT_SIZE);
|
DROP("wrong authentication code", pass->id);
|
||||||
char *computed = md5_final(&ctx);
|
|
||||||
|
|
||||||
if (memcmp(received, computed, OSPF_AUTH_CRYPT_SIZE))
|
|
||||||
DROP("wrong MD5 digest", pass->id);
|
|
||||||
|
|
||||||
if (n)
|
if (n)
|
||||||
n->csn = rcv_csn;
|
n->csn = rcv_csn;
|
||||||
|
@ -473,15 +468,10 @@ ospf_send_to(struct ospf_iface *ifa, ip_addr dst)
|
||||||
{
|
{
|
||||||
sock *sk = ifa->sk;
|
sock *sk = ifa->sk;
|
||||||
struct ospf_packet *pkt = (struct ospf_packet *) sk->tbuf;
|
struct ospf_packet *pkt = (struct ospf_packet *) sk->tbuf;
|
||||||
int plen = ntohs(pkt->length);
|
uint plen = ntohs(pkt->length);
|
||||||
|
|
||||||
if (ospf_is_v2(ifa->oa->po))
|
if (ospf_is_v2(ifa->oa->po))
|
||||||
{
|
ospf_pkt_finalize(ifa, pkt, &plen);
|
||||||
if (ifa->autype == OSPF_AUTH_CRYPT)
|
|
||||||
plen += OSPF_AUTH_CRYPT_SIZE;
|
|
||||||
|
|
||||||
ospf_pkt_finalize(ifa, pkt);
|
|
||||||
}
|
|
||||||
|
|
||||||
int done = sk_send_to(sk, plen, dst, 0);
|
int done = sk_send_to(sk, plen, dst, 0);
|
||||||
if (!done)
|
if (!done)
|
||||||
|
|
Loading…
Reference in a new issue