1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
|
/*
VTun - Virtual Tunnel over TCP/IP network.
Copyright (C) 1998-2016 Maxim Krasnyansky <max_mk@yahoo.com>
VTun has been derived from VPPP package by Maxim Krasnyansky.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
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 General Public License for more details.
*/
/*
* $Id: linkfd.c,v 1.13.2.7 2016/10/01 21:27:51 mtbishop Exp $
*/
#include "config.h"
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <strings.h>
#include <string.h>
#include <errno.h>
#include <sys/time.h>
#include <syslog.h>
#include <time.h>
#ifdef HAVE_SYS_RESOURCE_H
#include <sys/resource.h>
#endif
#ifdef HAVE_SCHED_H
#include <sched.h>
#endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#include "vtun.h"
#include "linkfd.h"
#include "lib.h"
#include "driver.h"
/* used by lfd_encrypt */
int send_a_packet = 0;
/* Host we are working with.
* Used by signal handlers that's why it is global.
*/
static struct vtun_host *lfd_host;
static struct lfd_mod *lfd_mod_head = NULL, *lfd_mod_tail = NULL;
/* Modules functions*/
/* Add module to the end of modules list */
static void lfd_add_mod(struct lfd_mod *mod)
{
if( !lfd_mod_head ){
lfd_mod_head = lfd_mod_tail = mod;
mod->next = mod->prev = NULL;
} else {
lfd_mod_tail->next = mod;
mod->prev = lfd_mod_tail;
mod->next = NULL;
lfd_mod_tail = mod;
}
}
/* Initialize and allocate each module */
static int lfd_alloc_mod(struct vtun_host *host)
{
struct lfd_mod *mod = lfd_mod_head;
while( mod ){
if( mod->alloc && (mod->alloc)(host) )
return 1;
mod = mod->next;
}
return 0;
}
/* Free all modules */
static int lfd_free_mod(void)
{
struct lfd_mod *mod = lfd_mod_head;
while( mod ){
if( mod->free && (mod->free)() )
return 1;
mod = mod->next;
}
lfd_mod_head = lfd_mod_tail = NULL;
return 0;
}
/* Run modules down (from head to tail) */
static inline int lfd_run_down(int len, char *in, char **out)
{
register struct lfd_mod *mod;
*out = in;
for(mod = lfd_mod_head; mod && len > 0; mod = mod->next )
if( mod->encode ){
len = (mod->encode)(len, in, out);
in = *out;
}
return len;
}
/* Run modules up (from tail to head) */
static inline int lfd_run_up(int len, char *in, char **out)
{
register struct lfd_mod *mod;
*out = in;
for(mod = lfd_mod_tail; mod && len > 0; mod = mod->prev )
if( mod->decode ){
len = (mod->decode)(len, in, out);
in = *out;
}
return len;
}
/* Check if modules are accepting the data(down) */
static inline int lfd_check_down(void)
{
register struct lfd_mod *mod;
int err = 1;
for(mod = lfd_mod_head; mod && err > 0; mod = mod->next )
if( mod->avail_encode )
err = (mod->avail_encode)();
return err;
}
/* Check if modules are accepting the data(up) */
static inline int lfd_check_up(void)
{
register struct lfd_mod *mod;
int err = 1;
for(mod = lfd_mod_tail; mod && err > 0; mod = mod->prev)
if( mod->avail_decode )
err = (mod->avail_decode)();
return err;
}
/********** Linker *************/
/* Termination flag */
static volatile sig_atomic_t linker_term;
static void sig_term(int sig)
{
vtun_syslog(LOG_INFO, "Closing connection");
io_cancel();
linker_term = VTUN_SIG_TERM;
}
static void sig_hup(int sig)
{
vtun_syslog(LOG_INFO, "Reestablishing connection");
io_cancel();
linker_term = VTUN_SIG_HUP;
}
/* Statistic dump and keep-alive monitor */
static volatile sig_atomic_t ka_need_verify = 0;
static time_t stat_timer = 0, ka_timer = 0;
static void sig_alarm(int sig)
{
static time_t tm_old, tm = 0;
static char stm[20];
tm_old = tm;
tm = time(NULL);
if( (lfd_host->flags & VTUN_KEEP_ALIVE) && (ka_timer -= tm-tm_old) <= 0){
ka_need_verify = 1;
ka_timer = lfd_host->ka_interval
+ 1; /* We have to complete select() on idle */
}
if( (lfd_host->flags & VTUN_STAT) && (stat_timer -= tm-tm_old) <= 0){
strftime(stm, sizeof(stm)-1, "%b %d %H:%M:%S", localtime(&tm));
fprintf(lfd_host->stat.file,"%s %lu %lu %lu %lu\n", stm,
lfd_host->stat.byte_in, lfd_host->stat.byte_out,
lfd_host->stat.comp_in, lfd_host->stat.comp_out);
stat_timer = VTUN_STAT_IVAL;
}
if ( ka_timer*stat_timer ){
alarm( (ka_timer < stat_timer) ? ka_timer : stat_timer );
} else {
alarm( (ka_timer) ? ka_timer : stat_timer );
}
}
static void sig_usr1(int sig)
{
/* Reset statistic counters on SIGUSR1 */
lfd_host->stat.byte_in = lfd_host->stat.byte_out = 0;
lfd_host->stat.comp_in = lfd_host->stat.comp_out = 0;
}
static int lfd_linker(void)
{
int fd1 = lfd_host->rmt_fd;
int fd2 = lfd_host->loc_fd;
register int len, fl;
struct timeval tv;
char *buf, *out;
fd_set fdset;
int maxfd, idle = 0, tmplen;
if( !(buf = lfd_alloc(VTUN_FRAME_SIZE + VTUN_FRAME_OVERHEAD)) ){
vtun_syslog(LOG_ERR,"Can't allocate buffer for the linker");
return 0;
}
/* Delay sending of first UDP packet over broken NAT routers
because we will probably be disconnected. Wait for the remote
end to send us something first, and use that connection. */
if (!VTUN_USE_NAT_HACK(lfd_host))
proto_write(fd1, buf, VTUN_ECHO_REQ);
maxfd = (fd1 > fd2 ? fd1 : fd2) + 1;
linker_term = 0;
while( !linker_term ){
errno = 0;
/* Wait for data */
FD_ZERO(&fdset);
FD_SET(fd1, &fdset);
FD_SET(fd2, &fdset);
tv.tv_sec = lfd_host->ka_interval;
tv.tv_usec = 0;
if( (len = select(maxfd, &fdset, NULL, NULL, &tv)) < 0 ){
if( errno != EAGAIN && errno != EINTR )
break;
else
continue;
}
if( ka_need_verify ){
if( idle > lfd_host->ka_maxfail ){
vtun_syslog(LOG_INFO,"Session %s network timeout", lfd_host->host);
break;
}
if (idle++ > 0) { /* No input frames, check connection with ECHO */
if( proto_write(fd1, buf, VTUN_ECHO_REQ) < 0 ){
vtun_syslog(LOG_ERR,"Failed to send ECHO_REQ");
break;
}
}
ka_need_verify = 0;
}
if (send_a_packet)
{
send_a_packet = 0;
tmplen = 1;
lfd_host->stat.byte_out += tmplen;
if( (tmplen=lfd_run_down(tmplen,buf,&out)) == -1 )
break;
if( tmplen && proto_write(fd1, out, tmplen) < 0 )
break;
lfd_host->stat.comp_out += tmplen;
}
/* Read frames from network(fd1), decode and pass them to
* the local device (fd2) */
if( FD_ISSET(fd1, &fdset) && lfd_check_up() ){
idle = 0; ka_need_verify = 0;
if( (len=proto_read(fd1, buf)) <= 0 )
break;
/* Handle frame flags */
fl = len & ~VTUN_FSIZE_MASK;
len = len & VTUN_FSIZE_MASK;
if( fl ){
if( fl==VTUN_BAD_FRAME ){
vtun_syslog(LOG_ERR, "Received bad frame");
continue;
}
if( fl==VTUN_ECHO_REQ ){
/* Send ECHO reply */
if( proto_write(fd1, buf, VTUN_ECHO_REP) < 0 )
break;
continue;
}
if( fl==VTUN_ECHO_REP ){
/* Just ignore ECHO reply, ka_need_verify==0 already */
continue;
}
if( fl==VTUN_CONN_CLOSE ){
vtun_syslog(LOG_INFO,"Connection closed by other side");
break;
}
}
lfd_host->stat.comp_in += len;
if( (len=lfd_run_up(len,buf,&out)) == -1 )
break;
if( len && dev_write(fd2,out,len) < 0 ){
if( errno != EAGAIN && errno != EINTR )
break;
else
continue;
}
lfd_host->stat.byte_in += len;
}
/* Read data from the local device(fd2), encode and pass it to
* the network (fd1) */
if( FD_ISSET(fd2, &fdset) && lfd_check_down() ){
if( (len = dev_read(fd2, buf, VTUN_FRAME_SIZE)) < 0 ){
if( errno != EAGAIN && errno != EINTR )
break;
else
continue;
}
if( !len ) break;
lfd_host->stat.byte_out += len;
if( (len=lfd_run_down(len,buf,&out)) == -1 )
break;
if( len && proto_write(fd1, out, len) < 0 )
break;
lfd_host->stat.comp_out += len;
}
}
if( !linker_term && errno )
vtun_syslog(LOG_INFO,"%s (%d)", strerror(errno), errno);
if (linker_term == VTUN_SIG_TERM) {
lfd_host->persist = 0;
}
/* Notify other end about our close */
proto_write(fd1, buf, VTUN_CONN_CLOSE);
lfd_free(buf);
return 0;
}
/* Link remote and local file descriptors */
int linkfd(struct vtun_host *host)
{
struct sigaction sa, sa_oldterm, sa_oldint, sa_oldhup;
int old_prio;
lfd_host = host;
old_prio=getpriority(PRIO_PROCESS,0);
setpriority(PRIO_PROCESS,0,LINKFD_PRIO);
/* Build modules stack */
if(host->flags & VTUN_ZLIB)
lfd_add_mod(&lfd_zlib);
if(host->flags & VTUN_LZO)
lfd_add_mod(&lfd_lzo);
if(host->flags & VTUN_ENCRYPT)
if(host->cipher == VTUN_LEGACY_ENCRYPT) {
lfd_add_mod(&lfd_legacy_encrypt);
} else {
lfd_add_mod(&lfd_encrypt);
}
if(host->flags & VTUN_SHAPE)
lfd_add_mod(&lfd_shaper);
if(lfd_alloc_mod(host))
return 0;
memset(&sa, 0, sizeof(sa));
sa.sa_handler=sig_term;
sigaction(SIGTERM,&sa,&sa_oldterm);
sigaction(SIGINT,&sa,&sa_oldint);
sa.sa_handler=sig_hup;
sigaction(SIGHUP,&sa,&sa_oldhup);
/* Initialize keep-alive timer */
if( host->flags & (VTUN_STAT|VTUN_KEEP_ALIVE) ){
sa.sa_handler=sig_alarm;
sigaction(SIGALRM,&sa,NULL);
alarm( (host->ka_interval < VTUN_STAT_IVAL) ?
host->ka_interval : VTUN_STAT_IVAL );
}
/* Initialize statstic dumps */
if( host->flags & VTUN_STAT ){
char file[40];
sa.sa_handler=sig_alarm;
sigaction(SIGALRM,&sa,NULL);
sa.sa_handler=sig_usr1;
sigaction(SIGUSR1,&sa,NULL);
sprintf(file,"%s/%.20s.dump", VTUN_STAT_DIR, host->host);
if( (host->stat.file=fopen(file, "a")) ){
setvbuf(host->stat.file, NULL, _IOLBF, 0);
} else
vtun_syslog(LOG_ERR, "Can't open stats file %s", file);
}
io_init();
lfd_linker();
if( host->flags & (VTUN_STAT|VTUN_KEEP_ALIVE) ){
alarm(0);
if (host->stat.file)
fclose(host->stat.file);
}
lfd_free_mod();
sigaction(SIGTERM,&sa_oldterm,NULL);
sigaction(SIGINT,&sa_oldint,NULL);
sigaction(SIGHUP,&sa_oldhup,NULL);
setpriority(PRIO_PROCESS,0,old_prio);
return linker_term;
}
|