2014-07-21 05:22:06 +02:00
|
|
|
// 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.
|
2012-08-24 23:13:42 +02:00
|
|
|
//
|
2015-01-06 20:56:21 +01:00
|
|
|
// Copyright (C) 2012-2015 OpenVPN Technologies, Inc.
|
2012-08-24 23:13:42 +02:00
|
|
|
//
|
2014-07-21 05:22:06 +02:00
|
|
|
// 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.
|
2012-08-24 23:13:42 +02:00
|
|
|
//
|
2014-07-21 05:22:06 +02:00
|
|
|
// 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/>.
|
2012-08-24 23:13:42 +02:00
|
|
|
|
2012-11-23 07:18:43 +01:00
|
|
|
// Define Frame classes. These classes act as a factory for standard protocol
|
|
|
|
// buffers and also try to optimize the buffers for alignment.
|
|
|
|
|
2012-02-06 21:39:10 +01:00
|
|
|
#ifndef OPENVPN_FRAME_FRAME_H
|
|
|
|
#define OPENVPN_FRAME_FRAME_H
|
2011-10-25 19:32:26 +02:00
|
|
|
|
2015-05-17 21:27:34 +02:00
|
|
|
#include <openvpn/common/size.hpp>
|
2011-10-25 19:32:26 +02:00
|
|
|
#include <openvpn/common/exception.hpp>
|
|
|
|
#include <openvpn/common/rc.hpp>
|
|
|
|
#include <openvpn/buffer/buffer.hpp>
|
|
|
|
|
|
|
|
namespace openvpn {
|
|
|
|
|
|
|
|
class Frame : public RC<thread_unsafe_refcount>
|
|
|
|
{
|
|
|
|
public:
|
2011-12-11 09:28:55 +01:00
|
|
|
typedef boost::intrusive_ptr<Frame> Ptr;
|
|
|
|
|
2011-10-25 19:32:26 +02:00
|
|
|
// Frame context types -- we maintain a Context object for each context type
|
|
|
|
enum {
|
|
|
|
ENCRYPT_WORK=0,
|
|
|
|
DECRYPT_WORK,
|
2012-01-31 12:15:21 +01:00
|
|
|
COMPRESS_WORK,
|
|
|
|
DECOMPRESS_WORK,
|
2011-10-25 19:32:26 +02:00
|
|
|
READ_LINK_UDP,
|
2012-01-21 00:13:48 +01:00
|
|
|
READ_LINK_TCP,
|
2011-10-25 19:32:26 +02:00
|
|
|
READ_TUN,
|
|
|
|
READ_BIO_MEMQ_DGRAM,
|
|
|
|
READ_BIO_MEMQ_STREAM,
|
2011-11-30 20:47:30 +01:00
|
|
|
READ_SSL_CLEARTEXT,
|
2011-12-11 09:28:55 +01:00
|
|
|
WRITE_SSL_INIT,
|
|
|
|
WRITE_SSL_CLEARTEXT,
|
2011-11-30 20:47:30 +01:00
|
|
|
WRITE_ACK_STANDALONE,
|
2012-02-27 08:31:45 +01:00
|
|
|
WRITE_DC_MSG,
|
2014-08-11 02:44:04 +02:00
|
|
|
WRITE_HTTP,
|
2015-04-21 08:25:14 +02:00
|
|
|
READ_HTTP,
|
|
|
|
|
2011-10-25 19:32:26 +02:00
|
|
|
N_ALIGN_CONTEXTS
|
|
|
|
};
|
|
|
|
|
|
|
|
OPENVPN_SIMPLE_EXCEPTION(frame_context_index);
|
|
|
|
|
|
|
|
// We manage an array of Context objects, one for each
|
|
|
|
// Frame context above.
|
|
|
|
class Context
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Context()
|
|
|
|
{
|
|
|
|
headroom_ = 0;
|
|
|
|
payload_ = 0;
|
|
|
|
tailroom_ = 0;
|
|
|
|
buffer_flags_ = 0;
|
|
|
|
align_adjust_ = 0;
|
|
|
|
|
|
|
|
adj_headroom_ = 0;
|
|
|
|
adj_capacity_ = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Context(const size_t headroom,
|
|
|
|
const size_t payload,
|
|
|
|
const size_t tailroom,
|
2011-11-26 08:30:27 +01:00
|
|
|
const size_t align_adjust, // length of leading prefix data before the data that needs to be aligned on a size_t boundary
|
2011-11-21 06:45:29 +01:00
|
|
|
const size_t align_block, // size of alignment block, usually sizeof(size_t) but sometimes the cipher block size
|
2011-10-25 19:32:26 +02:00
|
|
|
const unsigned int buffer_flags) // flags passed to BufferAllocated constructor
|
|
|
|
{
|
|
|
|
headroom_ = headroom;
|
|
|
|
payload_ = payload;
|
|
|
|
tailroom_ = tailroom;
|
|
|
|
buffer_flags_ = buffer_flags;
|
|
|
|
align_adjust_ = align_adjust;
|
2011-11-21 06:45:29 +01:00
|
|
|
align_block_ = align_block;
|
2011-10-25 19:32:26 +02:00
|
|
|
recalc_derived();
|
|
|
|
}
|
|
|
|
|
2014-12-21 18:32:37 +01:00
|
|
|
void reset_align_adjust(const size_t align_adjust)
|
|
|
|
{
|
|
|
|
align_adjust_ = align_adjust;
|
|
|
|
}
|
|
|
|
|
2011-10-25 19:32:26 +02:00
|
|
|
size_t headroom() const { return adj_headroom_; }
|
|
|
|
size_t payload() const { return payload_; }
|
|
|
|
size_t tailroom() const { return tailroom_; }
|
|
|
|
size_t capacity() const { return adj_capacity_; }
|
2013-11-11 20:33:35 +01:00
|
|
|
unsigned int buffer_flags() const { return buffer_flags_; }
|
2011-10-25 19:32:26 +02:00
|
|
|
|
|
|
|
// Calculate a starting offset into a buffer object, dealing with
|
|
|
|
// headroom and alignment issues.
|
2012-01-31 12:15:21 +01:00
|
|
|
size_t prepare(Buffer& buf) const
|
2011-10-25 19:32:26 +02:00
|
|
|
{
|
2011-12-11 09:28:55 +01:00
|
|
|
buf.reset(capacity(), buffer_flags());
|
2011-11-26 08:30:27 +01:00
|
|
|
buf.init_headroom(actual_headroom(buf.c_data_raw()));
|
2012-01-31 12:15:21 +01:00
|
|
|
return payload();
|
2011-10-25 19:32:26 +02:00
|
|
|
}
|
|
|
|
|
2012-01-21 00:13:48 +01:00
|
|
|
// Realign a buffer to headroom
|
|
|
|
void realign(Buffer& buf) const
|
|
|
|
{
|
|
|
|
buf.realign(actual_headroom(buf.c_data_raw()));
|
|
|
|
}
|
|
|
|
|
2014-08-11 02:49:10 +02:00
|
|
|
// Return a new BufferAllocated object initialized with the given data.
|
|
|
|
BufferPtr copy(const unsigned char *data, const size_t size) const
|
2011-10-25 19:32:26 +02:00
|
|
|
{
|
|
|
|
const size_t cap = size + headroom() + tailroom();
|
|
|
|
BufferPtr b = new BufferAllocated(cap, buffer_flags());
|
2011-11-26 08:30:27 +01:00
|
|
|
b->init_headroom(actual_headroom(b->c_data_raw()));
|
2011-10-25 19:32:26 +02:00
|
|
|
b->write(data, size);
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
2014-08-11 02:51:58 +02:00
|
|
|
// Return a new BufferAllocated object initialized with
|
|
|
|
// the data in given buffer. buf may be empty or undefined.
|
|
|
|
BufferPtr copy(const BufferPtr& buf) const
|
|
|
|
{
|
|
|
|
const size_t size = buf ? buf->size() : 0;
|
|
|
|
const size_t cap = size + headroom() + tailroom();
|
|
|
|
BufferPtr b = new BufferAllocated(cap, buffer_flags());
|
|
|
|
b->init_headroom(actual_headroom(b->c_data_raw()));
|
|
|
|
if (size)
|
|
|
|
b->write(buf->c_data(), size);
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
2011-10-25 19:32:26 +02:00
|
|
|
// How much payload space left in buffer
|
|
|
|
size_t remaining_payload(const Buffer& buf) const
|
|
|
|
{
|
|
|
|
if (payload() > buf.size())
|
|
|
|
return payload() - buf.size();
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-11-26 08:30:27 +01:00
|
|
|
// Used to set the capacity of a group of Context objects
|
|
|
|
// to the highest capacity of any one of the members.
|
|
|
|
void standardize_capacity(const size_t newcap)
|
|
|
|
{
|
|
|
|
if (newcap > adj_capacity_)
|
|
|
|
adj_capacity_ = newcap;
|
|
|
|
}
|
|
|
|
|
2012-01-21 00:13:48 +01:00
|
|
|
// return a boost::asio::mutable_buffers_1 object used by
|
|
|
|
// asio read methods.
|
|
|
|
boost::asio::mutable_buffers_1 mutable_buffers_1(Buffer& buf) const
|
|
|
|
{
|
|
|
|
return boost::asio::mutable_buffers_1(buf.data(), remaining_payload(buf));
|
|
|
|
}
|
|
|
|
|
2011-10-25 19:32:26 +02:00
|
|
|
std::string info() const
|
|
|
|
{
|
|
|
|
std::ostringstream info;
|
|
|
|
info << "head=" << headroom_ << "[" << adj_headroom_ << "] "
|
2011-11-21 06:45:29 +01:00
|
|
|
<< "pay=" << payload_ << ' '
|
|
|
|
<< "tail=" << tailroom_ << ' '
|
|
|
|
<< "cap=" << adj_capacity_ << ' '
|
|
|
|
<< "bf=" << buffer_flags_ << ' '
|
|
|
|
<< "align_adj=" << align_adjust_ << ' '
|
|
|
|
<< "align_block=" << align_block_;
|
2011-10-25 19:32:26 +02:00
|
|
|
return info.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// recalculate derived values when object parameters are modified
|
|
|
|
void recalc_derived()
|
|
|
|
{
|
2011-11-26 08:30:27 +01:00
|
|
|
// calculate adjusted headroom due to worst-case alignment loss
|
|
|
|
adj_headroom_ = headroom_ + align_block_;
|
2011-10-25 19:32:26 +02:00
|
|
|
|
|
|
|
// calculate capacity
|
|
|
|
adj_capacity_ = adj_headroom_ + payload_ + tailroom_;
|
|
|
|
}
|
|
|
|
|
2011-11-26 08:30:27 +01:00
|
|
|
// add a small delta ( < align_block) to headroom so that the point
|
|
|
|
// after the first align_adjust bytes of the buffer starting at base
|
|
|
|
// will be aligned on an align_block boundary
|
|
|
|
size_t actual_headroom(const void *base) const
|
2011-10-25 19:32:26 +02:00
|
|
|
{
|
2011-11-26 08:30:27 +01:00
|
|
|
return headroom_ + (-(size_t(base) + headroom_ + align_adjust_) & (align_block_ - 1));
|
2011-10-25 19:32:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// parameters
|
|
|
|
size_t headroom_;
|
|
|
|
size_t payload_;
|
|
|
|
size_t tailroom_;
|
2011-11-26 08:30:27 +01:00
|
|
|
size_t align_adjust_;
|
2011-11-21 06:45:29 +01:00
|
|
|
size_t align_block_;
|
2011-11-26 08:30:27 +01:00
|
|
|
unsigned int buffer_flags_;
|
2011-10-25 19:32:26 +02:00
|
|
|
|
|
|
|
// derived
|
|
|
|
size_t adj_headroom_;
|
|
|
|
size_t adj_capacity_;
|
|
|
|
};
|
|
|
|
|
|
|
|
Frame() {}
|
|
|
|
|
|
|
|
explicit Frame(const Context& c) { set_default_context(c); }
|
|
|
|
|
|
|
|
// set the default context
|
|
|
|
void set_default_context(const Context& c)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < N_ALIGN_CONTEXTS; ++i)
|
|
|
|
contexts[i] = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate a starting offset into a buffer object, dealing with
|
|
|
|
// headroom and alignment issues. context should be one of
|
2012-01-31 12:15:21 +01:00
|
|
|
// the context types above. Returns payload size of buffer.
|
2015-04-16 06:14:59 +02:00
|
|
|
size_t prepare(const unsigned int context, Buffer& buf) const
|
2011-10-25 19:32:26 +02:00
|
|
|
{
|
2012-01-31 12:15:21 +01:00
|
|
|
return (*this)[context].prepare(buf);
|
2011-10-25 19:32:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t n_contexts() const { return N_ALIGN_CONTEXTS; }
|
|
|
|
|
|
|
|
Context& operator[](const size_t i)
|
|
|
|
{
|
|
|
|
if (i >= N_ALIGN_CONTEXTS)
|
|
|
|
throw frame_context_index();
|
|
|
|
return contexts[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
const Context& operator[](const size_t i) const
|
|
|
|
{
|
|
|
|
if (i >= N_ALIGN_CONTEXTS)
|
|
|
|
throw frame_context_index();
|
|
|
|
return contexts[i];
|
|
|
|
}
|
|
|
|
|
2011-11-26 08:30:27 +01:00
|
|
|
void standardize_capacity(const unsigned int context_mask)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
size_t max_cap = 0;
|
|
|
|
unsigned int mask = context_mask;
|
|
|
|
|
|
|
|
// find the largest capacity in the group
|
|
|
|
for (i = 0; i < N_ALIGN_CONTEXTS; ++i)
|
|
|
|
{
|
|
|
|
if (mask & 1)
|
|
|
|
{
|
|
|
|
const size_t cap = contexts[i].capacity();
|
|
|
|
if (cap > max_cap)
|
|
|
|
max_cap = cap;
|
|
|
|
}
|
|
|
|
mask >>= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// set all members of group to largest capacity found
|
|
|
|
mask = context_mask;
|
|
|
|
for (i = 0; i < N_ALIGN_CONTEXTS; ++i)
|
|
|
|
{
|
|
|
|
if (mask & 1)
|
|
|
|
contexts[i].standardize_capacity(max_cap);
|
|
|
|
mask >>= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-25 19:32:26 +02:00
|
|
|
private:
|
|
|
|
Context contexts[N_ALIGN_CONTEXTS];
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace openvpn
|
|
|
|
|
2012-02-06 21:39:10 +01:00
|
|
|
#endif // OPENVPN_FRAME_FRAME_H
|