1 /*
2     VTun - Virtual Tunnel over TCP/IP network.
3 
4     Copyright (C) 1998-2016  Maxim Krasnyansky <max_mk@yahoo.com>
5 
6     VTun has been derived from VPPP package by Maxim Krasnyansky.
7 
8     This program is free software; you can redistribute it and/or modify
9     it under the terms of the GNU General Public License as published by
10     the Free Software Foundation; either version 2 of the License, or
11     (at your option) any later version.
12 
13     This program is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17  */
18 
19 /*
20  * $Id: vtun.h,v 1.12.2.9 2016/10/01 21:27:51 mtbishop Exp $
21  */
22 
23 #ifndef _VTUN_H
24 #define _VTUN_H
25 
26 #include "llist.h"
27 
28 /* Default VTUN port */
29 #define VTUN_PORT 5000
30 
31 /* Default VTUN connect timeout in sec */
32 #define VTUN_CONNECT_TIMEOUT 30
33 
34 /* General VTUN timeout for several operations, in sec */
35 #define VTUN_TIMEOUT 30
36 
37 /* Number of seconds for delay after pppd startup*/
38 #define VTUN_DELAY_SEC  10
39 
40 /* Statistic interval in seconds */
41 #define VTUN_STAT_IVAL  5*60  /* 5 min */
42 
43 /* Max lenght of device name */
44 #define VTUN_DEV_LEN  20
45 
46 /* End of configurable part */
47 
48 struct vtun_sopt {
49     char *dev;
50     char *laddr;
51     int  lport;
52     char *raddr;
53     int  rport;
54     char *host;
55 };
56 
57 struct vtun_stat {
58    unsigned long byte_in;
59    unsigned long byte_out;
60    unsigned long comp_in;
61    unsigned long comp_out;
62    FILE *file;
63 };
64 
65 struct vtun_cmd {
66    char *prog;
67    char *args;
68    int  flags;
69 };
70 /* Command flags */
71 #define VTUN_CMD_WAIT	0x01
72 #define VTUN_CMD_DELAY  0x02
73 #define VTUN_CMD_SHELL  0x04
74 
75 struct vtun_addr {
76    char *name;
77    char *ip;
78    int port;
79    int type;
80 };
81 /* Address types */
82 #define VTUN_ADDR_IFACE	0x01
83 #define VTUN_ADDR_NAME  0x02
84 
85 struct vtun_host {
86    char *host;
87    char *passwd;
88    char *dev;
89 
90    llist up;
91    llist down;
92 
93    int  flags;
94    int  timeout;
95    int  spd_in;
96    int  spd_out;
97    int  zlevel;
98    int  cipher;
99 
100    int  rmt_fd;
101    int  loc_fd;
102 
103    /* Persist mode */
104    int  persist;
105 
106    /* Multiple connections */
107    int  multi;
108 
109    /* Keep Alive */
110    int ka_interval;
111    int ka_maxfail;
112 
113    /* Source address */
114    struct vtun_addr src_addr;
115 
116    struct vtun_stat stat;
117 
118    struct vtun_sopt sopt;
119 };
120 
121 extern llist host_list;
122 
123 /* Flags definitions */
124 #define VTUN_TTY        0x0100
125 #define VTUN_PIPE       0x0200
126 #define VTUN_ETHER      0x0400
127 #define VTUN_TUN        0x0800
128 #define VTUN_TYPE_MASK  (VTUN_TTY | VTUN_PIPE | VTUN_ETHER | VTUN_TUN)
129 
130 #define VTUN_TCP        0x0010
131 #define VTUN_UDP        0x0020
132 #define VTUN_PROT_MASK  (VTUN_TCP | VTUN_UDP)
133 #define VTUN_KEEP_ALIVE 0x0040
134 
135 #define VTUN_ZLIB       0x0001
136 #define VTUN_LZO        0x0002
137 #define VTUN_SHAPE      0x0004
138 #define VTUN_ENCRYPT    0x0008
139 
140 /* Cipher options */
141 #define VTUN_ENC_BF128ECB	1
142 #define VTUN_ENC_BF128CBC	2
143 #define VTUN_ENC_BF128CFB	3
144 #define VTUN_ENC_BF128OFB	4
145 #define VTUN_ENC_BF256ECB	5
146 #define VTUN_ENC_BF256CBC	6
147 #define VTUN_ENC_BF256CFB	7
148 #define VTUN_ENC_BF256OFB	8
149 
150 #define VTUN_ENC_AES128ECB	9
151 #define VTUN_ENC_AES128CBC	10
152 #define VTUN_ENC_AES128CFB	11
153 #define VTUN_ENC_AES128OFB	12
154 #define VTUN_ENC_AES256ECB	13
155 #define VTUN_ENC_AES256CBC	14
156 #define VTUN_ENC_AES256CFB	15
157 #define VTUN_ENC_AES256OFB	16
158 
159 #define VTUN_LEGACY_ENCRYPT	999
160 
161 /* Mask to drop the flags which will be supplied by the server */
162 #define VTUN_CLNT_MASK  0xf000
163 
164 #define VTUN_STAT	0x1000
165 #define VTUN_PERSIST    0x2000
166 
167 #ifdef ENABLE_NAT_HACK
168 /* Flags for the NAT hack with delayed UDP socket connect */
169 #define VTUN_NAT_HACK_CLIENT	0x4000
170 #define VTUN_NAT_HACK_SERVER	0x8000
171 #define VTUN_NAT_HACK_MASK	(VTUN_NAT_HACK_CLIENT | VTUN_NAT_HACK_SERVER)
172 
173 #define VTUN_USE_NAT_HACK(host)	((host)->flags & VTUN_NAT_HACK_MASK)
174 #else
175 #define VTUN_USE_NAT_HACK(host)	0
176 #endif
177 
178 /* Constants and flags for VTun protocol */
179 #define VTUN_FRAME_SIZE     2048
180 #define VTUN_FRAME_OVERHEAD 100
181 #define VTUN_FSIZE_MASK 0x0fff
182 
183 #define VTUN_CONN_CLOSE 0x1000
184 #define VTUN_ECHO_REQ	0x2000
185 #define VTUN_ECHO_REP	0x4000
186 #define VTUN_BAD_FRAME  0x8000
187 
188 /* Authentication message size */
189 #define VTUN_MESG_SIZE	50
190 
191 /* Support for multiple connections */
192 #define VTUN_MULTI_DENY		0  /* no */
193 #define VTUN_MULTI_ALLOW	1  /* yes */
194 #define VTUN_MULTI_KILL		2
195 
196 /* keep interface in persistant mode */
197 #define VTUN_PERSIST_KEEPIF     2
198 
199 /* Values for the signal flag */
200 
201 #define VTUN_SIG_TERM 1
202 #define VTUN_SIG_HUP  2
203 
204 /* Global options */
205 struct vtun_opts {
206    int  timeout;
207    int  persist;
208 
209    char *cfg_file;
210 
211    char *shell; 	 /* Shell */
212    char *ppp;		 /* Command to configure ppp devices */
213    char *ifcfg;		 /* Command to configure net devices */
214    char *route;		 /* Command to configure routing */
215    char *fwall; 	 /* Command to configure FireWall */
216    char *iproute;	 /* iproute command */
217 
218    char *svr_name;       /* Server's host name */
219    char *svr_addr;       /* Server's address (string) */
220    struct vtun_addr bind_addr;	 /* Server should listen on this address */
221    int  svr_type;	 /* Server mode */
222    int  syslog; 	 /* Facility to log messages to syslog under */
223    int  quiet;		 /* Be quiet about common errors */
224 };
225 #define VTUN_STAND_ALONE	0
226 #define VTUN_INETD		1
227 
228 extern struct vtun_opts vtun;
229 
230 void server(int sock);
231 void client(struct vtun_host *host);
232 int  tunnel(struct vtun_host *host);
233 int  read_config(char *file);
234 struct vtun_host * find_host(char *host);
235 inline void clear_nat_hack_flags(int svr);
236 
237 #endif
238