1
0
mirror of https://github.com/mfontanini/libtins synced 2026-01-28 20:44:26 +01:00

Added support for IPSec.

This commit is contained in:
Matias Fontanini
2013-11-04 23:05:00 -03:00
parent de06fee5ab
commit 5345b29f8c
11 changed files with 679 additions and 57 deletions

View File

@@ -36,6 +36,7 @@
#include "ipv6.h"
#include "tcp.h"
#include "udp.h"
#include "ipsec.h"
#include "icmp.h"
#include "icmpv6.h"
#include "arp.h"
@@ -120,6 +121,10 @@ Tins::PDU *pdu_from_flag(Constants::IP::e flag, const uint8_t *buffer,
return new Tins::ICMPv6(buffer, size);
case Constants::IP::PROTO_IPV6:
return new Tins::IPv6(buffer, size);
case Constants::IP::PROTO_AH:
return new Tins::IPSecAH(buffer, size);
case Constants::IP::PROTO_ESP:
return new Tins::IPSecESP(buffer, size);
default:
break;
}
@@ -194,6 +199,27 @@ Constants::Ethernet::e pdu_flag_to_ether_type(PDU::PDUType flag) {
}
}
Constants::IP::e pdu_flag_to_ip_type(PDU::PDUType flag) {
switch(flag) {
case PDU::IP:
return Constants::IP::PROTO_IPIP;
case PDU::TCP:
return Constants::IP::PROTO_TCP;
case PDU::UDP:
return Constants::IP::PROTO_UDP;
case PDU::ICMP:
return Constants::IP::PROTO_ICMP;
case PDU::ICMPv6:
return Constants::IP::PROTO_ICMPV6;
case PDU::IPSEC_AH:
return Constants::IP::PROTO_AH;
case PDU::IPSEC_ESP:
return Constants::IP::PROTO_ESP;
default:
return static_cast<Constants::IP::e>(0xff);
};
}
bool increment(IPv4Address &addr) {
uint32_t addr_int = Endian::be_to_host<uint32_t>(addr);
bool reached_end = ++addr_int == 0xffffffff;

View File

@@ -385,29 +385,12 @@ void IP::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU* pare
#endif
checksum(0);
if(inner_pdu()) {
uint32_t new_flag;
switch(inner_pdu()->pdu_type()) {
case PDU::IP:
new_flag = Constants::IP::PROTO_IPIP;
break;
case PDU::TCP:
new_flag = Constants::IP::PROTO_TCP;
break;
case PDU::UDP:
new_flag = Constants::IP::PROTO_UDP;
break;
case PDU::ICMP:
new_flag = Constants::IP::PROTO_ICMP;
break;
default:
if(Internals::pdu_type_registered<IP>(inner_pdu()->pdu_type()))
new_flag = static_cast<Constants::IP::e>(
Internals::pdu_type_to_id<IP>(inner_pdu()->pdu_type())
);
else
// check for other protos
new_flag = 0xff;
};
uint32_t new_flag = Internals::pdu_flag_to_ip_type(inner_pdu()->pdu_type());
if(new_flag == 0xff && Internals::pdu_type_registered<IP>(inner_pdu()->pdu_type())) {
new_flag = static_cast<Constants::IP::e>(
Internals::pdu_type_to_id<IP>(inner_pdu()->pdu_type())
);
}
protocol(new_flag);
//flag(new_flag);
}

135
src/ipsec.cpp Normal file
View File

@@ -0,0 +1,135 @@
/*
* Copyright (c) 2012, Matias Fontanini
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <cstring>
#include "ipsec.h"
#include "internals.h"
#include "rawpdu.h"
namespace Tins {
IPSecAH::IPSecAH()
: _header(), _icv(4) {
length(2);
}
IPSecAH::IPSecAH(const uint8_t *buffer, uint32_t total_sz) {
// At least size for the header + 32bits of ICV
if(total_sz < sizeof(_header) + sizeof(uint32_t))
throw malformed_packet();
std::memcpy(&_header, buffer, sizeof(_header));
const uint32_t ah_len = 4 * (static_cast<uint16_t>(length()) + 2);
if(ah_len > total_sz)
throw malformed_packet();
_icv.assign(buffer + sizeof(_header), buffer + ah_len);
buffer += ah_len;
total_sz -= ah_len;
if(total_sz) {
inner_pdu(
Internals::pdu_from_flag(
static_cast<Constants::IP::e>(next_header()),
buffer,
total_sz,
true
)
);
}
}
void IPSecAH::next_header(uint8_t new_next_header) {
_header.next_header = new_next_header;
}
void IPSecAH::length(uint8_t new_length) {
_header.length = new_length;
}
void IPSecAH::spi(uint32_t new_spi) {
_header.spi = Endian::host_to_be(new_spi);
}
void IPSecAH::seq_number(uint32_t new_seq_number) {
_header.seq_number = Endian::host_to_be(new_seq_number);
}
void IPSecAH::icv(const byte_array &new_icv) {
_icv = new_icv;
}
uint32_t IPSecAH::header_size() const {
return sizeof(_header) + _icv.size();
}
void IPSecAH::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *) {
if(inner_pdu())
next_header(Internals::pdu_flag_to_ip_type(inner_pdu()->pdu_type()));
std::memcpy(buffer, &_header, sizeof(_header));
std::copy(
_icv.begin(),
_icv.end(),
buffer + sizeof(_header)
);
}
// IPSecESP
IPSecESP::IPSecESP()
: _header()
{
}
IPSecESP::IPSecESP(const uint8_t *buffer, uint32_t total_sz) {
if(total_sz < sizeof(_header))
throw malformed_packet();
std::memcpy(&_header, buffer, sizeof(_header));
buffer += sizeof(_header);
total_sz -= sizeof(_header);
if(total_sz) {
inner_pdu(new RawPDU(buffer, total_sz));
}
}
void IPSecESP::spi(uint32_t new_spi) {
_header.spi = Endian::host_to_be(new_spi);
}
void IPSecESP::seq_number(uint32_t new_seq_number) {
_header.seq_number = Endian::host_to_be(new_seq_number);
}
uint32_t IPSecESP::header_size() const {
return sizeof(_header);
}
void IPSecESP::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *) {
std::memcpy(buffer, &_header, sizeof(_header));
}
}

View File

@@ -200,30 +200,12 @@ void IPv6::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *pa
assert(total_sz >= header_size());
#endif
if(inner_pdu()) {
uint8_t new_flag = 0xff;
switch(inner_pdu()->pdu_type()) {
case PDU::IP:
new_flag = Constants::IP::PROTO_IPIP;
break;
case PDU::TCP:
new_flag = Constants::IP::PROTO_TCP;
break;
case PDU::UDP:
new_flag = Constants::IP::PROTO_UDP;
break;
case PDU::ICMP:
new_flag = Constants::IP::PROTO_ICMP;
break;
case PDU::ICMPv6:
new_flag = Constants::IP::PROTO_ICMPV6;
break;
default:
if(Internals::pdu_type_registered<IPv6>(inner_pdu()->pdu_type()))
new_flag = static_cast<Constants::IP::e>(
Internals::pdu_type_to_id<IPv6>(inner_pdu()->pdu_type())
);
break;
};
uint8_t new_flag = Internals::pdu_flag_to_ip_type(inner_pdu()->pdu_type());
if(new_flag == 0xff && Internals::pdu_type_registered<IPv6>(inner_pdu()->pdu_type())) {
new_flag = static_cast<Constants::IP::e>(
Internals::pdu_type_to_id<IPv6>(inner_pdu()->pdu_type())
);
}
set_last_next_header(new_flag);
}
payload_length(total_sz - sizeof(_header));