Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
257 changes: 215 additions & 42 deletions src/openvpn/forward.c

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions src/openvpn/forward.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
* file
*/

#define BULK_MODE(c) (c->c2.frame.bulk_size > 0)
#define BULK_DATA(b) (b && (b->bulk_leng > 0) && (b->bulk_indx < b->bulk_leng))
#define INST_LENG(a) (a && (a->inst_leng > 0) && (a->inst_indx < a->inst_leng) && (a->pending == NULL))

#define TUN_OUT(c) (BLEN(&(c)->c2.to_tun) > 0)
#define LINK_OUT(c) (BLEN(&(c)->c2.to_link) > 0)
#define ANY_OUT(c) (TUN_OUT(c) || LINK_OUT(c))
Expand Down Expand Up @@ -196,6 +200,8 @@ bool process_incoming_link_part1(struct context *c, struct link_socket_info *lsi
void process_incoming_link_part2(struct context *c, struct link_socket_info *lsi,
const uint8_t *orig_buf);

void process_incoming_link_part3(struct context *c);

/**
* Transfers \c float_sa data extracted from an incoming DCO
* PEER_FLOAT_NTF to \c out_osaddr for later processing.
Expand Down
62 changes: 62 additions & 0 deletions src/openvpn/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -2976,6 +2976,11 @@ frame_finalize_options(struct context *c, const struct options *o)
tailroom += COMP_EXTRA_BUFFER(payload_size);
#endif

if (frame->bulk_size > 0)
{
payload_size = BAT_SIZE(TUN_BAT_ONE, frame->tun_mtu, TUN_BAT_OFF);
}

frame->buf.payload_size = payload_size;
frame->buf.headroom = headroom;
frame->buf.tailroom = tailroom;
Expand Down Expand Up @@ -3478,6 +3483,10 @@ do_init_frame_tls(struct context *c)
if (c->c2.tls_multi)
{
tls_multi_init_finalize(c->c2.tls_multi, c->options.ce.tls_mtu);
if (c->c2.frame.bulk_size > 0)
{
c->c2.tls_multi->opt.frame.buf.payload_size = c->c2.frame.tun_mtu;
}
ASSERT(c->c2.tls_multi->opt.frame.buf.payload_size <= c->c2.frame.buf.payload_size);
frame_print(&c->c2.tls_multi->opt.frame, D_MTU_INFO, "Control Channel MTU parms");

Expand Down Expand Up @@ -3545,6 +3554,14 @@ do_init_frame(struct context *c)
c->c2.frame.extra_tun += c->options.ce.tun_mtu_extra;
}

/*
* Adjust bulk size based on the --bulk-mode parameter.
*/
if (c->options.ce.bulk_mode)
{
c->c2.frame.bulk_size = c->options.ce.tun_mtu;
}

/*
* Fill in the blanks in the frame parameters structure,
* make sure values are rational, etc.
Expand Down Expand Up @@ -3685,9 +3702,43 @@ init_context_buffers(const struct frame *frame)

size_t buf_size = BUF_SIZE(frame);

if (frame->bulk_size > 0)
{
size_t off_size = (frame->buf.headroom + TUN_BAT_OFF + frame->buf.tailroom);
buf_size = BAT_SIZE(TUN_BAT_MAX, frame->tun_mtu, off_size);
}

dmsg(M_INFO, "MEM NEW [%ld] [%d+%d+%d]", buf_size, frame->buf.headroom, frame->buf.payload_size, frame->buf.tailroom);

b->read_link_buf = alloc_buf(buf_size);
b->read_tun_buf = alloc_buf(buf_size);

if (frame->bulk_size > 0)
{
for (int x = 0; x < TUN_BAT_MAX; ++x)
{
size_t part_size = BUF_SIZE(frame);
b->read_tun_bufs[x] = alloc_buf(part_size);
b->read_tun_bufs[x].offset = TUN_BAT_OFF;
b->read_tun_bufs[x].len = 0;
}

b->read_tun_max = alloc_buf(buf_size);
b->read_tun_max.offset = TUN_BAT_OFF;
b->read_tun_max.len = 0;

b->send_tun_max = alloc_buf(buf_size);
b->send_tun_max.offset = TUN_BAT_OFF;
b->send_tun_max.len = 0;

b->to_tun_max = alloc_buf(buf_size);
b->to_tun_max.offset = TUN_BAT_OFF;
b->to_tun_max.len = 0;
}

b->bulk_indx = -1;
b->bulk_leng = -1;

b->aux_buf = alloc_buf(buf_size);

b->encrypt_buf = alloc_buf(buf_size);
Expand All @@ -3710,6 +3761,17 @@ free_context_buffers(struct context_buffers *b)
free_buf(&b->read_tun_buf);
free_buf(&b->aux_buf);

if (b->to_tun_max.data)
{
free_buf(&b->to_tun_max);
free_buf(&b->send_tun_max);
free_buf(&b->read_tun_max);
for (int x = 0; x < TUN_BAT_MAX; ++x)
{
free_buf(&b->read_tun_bufs[x]);
}
}

#ifdef USE_COMP
free_buf(&b->compress_buf);
free_buf(&b->decompress_buf);
Expand Down
10 changes: 8 additions & 2 deletions src/openvpn/mtu.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,15 @@ void
alloc_buf_sock_tun(struct buffer *buf, const struct frame *frame)
{
/* allocate buffer for overlapped I/O */
*buf = alloc_buf(BUF_SIZE(frame));
int alen = BUF_SIZE(frame);
int blen = frame->buf.payload_size;
if (frame->bulk_size > 0) {
alen = BAT_SIZE(TUN_BAT_MAX, frame->tun_mtu, TUN_BAT_OFF);
blen = BAT_SIZE(TUN_BAT_MAX, frame->tun_mtu, TUN_BAT_NOP);
}
*buf = alloc_buf(alen);
ASSERT(buf_init(buf, frame->buf.headroom));
buf->len = frame->buf.payload_size;
buf->len = blen;
ASSERT(buf_safe(buf, 0));
}

Expand Down
15 changes: 15 additions & 0 deletions src/openvpn/mtu.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@
*/
#define TUN_MTU_MIN 100

/*
* Bulk mode static define values.
*/
#define TUN_BAT_MIN 6
#define TUN_BAT_MAX 9
#define TUN_BAT_OFF 250
#define TUN_BAT_NOP 0
#define TUN_BAT_ONE 1

/*
* Default MTU of network over which tunnel data will pass by TCP/UDP.
*/
Expand Down Expand Up @@ -157,6 +166,11 @@ struct frame
* which defaults to 0 for tun and 32
* (\c TAP_MTU_EXTRA_DEFAULT) for tap.
* */

int bulk_size; /**< Set in the init frame function to
* signal to related functions to
* process bulk mode data transfers.
* */
};

/* Forward declarations, to prevent includes */
Expand All @@ -176,6 +190,7 @@ struct options;
* larger than the headroom.
*/
#define BUF_SIZE(f) ((f)->buf.headroom + (f)->buf.payload_size + (f)->buf.tailroom)
#define BAT_SIZE(a, b, c) ((a * b) + c)

/*
* Function prototypes.
Expand Down
142 changes: 135 additions & 7 deletions src/openvpn/multi.c
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,10 @@ multi_init(struct context *t)
}

m->deferred_shutdown_signal.signal_received = 0;

m->inst_indx = -1;
m->inst_leng = -1;
m->inst_list = calloc(TUN_BAT_MAX, sizeof(struct multi_instance *));
}

const char *
Expand Down Expand Up @@ -721,6 +725,10 @@ multi_uninit(struct multi_context *m)
multi_reap_free(m->reaper);
mroute_helper_free(m->route_helper);
multi_io_free(m->multi_io);

m->inst_indx = -1;
m->inst_leng = -1;
free(m->inst_list);
}
}

Expand Down Expand Up @@ -3144,7 +3152,7 @@ multi_process_float(struct multi_context *m, struct multi_instance *mi, struct l
msg(D_MULTI_LOW, "Disallow float to an address taken by another client %s",
multi_instance_string(ex_mi, false, &gc));

mi->context.c2.buf.len = 0;
mi->context.c2.buf2.len = 0;

goto done;
}
Expand Down Expand Up @@ -3368,7 +3376,7 @@ multi_process_incoming_link(struct multi_context *m, struct multi_instance *inst
if (!instance)
{
#ifdef MULTI_DEBUG_EVENT_LOOP
printf("TCP/UDP -> TUN [%d]\n", BLEN(&m->top.c2.buf));
printf("TCP/UDP -> TUN [%d]\n", BLEN(&m->top.c2.buf2));
#endif
multi_set_pending(m, multi_get_create_instance_udp(m, &floated, sock));
}
Expand All @@ -3387,7 +3395,7 @@ multi_process_incoming_link(struct multi_context *m, struct multi_instance *inst
if (!instance)
{
/* transfer packet pointer from top-level context buffer to instance */
c->c2.buf = m->top.c2.buf;
c->c2.buf2 = m->top.c2.buf2;

/* transfer from-addr from top-level context buffer to instance */
if (!floated)
Expand All @@ -3396,7 +3404,7 @@ multi_process_incoming_link(struct multi_context *m, struct multi_instance *inst
}
}

if (BLEN(&c->c2.buf) > 0)
if (BLEN(&c->c2.buf2) > 0)
{
struct link_socket_info *lsi;
const uint8_t *orig_buf;
Expand All @@ -3405,16 +3413,17 @@ multi_process_incoming_link(struct multi_context *m, struct multi_instance *inst

perf_push(PERF_PROC_IN_LINK);
lsi = &sock->info;
orig_buf = c->c2.buf.data;
orig_buf = c->c2.buf2.data;
if (process_incoming_link_part1(c, lsi, floated))
{
/* nonzero length means that we have a valid, decrypted packed */
if (floated && c->c2.buf.len > 0)
if (floated && c->c2.buf2.len > 0)
{
multi_process_float(m, m->pending, sock);
}

process_incoming_link_part2(c, lsi, orig_buf);
process_incoming_link_part3(c);
}
perf_pop();

Expand Down Expand Up @@ -3543,12 +3552,19 @@ multi_process_incoming_link(struct multi_context *m, struct multi_instance *inst
return ret;
}

int min_max(int a, int b, int c)
{
if (a > c) { return c; }
if (a < b) { return b; }
return a;
}

/*
* Process packets in the TUN/TAP interface -> TCP/UDP socket direction,
* i.e. server -> client direction.
*/
bool
multi_process_incoming_tun(struct multi_context *m, const unsigned int mpp_flags)
multi_process_incoming_tun_part2(struct multi_context *m, const unsigned int mpp_flags)
{
bool ret = true;

Expand Down Expand Up @@ -3594,6 +3610,27 @@ multi_process_incoming_tun(struct multi_context *m, const unsigned int mpp_flags
/* for now, treat multicast as broadcast */
multi_bcast(m, &m->top.c2.buf, NULL, vid);
}
else if (m->inst_indx == -9)
{
struct multi_instance *inst = multi_get_instance_by_virtual_addr(m, &dest, dev_type == DEV_TYPE_TUN);
if (inst)
{
int leng = m->inst_leng;
for (int x = 0; x < leng; ++x)
{
if (m->inst_list[x] == inst)
{
m->inst_indx = x;
return true;
}
}
leng = min_max(leng, 0, TUN_BAT_MIN - 1);
m->inst_list[leng] = inst;
m->inst_indx = leng;
m->inst_leng = (leng + 1);
}
return true;
}
else
{
multi_set_pending(
Expand Down Expand Up @@ -3635,6 +3672,97 @@ multi_process_incoming_tun(struct multi_context *m, const unsigned int mpp_flags
return ret;
}

bool multi_process_post_part2(struct multi_context *m, const unsigned int mpp_flags)
{
if (!INST_LENG(m))
{
return false;
}
struct multi_instance *i = m->inst_list[m->inst_indx];
if (!i)
{
m->inst_indx += 1;
return false;
}
if (!(multi_output_queue_ready(m, i)))
{
return false;
}
multi_set_pending(m, i);
set_prefix(m->pending);
multi_process_post(m, m->pending, mpp_flags);
clear_prefix();
m->inst_list[m->inst_indx] = NULL;
m->inst_indx += 1;
return true;
}

bool multi_process_incoming_tun_part3(struct multi_context *m, const unsigned int mpp_flags)
{
struct context *c, *b = &(m->top);
struct multi_instance *i;
int leng = b->c2.buffers->bulk_leng;
m->inst_indx = -1;
m->inst_leng = -1;
for (int x = 0; x < leng; ++x)
{
m->inst_indx = -9;
m->top.c2.buf = b->c2.bufs[x];
multi_process_incoming_tun_part2(m, mpp_flags);
if (m->inst_indx > -1)
{
i = m->inst_list[m->inst_indx];
c = &(i->context);
int y = min_max(c->c2.buffers->bulk_leng, 0, TUN_BAT_MIN - 1);
c->c2.buffers->read_tun_bufs[y].offset = TUN_BAT_OFF;
c->c2.buffers->read_tun_bufs[y].len = BLEN(&b->c2.bufs[x]);
bcopy(BPTR(&b->c2.bufs[x]), BPTR(&c->c2.buffers->read_tun_bufs[y]), BLEN(&b->c2.bufs[x]));
c->c2.bufs[y] = c->c2.buffers->read_tun_bufs[y];
c->c2.buffers->bulk_indx = 0;
c->c2.buffers->bulk_leng = (y + 1);
}
}
for (int x = 0; x < m->inst_leng; ++x)
{
i = m->inst_list[x];
c = &(i->context);
c->c2.buf = c->c2.bufs[0];
process_incoming_tun(c, c->c2.link_sockets[0]);
}
b->c2.buffers->bulk_indx = -1;
b->c2.buffers->bulk_leng = -1;
m->inst_indx = 0;
return multi_process_post_part2(m, mpp_flags);
}

bool multi_process_incoming_tun(struct multi_context *m, const unsigned int mpp_flags)
{
if (!(m->top.options.ce.bulk_mode)) {
return multi_process_incoming_tun_part2(m, mpp_flags);
} else {
return multi_process_incoming_tun_part3(m, mpp_flags);
}
}

bool multi_in_tun(struct multi_context *m, const unsigned int mpp_flags)
{
if (INST_LENG(m))
{
multi_process_post_part2(m, mpp_flags);
}
else
{
struct context *c = &(m->top);
read_incoming_tun(c);
if (!IS_SIG(c))
{
multi_process_incoming_tun(m, mpp_flags);
}
return true;
}
return false;
}

/*
* Process a possible client-to-client/bcast/mcast message in the
* queue.
Expand Down
Loading