-
Notifications
You must be signed in to change notification settings - Fork 28
/
kv_lib.c
468 lines (391 loc) · 11.5 KB
/
kv_lib.c
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/**
* @file
* KidVPN library.
* Verification using sylixos(tm) real-time operating system
*/
/*
* Copyright (c) 2006-2018 SylixOS Group.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
* 4. This code has been or is applying for intellectual property protection
* and can only be used with acoinfo software products.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*
* Author: Han.hui <hanhui@acoinfo.com>
*
*/
#include "kv_lib.h"
/* CBC enable */
int kv_cbc_en = 0;
/* CBC iv */
static unsigned char kv_cbc_iv[KV_CIPHER_IV_LEN];
/* CBC iv file */
static char *kv_cbc_iv_file = NULL;
/* vnd/tap if name */
static char kv_vnd_ifname[IFNAMSIZ];
/*
* init vnd device
*/
int kv_lib_init (int vnd_id, const char *tap_name, int *s_fd, int *v_fd, UINT8 hwaddr[], int mtu)
{
int i, so_fd = -1, vnd_fd = -1;
struct ifreq req;
#ifdef SYLIXOS
struct ifvnd vnd;
#endif /* SYLIXOS */
so_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (so_fd < 0) {
fprintf(stderr, "[KidVPN] Can not open socket error(%d): %s\n", errno, strerror(errno));
goto error;
}
#ifdef SYLIXOS
vnd_fd = open(IF_VND_PATH, O_RDWR);
if (vnd_fd < 0) {
fprintf(stderr, "[KidVPN] Can not open %s error(%d): %s\n", IF_VND_PATH, errno, strerror(errno));
goto error;
}
vnd.ifvnd_id = vnd_id;
if (ioctl(vnd_fd, SIOCVNDSEL, &vnd)) {
fprintf(stderr, "[KidVPN] Command 'SIOCVNDSEL' error(%d): %s\n", errno, strerror(errno));
goto error;
}
if (vnd.ifvnd_type != IF_VND_TYPE_ETHERNET) {
fprintf(stderr, "[KidVPN] Virtual net device MUST a ethernet type.\n");
goto error;
}
strcpy(req.ifr_name, vnd.ifvnd_ifname);
#else /* SYLIXOS */
vnd_fd = open("/dev/net/tun", O_RDWR);
if (vnd_fd < 0) {
fprintf(stderr, "[KidVPN] Can not open %s error(%d): %s\n", "/dev/net/tun", errno, strerror(errno));
goto error;
}
bzero(&req, sizeof(req));
if (tap_name && vnd_id < 0) {
strncpy(req.ifr_name, tap_name, IFNAMSIZ);
req.ifr_name[IFNAMSIZ - 1] = '\0';
} else {
snprintf(req.ifr_name, IFNAMSIZ, "tap%d", vnd_id);
}
req.ifr_flags = IFF_TAP | IFF_NO_PI;
if (ioctl(vnd_fd, TUNSETIFF, (void *)&req)) {
fprintf(stderr, "[KidVPN] Command 'TUNSETIFF' error(%d): %s\n", errno, strerror(errno));
goto error;
}
#endif /* !SYLIXOS */
printf("[KidVPN] We use virtual net device: %s for VPN connect.\n", req.ifr_name);
if (ioctl(so_fd, SIOCGIFHWADDR, &req)) {
fprintf(stderr, "[KidVPN] Command 'SIOCGIFHWADDR' error(%d): %s\n", errno, strerror(errno));
goto error;
}
for (i = 0; i < ETH_ALEN; i++) {
hwaddr[i] = req.ifr_hwaddr.sa_data[i];
}
if ((hwaddr[0] == 0) && (hwaddr[1] == 0) && (hwaddr[2] == 0) &&
(hwaddr[3] == 0) && (hwaddr[4] == 0) && (hwaddr[5] == 0)) {
fprintf(stderr, "[KidVPN] Virtual net device hwaddr error.\n");
goto error;
}
req.ifr_mtu = mtu;
if (ioctl(so_fd, SIOCSIFMTU, &req)) {
fprintf(stderr, "[KidVPN] Command 'SIOCSIFMTU' (%d) error(%d): %s\n", mtu, errno, strerror(errno));
goto error;
}
strcpy(kv_vnd_ifname, req.ifr_name);
if (s_fd) {
*s_fd = so_fd;
}
if (v_fd) {
*v_fd = vnd_fd;
}
return (0);
error:
if (so_fd >= 0) {
close(so_fd);
}
if (vnd_fd >= 0) {
close(vnd_fd);
}
return (-1);
}
/*
* deinit vnd device
*/
void kv_lib_deinit (int s_fd, int v_fd)
{
if (s_fd >= 0) {
close(s_fd);
}
if (v_fd >= 0) {
close(v_fd);
}
}
/*
* KidVPN set MTU
*/
int kv_lib_setmtu (int s_fd, int mtu)
{
struct ifreq req;
strcpy(req.ifr_name, kv_vnd_ifname);
if (ioctl(s_fd, SIOCGIFMTU, &req)) {
fprintf(stderr, "[KidVPN] Command 'SIOCGIFMTU' (%d) error(%d): %s\n", mtu, errno, strerror(errno));
return (-1);
}
if (mtu == req.ifr_mtu) {
return (0);
}
req.ifr_mtu = mtu;
if (ioctl(s_fd, SIOCSIFMTU, &req)) {
fprintf(stderr, "[KidVPN] Command 'SIOCSIFMTU' (%d) error(%d): %s\n", mtu, errno, strerror(errno));
return (-1);
}
return (0);
}
/*
* KidVPN update iv buffer
*/
int kv_lib_update_iv (const char *iv_file)
{
FILE *fp;
char buf[256];
if (iv_file) {
kv_cbc_iv_file = strdup(iv_file);
if (!kv_cbc_iv_file) {
fprintf(stderr, "[KidVPN] Not enough memory.\n");
return (-1);
}
} else if (!kv_cbc_iv_file) {
fprintf(stderr, "[KidVPN] No IV file specified.\n");
return (-1);
}
fp = fopen(kv_cbc_iv_file, "r");
if (!fp) {
fprintf(stderr, "[KidVPN] Open %s error(%d): %s\n", kv_cbc_iv_file, errno, strerror(errno));
return (-1);
}
if (!fgets(buf, sizeof(buf), fp)) { /* read iv */
fprintf(stderr, "[KidVPN] IV file %s error(%d): %s\n", kv_cbc_iv_file, errno, strerror(errno));
fclose(fp);
return (-1);
}
buf[sizeof(buf) - 1] = '\0';
fclose(fp);
#ifdef USE_OPENSSL
MD5((unsigned char *)buf, strlen(buf), kv_cbc_iv);
#else /* USE_OPENSSL */
mbedtls_md5((unsigned char *)buf, strlen(buf), kv_cbc_iv);
#endif /* !USE_OPENSSL */
kv_cbc_en = 1;
return (0);
}
/*
* KidVPN encode
*/
#ifdef USE_OPENSSL
void kv_lib_encode (UINT8 *out, UINT8 *in, int len, int *rlen, AES_KEY *aes_en)
#else /* USE_OPENSSL */
void kv_lib_encode (UINT8 *out, UINT8 *in, int len, int *rlen, mbedtls_aes_context *aes_en)
#endif /* !USE_OPENSSL */
{
int aes_len = ROUND_UP(len, KV_AES_BLK_LEN);
int spare;
int i, times;
unsigned char *iv;
if (rlen) {
*rlen = aes_len;
}
if (aes_len > len) {
spare = aes_len - len;
bzero(out + len, spare);
bzero(in + len, spare);
}
times = aes_len >> KV_AES_BLK_SHIFT; /* aes_len / 16 */
if (kv_cbc_en) { /* CBC */
int j;
iv = kv_cbc_iv;
for (i = 0; i < times; i++) {
for (j = 0; j < KV_CIPHER_IV_LEN; j++) {
out[j] = (unsigned char)(in[j] ^ iv[j]);
}
#ifdef USE_OPENSSL
AES_encrypt(out, out, aes_en);
#else /* USE_OPENSSL */
mbedtls_aes_crypt_ecb(aes_en, MBEDTLS_AES_ENCRYPT, out, out);
#endif /* !USE_OPENSSL */
iv = out;
in += KV_AES_BLK_LEN;
out += KV_AES_BLK_LEN;
}
} else { /* ECB */
for (i = 0; i < times; i++) {
#ifdef USE_OPENSSL
AES_encrypt(in, out, aes_en);
#else /* USE_OPENSSL */
mbedtls_aes_crypt_ecb(aes_en, MBEDTLS_AES_ENCRYPT, in, out);
#endif /* !USE_OPENSSL */
in += KV_AES_BLK_LEN;
out += KV_AES_BLK_LEN;
}
}
}
/*
* KidVPN decode
*/
#ifdef USE_OPENSSL
void kv_lib_decode (UINT8 *out, UINT8 *in, int len, int *rlen, AES_KEY *aes_de)
#else /* USE_OPENSSL */
void kv_lib_decode (UINT8 *out, UINT8 *in, int len, int *rlen, mbedtls_aes_context *aes_de)
#endif /* !USE_OPENSSL */
{
int aes_len = ROUND_UP(len, KV_AES_BLK_LEN);
int spare;
int i, times;
unsigned char *iv;
if (rlen) {
*rlen = aes_len;
}
if (aes_len > len) {
spare = aes_len - len;
bzero(out + len, spare);
bzero(in + len, spare);
}
times = aes_len >> KV_AES_BLK_SHIFT; /* aes_len / 16 */
if (kv_cbc_en) { /* CBC */
int j;
iv = kv_cbc_iv;
for (i = 0; i < times; i++) {
#ifdef USE_OPENSSL
AES_decrypt(in, out, aes_de);
#else /* USE_OPENSSL */
mbedtls_aes_crypt_ecb(aes_de, MBEDTLS_AES_DECRYPT, in, out);
#endif /* !USE_OPENSSL */
for (j = 0; j < KV_CIPHER_IV_LEN; j++) {
out[j] = (unsigned char)(out[j] ^ iv[j]);
}
iv = in;
in += KV_AES_BLK_LEN;
out += KV_AES_BLK_LEN;
}
} else { /* ECB */
for (i = 0; i < times; i++) {
#ifdef USE_OPENSSL
AES_decrypt(in, out, aes_de);
#else /* USE_OPENSSL */
mbedtls_aes_crypt_ecb(aes_de, MBEDTLS_AES_DECRYPT, in, out);
#endif /* !USE_OPENSSL */
in += KV_AES_BLK_LEN;
out += KV_AES_BLK_LEN;
}
}
}
/*
* KidVPN address is same
*/
int kv_lib_addr_is_same (struct sockaddr_in *addr1, struct sockaddr_in *addr2)
{
if ((addr1->sin_port == addr2->sin_port) &&
(addr1->sin_addr.s_addr == addr2->sin_addr.s_addr)) {
return (1);
}
return (0);
}
/*
* KidVPN client hash
*/
int kv_lib_cli_hash (UINT8 hwaddr[])
{
int hash;
hash = hwaddr[0]
+ hwaddr[1]
+ hwaddr[2]
+ hwaddr[3]
+ hwaddr[4]
+ hwaddr[5];
return (hash & KV_CLI_HASH_MASK);
}
/*
* KidVPN add a new client into list
*/
void kv_lib_cli_add (struct kv_cli_node *cli, struct kv_cli_node *header[])
{
int hash = kv_lib_cli_hash(cli->hwaddr);
cli->next = header[hash];
cli->prev = NULL;
if (header[hash]) {
header[hash]->prev = cli;
}
header[hash] = cli;
}
/*
* KidVPN delete a client from list
*/
void kv_lib_cli_delete (struct kv_cli_node *cli, struct kv_cli_node *header[])
{
int hash = kv_lib_cli_hash(cli->hwaddr);
if (header[hash] == cli) {
header[hash] = cli->next;
}
if (cli->next) {
cli->next->prev = cli->prev;
}
if (cli->prev) {
cli->prev->next = cli->next;
}
}
/*
* KidVPN find client from list
*/
struct kv_cli_node *kv_lib_cli_find (UINT8 hwaddr[], struct kv_cli_node *header[])
{
struct kv_cli_node *cli;
int hash = kv_lib_cli_hash(hwaddr);
for (cli = header[hash]; cli != NULL; cli = cli->next) {
if (!memcmp(cli->hwaddr, hwaddr, ETH_ALEN)) {
return (cli);
}
}
return (NULL);
}
#if KV_VOLUNTARILY_QUIT
/*
* signalfd init
*/
int kv_lib_signalfd (void)
{
int sigfd;
sigset_t mask;
sigemptyset(&mask);
sigaddset(&mask, SIGTERM);
sigaddset(&mask, SIGUSR1);
sigprocmask(SIG_BLOCK, &mask, NULL);
sigfd = signalfd(-1, &mask, SFD_CLOEXEC | SFD_NONBLOCK);
if (sigfd < 0) {
fprintf(stderr, "[KidVPN] Can not open signalfd, error(%d): %s\n", errno, strerror(errno));
return (-1);
}
return (sigfd);
}
#endif /* KV_VOLUNTARILY_QUIT */
/*
* end
*/