0
0
mirror of https://github.com/OpenVPN/openvpn3.git synced 2024-09-20 12:12:15 +02:00

IP packet headers: Added IPv6 and ICMPv6

Signed-off-by: James Yonan <james@openvpn.net>
This commit is contained in:
James Yonan 2018-04-05 17:41:22 -06:00 committed by Lev Stipakov
parent 6224ade511
commit f051a10f34
9 changed files with 183 additions and 58 deletions

View File

@ -23,7 +23,7 @@
#define OPENVPN_IP_DHCP_H
#include <openvpn/ip/eth.hpp>
#include <openvpn/ip/ip.hpp>
#include <openvpn/ip/ip4.hpp>
#include <openvpn/ip/udp.hpp>
#pragma pack(push)
@ -78,7 +78,7 @@ namespace openvpn {
struct DHCPPacket {
EthHeader eth;
IPHeader ip;
IPv4Header ip;
UDPHeader udp;
DHCP dhcp;
std::uint8_t options[];

View File

@ -19,40 +19,42 @@
// along with this program in the COPYING file.
// If not, see <http://www.gnu.org/licenses/>.
// Define the ICMP header
// Define the ICMPv4 header
#ifndef OPENVPN_IP_ICMP_H
#define OPENVPN_IP_ICMP_H
#pragma once
#include <cstdint> // for std::uint32_t, uint16_t, uint8_t
#include <openvpn/ip/ip.hpp>
#include <openvpn/ip/ip4.hpp>
#pragma pack(push)
#pragma pack(1)
namespace openvpn {
struct ICMP {
struct ICMPv4 {
enum {
ECHO_REPLY = 0,
ECHO_REQUEST = 8,
ECHO_REPLY = 0,
};
struct IPHeader head;
struct IPv4Header head;
std::uint8_t type;
std::uint8_t code;
union {
struct {
std::uint8_t type;
std::uint8_t code;
};
std::uint16_t type_code;
};
std::uint16_t checksum;
union {
struct {
std::uint16_t id;
std::uint16_t seq_num;
} echo;
} hd;
};
};
};
}
#pragma pack(pop)
#endif

61
openvpn/ip/icmp6.hpp Normal file
View File

@ -0,0 +1,61 @@
// OpenVPN -- An application to securely tunnel IP networks
// over a single port, with support for SSL/TLS-based
// session authentication and key exchange,
// packet encryption, packet authentication, and
// packet compression.
//
// Copyright (C) 2012-2017 OpenVPN Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License Version 3
// as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program in the COPYING file.
// If not, see <http://www.gnu.org/licenses/>.
// Define the ICMPv6 header
#pragma once
#include <cstdint> // for std::uint32_t, uint16_t, uint8_t
#include <openvpn/ip/ip6.hpp>
#pragma pack(push)
#pragma pack(1)
namespace openvpn {
struct ICMPv6 {
enum {
ECHO_REQUEST = 128,
ECHO_REPLY = 129,
};
struct IPv6Header head;
union {
struct {
std::uint8_t type;
std::uint8_t code;
};
std::uint16_t type_code;
};
std::uint16_t checksum;
union {
struct {
std::uint16_t id;
std::uint16_t seq_num;
};
};
};
}
#pragma pack(pop)

View File

@ -19,10 +19,9 @@
// along with this program in the COPYING file.
// If not, see <http://www.gnu.org/licenses/>.
// Define the IP protocol header
// IPv4 header
#ifndef OPENVPN_IP_IP_H
#define OPENVPN_IP_IP_H
#pragma once
#include <cstdint> // for std::uint32_t, uint16_t, uint8_t
@ -30,13 +29,9 @@
#pragma pack(1)
namespace openvpn {
struct IPHeader
{
static unsigned int version(const std::uint8_t version_len)
{
return (version_len >> 4) & 0x0F;
}
struct IPv4Header
{
static unsigned int length(const std::uint8_t version_len)
{
return (version_len & 0x0F) << 2;
@ -61,12 +56,6 @@ namespace openvpn {
std::uint8_t ttl;
enum {
ICMP = 1, /* ICMP protocol */
IGMP = 2, /* IGMP protocol */
TCP = 6, /* TCP protocol */
UDP = 17, /* UDP protocol */
};
std::uint8_t protocol;
std::uint16_t check;
@ -74,28 +63,6 @@ namespace openvpn {
std::uint32_t daddr;
/* The options start here. */
};
inline std::uint16_t ip_checksum(const void *ip, unsigned int size)
{
std::uint16_t *buffer = (std::uint16_t *)ip;
std::uint32_t cksum = 0;
while (size > 1)
{
cksum += *buffer++;
size -= sizeof(uint16_t);
}
if (size)
cksum += *(uint8_t*)buffer;
cksum = (cksum >> 16) + (cksum & 0xffff);
cksum += (cksum >> 16);
return ~cksum;
}
}
#pragma pack(pop)
#endif

50
openvpn/ip/ip6.hpp Normal file
View File

@ -0,0 +1,50 @@
// OpenVPN -- An application to securely tunnel IP networks
// over a single port, with support for SSL/TLS-based
// session authentication and key exchange,
// packet encryption, packet authentication, and
// packet compression.
//
// Copyright (C) 2012-2017 OpenVPN Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License Version 3
// as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program in the COPYING file.
// If not, see <http://www.gnu.org/licenses/>.
// IPv6 header
#pragma once
#include <cstdint> // for std::uint32_t, uint16_t, uint8_t
#include <openvpn/common/socktypes.hpp>
#pragma pack(push)
#pragma pack(1)
namespace openvpn {
struct IPv6Header
{
std::uint8_t version_prio;
std::uint8_t flow_lbl[3];
std::uint16_t payload_len;
std::uint8_t nexthdr;
std::uint8_t hop_limit;
struct in6_addr saddr;
struct in6_addr daddr;
};
}
#pragma pack(pop)

45
openvpn/ip/ipcommon.hpp Normal file
View File

@ -0,0 +1,45 @@
// OpenVPN -- An application to securely tunnel IP networks
// over a single port, with support for SSL/TLS-based
// session authentication and key exchange,
// packet encryption, packet authentication, and
// packet compression.
//
// Copyright (C) 2012-2017 OpenVPN Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License Version 3
// as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program in the COPYING file.
// If not, see <http://www.gnu.org/licenses/>.
// Common declarations for IPv4 and IPv6
#pragma once
#include <cstdint> // for std::uint32_t, uint16_t, uint8_t
namespace openvpn {
namespace IPCommon {
enum {
ICMPv4 = 1, /* ICMPv4 protocol */
ICMPv6 = 58, /* ICMPv6 protocol */
IGMP = 2, /* IGMP protocol */
TCP = 6, /* TCP protocol */
UDP = 17, /* UDP protocol */
};
inline unsigned int version(const std::uint8_t version_len_prio)
{
return (version_len_prio >> 4) & 0x0F;
}
}
}

View File

@ -24,7 +24,7 @@
#ifndef OPENVPN_IP_UDP_H
#define OPENVPN_IP_UDP_H
#include <openvpn/ip/ip.hpp>
#include <openvpn/ip/ipcommon.hpp>
namespace openvpn {
@ -68,7 +68,7 @@ namespace openvpn {
}
/* the protocol number and the length of the UDP packet */
sum += (std::uint16_t)IPHeader::UDP + (std::uint16_t)len_udp;
sum += (std::uint16_t)IPCommon::UDP + (std::uint16_t)len_udp;
/* keep only the last 16 bits of the 32 bit calculated sum and add the carries */
while (sum >> 16)

View File

@ -26,7 +26,7 @@
#include <openvpn/common/socktypes.hpp>
#include <openvpn/buffer/buffer.hpp>
#include <openvpn/addr/ipv4.hpp>
#include <openvpn/ip/ipcommon.hpp>
#include <openvpn/ip/dhcp.hpp>
#include <openvpn/tun/builder/capture.hpp>
@ -54,7 +54,7 @@ namespace openvpn {
return false;
DHCPPacket* dhcp = (DHCPPacket*)buf.data();
if (dhcp->ip.protocol == IPHeader::UDP
if (dhcp->ip.protocol == IPCommon::UDP
&& dhcp->udp.source == htons(DHCP::BOOTPS_PORT)
&& dhcp->udp.dest == htons(DHCP::BOOTPC_PORT)
&& dhcp->dhcp.op == DHCP::BOOTREPLY)

View File

@ -29,7 +29,7 @@
#include <openvpn/common/size.hpp>
#include <openvpn/common/rc.hpp>
#include <openvpn/frame/frame.hpp>
#include <openvpn/ip/ip.hpp>
#include <openvpn/ip/ipcommon.hpp>
#include <openvpn/common/socktypes.hpp>
#include <openvpn/log/sessionstats.hpp>
#include <openvpn/tun/tunlog.hpp>
@ -73,7 +73,7 @@ namespace openvpn {
{
if (buf.offset() >= 4 && buf.size() >= 1)
{
switch (IPHeader::version(buf[0]))
switch (IPCommon::version(buf[0]))
{
case 4:
prepend_pf_inet(buf, PF_INET);