1999-05-11 17:53:45 +08:00
|
|
|
/*
|
|
|
|
* Rest in pieces - RIP protocol
|
|
|
|
*
|
|
|
|
* Copyright (c) 1999 Pavel Machek <pavel@ucw.cz>
|
2004-06-27 04:11:14 +08:00
|
|
|
* Copyright (c) 2004 Ondrej Filip <feela@network.cz>
|
1999-05-11 17:53:45 +08:00
|
|
|
*
|
2003-04-07 02:38:01 +08:00
|
|
|
* Bug fixes by Eric Leblond <eleblond@init-sys.com>, April 2003
|
|
|
|
*
|
1999-05-11 17:53:45 +08:00
|
|
|
* Can be freely distributed and used under the terms of the GNU GPL.
|
|
|
|
*/
|
|
|
|
|
2003-04-07 02:38:01 +08:00
|
|
|
#undef LOCAL_DEBUG
|
1999-05-11 17:53:45 +08:00
|
|
|
|
|
|
|
#include "nest/bird.h"
|
|
|
|
#include "nest/iface.h"
|
|
|
|
#include "nest/protocol.h"
|
|
|
|
#include "nest/route.h"
|
|
|
|
#include "lib/socket.h"
|
|
|
|
#include "lib/resource.h"
|
|
|
|
#include "lib/lists.h"
|
|
|
|
#include "lib/timer.h"
|
1999-06-01 03:16:22 +08:00
|
|
|
#include "lib/md5.h"
|
2000-04-01 07:30:21 +08:00
|
|
|
#include "lib/string.h"
|
1999-05-11 17:53:45 +08:00
|
|
|
|
|
|
|
#include "rip.h"
|
|
|
|
|
|
|
|
#define P ((struct rip_proto *) p)
|
|
|
|
#define P_CF ((struct rip_proto_config *)p->cf)
|
|
|
|
|
1999-08-20 17:59:39 +08:00
|
|
|
#define PACKETLEN(num) (num * sizeof(struct rip_block) + sizeof(struct rip_packet_heading))
|
|
|
|
|
2000-06-06 01:13:36 +08:00
|
|
|
/*
|
2000-06-05 20:52:57 +08:00
|
|
|
* rip_incoming_authentication - check authentication of incomming packet and return 1 if there's problem.
|
|
|
|
*/
|
1999-05-11 17:53:45 +08:00
|
|
|
int
|
1999-12-01 20:52:57 +08:00
|
|
|
rip_incoming_authentication( struct proto *p, struct rip_block_auth *block, struct rip_packet *packet, int num, ip_addr whotoldme )
|
1999-05-11 17:53:45 +08:00
|
|
|
{
|
|
|
|
DBG( "Incoming authentication: " );
|
2003-04-07 02:38:01 +08:00
|
|
|
switch (ntohs(block->authtype)) { /* Authentication type */
|
1999-11-25 22:54:08 +08:00
|
|
|
case AT_PLAINTEXT:
|
|
|
|
{
|
2004-06-27 04:11:14 +08:00
|
|
|
struct password_item *passwd = password_find(P_CF->passwords);
|
1999-11-25 22:54:08 +08:00
|
|
|
DBG( "Plaintext passwd" );
|
|
|
|
if (!passwd) {
|
2000-06-05 03:56:06 +08:00
|
|
|
log( L_AUTH "No passwords set and password authentication came" );
|
1999-11-25 22:54:08 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (strncmp( (char *) (&block->packetlen), passwd->password, 16)) {
|
2000-06-05 03:56:06 +08:00
|
|
|
log( L_AUTH "Passwd authentication failed!" );
|
2002-09-21 21:57:48 +08:00
|
|
|
DBG( "Expected %s, got %.16s\n", passwd->password, &block->packetlen );
|
1999-11-25 22:54:08 +08:00
|
|
|
return 1;
|
|
|
|
}
|
1999-05-11 17:53:45 +08:00
|
|
|
}
|
2004-06-27 04:11:14 +08:00
|
|
|
break;
|
1999-06-01 01:12:38 +08:00
|
|
|
case AT_MD5:
|
|
|
|
DBG( "md5 password" );
|
|
|
|
{
|
2004-06-27 04:11:14 +08:00
|
|
|
struct password_item *pass = NULL, *ptmp;
|
1999-06-01 03:16:22 +08:00
|
|
|
struct rip_md5_tail *tail;
|
2004-06-27 04:11:14 +08:00
|
|
|
struct MD5Context ctxt;
|
|
|
|
char md5sum_packet[16];
|
|
|
|
char md5sum_computed[16];
|
|
|
|
struct neighbor *neigh = neigh_find(p, &whotoldme, 0);
|
|
|
|
list *l = P_CF->passwords;
|
|
|
|
|
2003-04-07 02:38:01 +08:00
|
|
|
if (ntohs(block->packetlen) != PACKETLEN(num) - sizeof(struct rip_md5_tail) ) {
|
2000-06-05 03:56:06 +08:00
|
|
|
log( L_ERR "Packet length in MD5 does not match computed value" );
|
1999-08-20 17:59:39 +08:00
|
|
|
return 1;
|
|
|
|
}
|
1999-08-18 21:19:33 +08:00
|
|
|
|
2003-04-07 02:38:01 +08:00
|
|
|
tail = (struct rip_md5_tail *) ((char *) packet + (ntohs(block->packetlen) ));
|
|
|
|
if ((tail->mustbeFFFF != 0xffff) || (tail->mustbe0001 != 0x0100)) {
|
2000-06-05 03:56:06 +08:00
|
|
|
log( L_ERR "MD5 tail signature is not there" );
|
1999-11-25 22:54:08 +08:00
|
|
|
return 1;
|
|
|
|
}
|
1999-06-01 03:16:22 +08:00
|
|
|
|
2004-06-27 04:11:14 +08:00
|
|
|
WALK_LIST(ptmp, *l)
|
|
|
|
{
|
2004-09-09 00:04:02 +08:00
|
|
|
if (block->keyid != ptmp->id) continue;
|
2008-11-06 05:36:49 +08:00
|
|
|
if ((ptmp->genfrom > now_real) || (ptmp->gento < now_real)) continue;
|
2004-06-27 04:11:14 +08:00
|
|
|
pass = ptmp;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!pass) return 1;
|
|
|
|
|
|
|
|
if (!neigh) {
|
|
|
|
log( L_AUTH "Non-neighbour MD5 checksummed packet?" );
|
|
|
|
} else {
|
|
|
|
if (neigh->aux > block->seq) {
|
|
|
|
log( L_AUTH "MD5 protected packet with lower numbers" );
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
neigh->aux = block->seq;
|
1999-06-01 01:12:38 +08:00
|
|
|
}
|
2004-06-27 04:11:14 +08:00
|
|
|
|
|
|
|
memcpy(md5sum_packet, tail->md5, 16);
|
|
|
|
password_cpy(tail->md5, pass->password, 16);
|
|
|
|
|
|
|
|
MD5Init(&ctxt);
|
|
|
|
MD5Update(&ctxt, (char *) packet, ntohs(block->packetlen) + sizeof(struct rip_block_auth) );
|
|
|
|
MD5Final(md5sum_computed, &ctxt);
|
|
|
|
if (memcmp(md5sum_packet, md5sum_computed, 16))
|
|
|
|
return 1;
|
1999-06-01 01:12:38 +08:00
|
|
|
}
|
1999-05-11 17:53:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2000-06-06 01:13:36 +08:00
|
|
|
/*
|
2000-06-05 20:52:57 +08:00
|
|
|
* rip_outgoing_authentication - append authentication information to the packet.
|
|
|
|
* %num: number of rip_blocks already in packets. This function returns size of packet to send.
|
|
|
|
*/
|
1999-11-25 22:54:08 +08:00
|
|
|
int
|
1999-06-01 01:12:38 +08:00
|
|
|
rip_outgoing_authentication( struct proto *p, struct rip_block_auth *block, struct rip_packet *packet, int num )
|
1999-05-11 17:53:45 +08:00
|
|
|
{
|
2004-06-27 04:11:14 +08:00
|
|
|
struct password_item *passwd = password_find( P_CF->passwords);
|
1999-11-25 22:54:08 +08:00
|
|
|
|
|
|
|
if (!P_CF->authtype)
|
|
|
|
return PACKETLEN(num);
|
|
|
|
|
1999-05-11 17:53:45 +08:00
|
|
|
DBG( "Outgoing authentication: " );
|
|
|
|
|
1999-08-18 21:19:33 +08:00
|
|
|
if (!passwd) {
|
2000-06-05 03:56:06 +08:00
|
|
|
log( L_ERR "No suitable password found for authentication" );
|
1999-11-25 22:54:08 +08:00
|
|
|
return PACKETLEN(num);
|
1999-08-18 21:19:33 +08:00
|
|
|
}
|
|
|
|
|
2003-04-07 02:38:01 +08:00
|
|
|
block->authtype = htons(P_CF->authtype);
|
1999-08-18 21:19:33 +08:00
|
|
|
block->mustbeFFFF = 0xffff;
|
1999-05-11 17:53:45 +08:00
|
|
|
switch (P_CF->authtype) {
|
|
|
|
case AT_PLAINTEXT:
|
2004-06-27 04:11:14 +08:00
|
|
|
password_cpy( (char *) (&block->packetlen), passwd->password, 16);
|
1999-11-25 22:54:08 +08:00
|
|
|
return PACKETLEN(num);
|
1999-08-18 21:19:33 +08:00
|
|
|
case AT_MD5:
|
|
|
|
{
|
|
|
|
struct rip_md5_tail *tail;
|
|
|
|
struct MD5Context ctxt;
|
2004-06-01 02:11:16 +08:00
|
|
|
static u32 sequence = 0;
|
1999-08-18 21:19:33 +08:00
|
|
|
|
|
|
|
if (num > PACKET_MD5_MAX)
|
2000-06-05 03:56:06 +08:00
|
|
|
bug( "We can not add MD5 authentication to this long packet" );
|
1999-08-18 21:19:33 +08:00
|
|
|
|
2004-06-01 02:11:16 +08:00
|
|
|
/* need to preset the sequence number to a sane value */
|
2004-06-01 04:35:19 +08:00
|
|
|
if (!sequence)
|
2004-06-01 02:11:16 +08:00
|
|
|
sequence = (u32) time(NULL);
|
|
|
|
|
1999-08-18 21:19:33 +08:00
|
|
|
block->keyid = passwd->id;
|
2003-04-07 02:38:01 +08:00
|
|
|
block->authlen = sizeof(struct rip_block_auth);
|
1999-08-18 21:19:33 +08:00
|
|
|
block->seq = sequence++;
|
|
|
|
block->zero0 = 0;
|
1999-11-25 22:54:08 +08:00
|
|
|
block->zero1 = 0;
|
2003-04-07 02:38:01 +08:00
|
|
|
block->packetlen = htons(PACKETLEN(num));
|
|
|
|
tail = (struct rip_md5_tail *) ((char *) packet + PACKETLEN(num) );
|
1999-08-18 21:19:33 +08:00
|
|
|
tail->mustbeFFFF = 0xffff;
|
2003-04-07 02:38:01 +08:00
|
|
|
tail->mustbe0001 = 0x0100;
|
1999-08-18 21:19:33 +08:00
|
|
|
|
2004-06-27 04:11:14 +08:00
|
|
|
password_cpy(tail->md5, passwd->password, 16);
|
1999-08-18 21:19:33 +08:00
|
|
|
MD5Init(&ctxt);
|
2003-04-07 02:38:01 +08:00
|
|
|
MD5Update(&ctxt, (char *) packet, PACKETLEN(num) + sizeof(struct rip_md5_tail));
|
|
|
|
MD5Final(tail->md5, &ctxt);
|
1999-11-25 23:03:12 +08:00
|
|
|
return PACKETLEN(num) + block->authlen;
|
1999-06-01 01:12:38 +08:00
|
|
|
}
|
1999-11-25 22:54:08 +08:00
|
|
|
default:
|
2000-06-05 03:56:06 +08:00
|
|
|
bug( "Unknown authtype in outgoing authentication?" );
|
1999-05-11 17:53:45 +08:00
|
|
|
}
|
|
|
|
}
|